News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Compare 2 pixel

Started by Farabi, May 23, 2011, 02:02:18 PM

Previous topic - Next topic

Farabi


to=tolerance of difference between 2 pixel
CmpPix proc uses esi edi ecx  pix1:dword,pix2:dword,tol:dword
LOCAL r3,g3,b3,r4,g4,b4:dword

mov eax,pix1
mov edx,pix2

movzx ecx,al
mov r3,ecx
shr ecx,8
movzx ecx,al
mov g3,ecx
shr ecx,8
movzx ecx,al
mov b3,ecx

movzx ecx,dl
mov r4,ecx
shr ecx,8
movzx ecx,dl
mov g4,ecx
shr ecx,8
movzx ecx,dl
mov b4,ecx

mov eax,r3
sub eax,r4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
ret
@@:

mov eax,g3
sub eax,g4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
ret
@@:

mov eax,b3
sub eax,b4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
ret
@@:



xor eax,eax
inc eax

ret
CmpPix endp


I was wondering that our eyes is very sensitive to movement, I think it was because our brain was remembered the previous frame and then compared it to a new one, not just that, our eyes was very sensitif to color gradation too.
Im using that algo to compare between 2 pixel on a 2 images. From that, I can detect something is moving from a camera. Very eficient enough, 3% CPU Usage on mine. I used OpenCV to acess the camera. I guess I can speed up OpenCV if my experiment is done.

here is an example of above usage http://ompldr.org/vOHJkcQ/Lib.zip (7MB)
Source still messy so I did not included it, I will rearrange it

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Oh I forget, that application need a  camera, so it will crash if you dont have it.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

qWord

hi Farabi,
Windows pop up an dialog for setting up my microphone - Your program run, but it shows nothing. I'm using an Laptop with camera.
FPU in a trice: SmplMath
It's that simple!

RuiLoureiro

Farabi,
        1. Why «CmpPix proc uses esi edi ecx» if CmpPix doesnt use ESI and EDI ?
           It should be

Quote       
CmpPix proc uses ecx  pix1:dword,pix2:dword,tol:dword
   LOCAL r3,g3,b3,r4,g4,b4:dword
       
           
        2. There is a bug in your code. r3 is = g3 and is = b3 ever  (r3=g3=b3 ever)
       
Quote
   movzx ecx,al
   mov r3,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,al           ; <<<<< this move destroy ECX
   mov g3,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,al           ; <<<<< this move destroy ECX
   mov b3,ecx
       

        3. There is a bug in your code. r4 is = g4 and is = b4 ever  (r4=g4=b4 ever)

Quote       
   movzx ecx,dl
   mov r4,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,dl           ; <<<<< this move destroy ECX
   mov g4,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,dl           ; <<<<< this move destroy ECX
   mov b4,ecx

mineiro

#4
Hello Sr Farabi, I think you are rotating the wrong register in the beginning of your code.

I remember see one program, that is very similar, but the coder make an inverse of your program. This guy have get all movements in the image, and the objects that have slow difference in position is the target. He put some sounds when he detect these slow movements. Their program is to help blind people to walk in the street. So, objects that are a bit static emit some sounds different from objects that quickly appear in camera. I think 50 hertz can be a good aproach. The light appear to be static, but it is pulsing 50 times in one second, and our eyes cannot see this difference.
http://www.seeingwithsound.com/
On other hands, i remember see something about "retinex".

Tedd

A little correction and cleanup..
(Although the method could probably be improved, and MMX would be much better suited for this task.)

CmpPix proc pix1:dword,pix2:dword,tol:dword
    LOCAL r3,g3,b3,r4,g4,b4:dword

    xor ecx,ecx

    mov eax,pix1
    mov cl,al
    mov r3,ecx
    mov cl,ah
    mov g3,ecx
    shr eax,16
    mov b3,eax

    mov eax,pix2
    mov cl,al
    mov r4,ecx
    mov cl,ah
    mov g4,ecx
    shr eax,16
    mov b4,eax


    xor ecx,ecx     ;ecx = 0/FALSE

    mov eax,r3
    sub eax,r4      ;eax = r3-r4 (carry set if negative; i.e. r3<r4)
    sbb edx,edx     ;edx = -1 if carry, else 0
    sbb eax,ecx     ;subtract extra 1 for correction, if needed
    xor eax,edx     ;do negation, if needed
    cmp eax,tol
    jg @out

    mov eax,g3
    sub eax,g4
    sbb edx,edx
    sbb eax,ecx
    xor eax,edx
    cmp eax,tol
    jg @out

    mov eax,b3
    sub eax,b4
    sbb edx,edx
    sbb eax,ecx
    xor eax,edx
    cmp eax,tol
    jg @out

    mov ecx,TRUE
  @out:
    mov eax,ecx
    ret
CmpPix endp
No snowflake in an avalanche feels responsible.

Farabi

Quote from: RuiLoureiro on May 23, 2011, 04:27:24 PM
Farabi,
        1. Why «CmpPix proc uses esi edi ecx» if CmpPix doesnt use ESI and EDI ?
           It should be

Quote       
CmpPix proc uses ecx  pix1:dword,pix2:dword,tol:dword
   LOCAL r3,g3,b3,r4,g4,b4:dword
       
           
        2. There is a bug in your code. r3 is = g3 and is = b3 ever  (r3=g3=b3 ever)
       
Quote
   movzx ecx,al
   mov r3,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,al           ; <<<<< this move destroy ECX
   mov g3,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,al           ; <<<<< this move destroy ECX
   mov b3,ecx
       

        3. There is a bug in your code. r4 is = g4 and is = b4 ever  (r4=g4=b4 ever)

Quote       
   movzx ecx,dl
   mov r4,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,dl           ; <<<<< this move destroy ECX
   mov g4,ecx

   shr ecx,8              ; Why shr ECX if
   movzx ecx,dl           ; <<<<< this move destroy ECX
   mov b4,ecx


Thanks rui I making a mistake, fixed it, it become more sensitive to light changing and camera movement.


CmpPix proc uses esi edi ecx  pix1:dword,pix2:dword,tol:dword
LOCAL r3,g3,b3,r4,g4,b4:dword

mov eax,pix1
mov edx,pix2

movzx ecx,al
mov r3,ecx
shr eax,8
movzx ecx,al
mov g3,ecx
shr eax,8
movzx ecx,al
mov b3,ecx

movzx ecx,dl
mov r4,ecx
shr edx,8
movzx ecx,dl
mov g4,ecx
shr edx,8
movzx ecx,dl
mov b4,ecx

mov eax,r3
sub eax,r4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
ret
@@:

mov eax,g3
sub eax,g4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
ret
@@:

mov eax,b3
sub eax,b4
cmp eax,0
jg @f
neg eax
@@:
cmp eax,tol
jle @f
xor eax,eax
ret
@@:



xor eax,eax
inc eax

ret
CmpPix endp
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Quote from: qWord on May 23, 2011, 02:36:40 PM
hi Farabi,
Windows pop up an dialog for setting up my microphone - Your program run, but it shows nothing. I'm using an Laptop with camera.

Try to turn off another application that uses camera.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

#8
Replace the exe with this one http://ompldr.org/vOHM4eg/OpenCV.zip
It will follow any moving object. It only 7% CPU Usage. If you can provide me a simple single call camera function, I will replace the OpenCV function and cut it dependencies.



Edgars graphic lib is very fast.  :U
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Replace the exe with this newest one, more more more accurate.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi



Playing up with distinction between 2 pixel to detect edges. Good enough.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

#11
Here it is the final result of grayscale and Traceedge function

Dependency Donkey Graph lib
Quote
fGrayScale proc lpBitsSrc:dword,lpBitsRslt:dword,x:dword,y:dword,w:dword,h:Dword
   LOCAL _x,_y:dword
   LOCAL cp,np:dword
   LOCAL r,g,b:dword
   
   xor ecx,ecx
   mov ecx,y
   loop_y:
   push ecx
      mov _y,ecx
      xor ecx,ecx
      mov ecx,x
      loop_x:
      push ecx
         mov _x,ecx
         invoke GetDIBPixel,_x,_y,lpBitsSrc,w,h
         mov cp,eax
         
         movzx ecx,al
         mov b,ecx
         shr eax,8
         movzx ecx,al
         mov g,ecx
         shr eax,8
         movzx ecx,al
         mov r,ecx
         
         xor edx,edx
         mov eax,r
         mov ecx,30
         mul ecx
         mov ecx,100
         div ecx
         mov r,eax
         
         xor edx,edx
         mov eax,g
         mov ecx,59
         mul ecx
         mov ecx,100
         div ecx
         mov g,eax
         
         xor edx,edx
         mov eax,b
         mov ecx,11
         mul ecx
         mov ecx,100
         div ecx
         mov b,eax
         
         xor eax,eax
         add eax,r
         add eax,g
         add eax,b
         
      ;   xor edx,edx
      ;   mov ecx,3
      ;   div ecx
         
         xor ecx,ecx
         mov cl,al
         shl ecx,8
         mov cl,al
         shl ecx,8
         mov cl,al
         
         invoke SetDIBPixel,_x,_y,lpBitsRslt,w,h,ecx
         
      no_need:
      pop ecx
      inc ecx
      cmp ecx,w
      jl loop_x
   pop ecx
   inc ecx
   cmp ecx,h
   jl loop_y
   done:
   
   ret
fGrayScale endp

fTraceEdge proc uses esi edi lpbitsSrc:dword,lpbistRslt:dword,x:dword,y:dword,w:dword,h:dword
   LOCAL _x,_y:dword
   LOCAL cp,np:dword
   LOCAL t2,t3:dword
   
   mov esi,lpbitsSrc
   mov edi,lpbistRslt
   
   mov t2,2
   mov t3,5
   
   xor ecx,ecx
   mov ecx,y
   loop_y:
   push ecx
      mov _y,ecx
      xor ecx,ecx
      mov ecx,x
      loop_x:
      push ecx
         mov _x,ecx
         invoke GetDIBPixel,_x,_y,lpbitsSrc,w,h
         mov cp,eax
         mov eax,t2
         add _x,eax
         mov eax,w
         .if _x<eax
            invoke GetDIBPixel,_x,_y,lpbitsSrc,w,h
            mov np,eax
               invoke CmpPix,cp,np,t3
               .if eax==0
                  mov eax,t2
                  sub _x,eax
                  invoke SetDIBPixel,_x,_y,lpbistRslt,w,h,0FFFFFFh
               .endif
         .endif
      no_need:
      pop ecx
      inc ecx
      cmp ecx,w
      jl loop_x
   pop ecx
   inc ecx
   cmp ecx,h
   jl loop_y
   done:
   
   xor ecx,ecx
   mov ecx,x
   loop_y2:
   push ecx
      mov _x,ecx
      xor ecx,ecx
      mov ecx,y
      loop_x2:
      push ecx
         mov _y,ecx
         invoke GetDIBPixel,_x,_y,lpbitsSrc,w,h
         mov cp,eax
         mov eax,t2
         add _y,eax
         mov eax,h
         .if _y<eax
            invoke GetDIBPixel,_x,_y,lpbitsSrc,w,h
            mov np,eax
               invoke CmpPix,cp,np,t3
               .if eax==0
                  mov eax,t2
                  sub _y,eax
                  invoke SetDIBPixel,_x,_y,lpbistRslt,w,h,0FFFFFFh
               .endif
         .endif
      no_need2:
      pop ecx
      inc ecx
      cmp ecx,h
      jl loop_x2
   pop ecx
   inc ecx
   cmp ecx,w
   jl loop_y2
   done2:
   
   ret
fTraceEdge endp

How to use it
Quote
invoke GetObject,bmp,sizeof BITMAP,addr b
invoke GetObject,bmpRslt,sizeof BITMAP,addr b2

invoke fGrayScale,b.bmBits,b.bmBits,0,0,640,480
invoke fTraceEdge,b.bmBits,b2.bmBits,0,0,640,480


The result is good enough


Now Im stuck on the line position retrieval, too complicated for me.

So here is my plan, from the line retrieval, I will check conectivity beetween all lines to determined is it an object or not. After that done, I will take the texture, and remember the histrogram, from the histogram, I can determine is the object was remembered or not. It will be fast enough.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Twister

That is pretty neat, Farabi! I'm going to start looking more into this now. :P

Farabi

Somebody is trying to steal my laptop, maybe this research attracting them. Maybe.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

joemc

asks to setup speech recognition, than shows a blank window with your title in it.  My laptop's "camera on" light never turns on.  VLC does turn it on.  Do not have much experience with webcams though.

Toshiba R705. Windows 7 64.