Webcam motion detector thingy

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Webcam motion detector thingy

Post by dobro »

@Johakim

updated for V 4.51 ;)


; codé par by JLC
; Modifié par Dobro pour le Purebasic 4.51
; et utilisation des prototypes
; ce prg repere un mouvement et signale a l'aide de carré vert

;EnableExplicit

Prototype.l appel_fonction1(titr$, type,type1,type2, type3, type4, id ,type5)

;{ CAP constants
#WM_CAP_START = #WM_USER
#WM_CAP_DRIVER_CONNECT = #WM_CAP_START + 10
#WM_CAP_DRIVER_DISCONNECT = #WM_CAP_START + 11
#WM_CAP_DRIVER_GET_CAPS = #WM_CAP_START + 14
#WM_CAP_EDIT_COPY = #WM_CAP_START + 30
#WM_CAP_SET_PREVIEW = #WM_CAP_START + 50
#WM_CAP_SET_PREVIEWRATE = #WM_CAP_START + 52
#WM_CAP_STOP = #WM_CAP_START + 68
#WM_CAP_SET_SCALE = #WM_CAP_START + 53
#WM_CAP_START = #WM_USER
#WM_CAP_DLG_VIDEOSOURCE = #WM_CAP_START + 42
;}
#Main =0
#camWidth = 640
#camHeight = 480
#w16 = 40
#h16 = 30

Structure avg
            r.l
            g.l
            b.l
EndStructure

Global hWndC
Define Ccam_lib1, *capAddress

Procedure rotate90(*MemoryTarget,W,H,size)
             Protected X,Y,Target, Origin, *MemoryOrigin = allocatememory (size)
             copymemory (*MemoryTarget,*MemoryOrigin,size)
             For Y = 0 To H - 1
                         For X = 0 To W - 1
                                    Origin = (Y * W + X) << 2
                                    Target = ((H - Y - 1) + (X * H)) << 2
                                     pokel (*MemoryTarget + Target, peekl (*MemoryOrigin + Origin))
                         Next
             Next
             freememory (*MemoryOrigin)
EndProcedure



Procedure greyScale(*Memory, memorysize )
             Protected Counter, Color
             For Counter = 0 To memorysize - 1 Step 4
                        Color = peekl (*Memory + Counter)
                        Color = ( red (Color) + green (Color) + blue (Color)) / 3
                         pokel (*Memory + Counter, rgb (Color, Color, Color))
             Next
EndProcedure



Procedure captureImage(dummy)
             Protected image, bmp.BITMAP, imageid , imageSize, *bits, pos
             Protected Dim prev.avg( #w16 , #h16 )
             Protected X,Y,yy,xx, avg.avg, Color, diff
            
             Repeat
                         SendMessage_ (hWndC, #WM_CAP_EDIT_COPY ,0,0)
                        image = getclipboardimage ( #PB_Any ,32)
                         If image<>0
                                     imageid = imageid (image) ;maybe flip it first
                                    
                                     GetObject_ ( imageid , sizeof (BITMAP),@bmp.BITMAP)
                                    imageSize = bmp\bmWidthBytes * bmp\bmHeight
                                    *bits = bmp\bmBits
                                    
                                     startdrawing ( windowoutput (0))
                                                 drawimage ( imageid ,0,0) ;____________________________________________ici
                                                 drawingmode ( #PB_2DDrawing_Outlined )
                                                
                                                 For X=0 To #w16 -1
                                                             For Y=0 To #h16 -1
                                                                        avg\r = 0: avg\g=0: avg\b=0
                                                                         For xx=X*16 To X*16+16-1
                                                                                     For yy=Y*16 To Y*16+16-1
                                                                                                pos = (yy * bmp\bmWidth + xx) << 2
                                                                                                Color = peekl (*bits+pos)
                                                                                                avg\r + red (Color)
                                                                                                avg\g + green (Color)
                                                                                                avg\b + blue (Color)
                                                                                                 ;Plot(xx,479-yy,color)
                                                                                     Next
                                                                         Next
                                                                        avg\r = avg\r / 256
                                                                        avg\g = avg\g / 256
                                                                        avg\b = avg\b / 256
                                                                 ; Box(X*16,Y*16,16,16,RGB(avg\r,avg\g,avg\b))
                                                                        diff = 0
                                                                        diff + abs (prev(X,Y)\r - avg\r)
                                                                        diff + abs (prev(X,Y)\g - avg\g)
                                                                        diff + abs (prev(X,Y)\b - avg\b)
                                                                         If diff > 40
                                                                                     box (X*16,464-Y*16,15,15, #Green )
                                                                             ; Circle(X*16+8,464-Y*16+8,8,#Green) ;Or circleIfif you want
                                                                         EndIf
                                                                        prev(X,Y)\r = avg\r
                                                                        prev(X,Y)\g = avg\g
                                                                        prev(X,Y)\b = avg\b
                                                             Next
                                                 Next
                                                
                                     ; DrawImage(imageID,0,0)
                                     stopdrawing ()
                                     freeimage (image)
                         EndIf
                        
                         ;Delay(2000)
             ForEver
EndProcedure



If openwindow ( #Main ,0,0,640,480, "Webcam Motion Detector by JLC" , #PB_Window_ScreenCentered|#PB_Window_SystemMenu )
            Ccam_lib1 = openlibrary ( #PB_Any , "avicap32.dll" )
             If Ccam_lib1
                        appel_fonction1.appel_fonction1 = getfunction (Ccam_lib1, "capCreateCaptureWindowA" )
                        hWndC= appel_fonction1( "My Capture Window" , #WS_CHILD | #WS_VISIBLE , 0,0, 1 , 1, windowid ( #Main ),0)
                         SendMessage_ (hWndC, #WM_CAP_DRIVER_CONNECT , 0, 0)
                         SendMessage_ (hWndC, #WM_CAP_SET_OVERLAY , #True , 0)
                         SendMessage_ (hWndC, #WM_CAP_SET_PREVIEW , #True , 0)
                         SendMessage_ (hWndC, #WM_CAP_SET_PREVIEWRATE , 1, 0)
             EndIf
EndIf


createthread (@captureImage(),0)

Repeat
             delay (10)
Until waitwindowevent () = #PB_Event_CloseWindow

SendMessage_ (hWndC, #WM_CAP_STOP ,0,0)
SendMessage_ (hWndC, #WM_CAP_DRIVER_DISCONNECT ,0,0)
DestroyWindow_ (hWndC)
closelibrary (Ccam_lib1)

Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply