Re: PureBasic Interface to OpenCV
Posted: Thu Sep 04, 2014 4:06 pm
Hi, JHP! This really doesn't help me much..... Isn't there something a bit easier in your examples?
Thanks!
Mythros
Thanks!
Mythros
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
If tracking
cvRectangleR(*image, *contours\rect\x, *contours\rect\y, *contours\rect\width, *contours\rect\height, 0, 255, 255, 0, 1, #CV_AA, #Null)
cvPutText(*imgAverage, "ghost detected, wake up...", 20, 40, @font, 0, 255, 255, 0)
cvShowImage(#CV_WINDOW_NAME, *imgAverage)
cvWaitKey(2)
EndIf
Code: Select all
IncludeFile "includes/cv_functions.pbi"
InitSound()
LoadSound(snd, "c:\windows\media\Windows Logon Sound.wav")
Global lpPrevWndFunc
#CV_WINDOW_NAME = "PureBasic Interface to OpenCV"
#CV_DESCRIPTION = "Calculates the weighted sum of the input image and the accumulator creating a silhouette " +
"effect, detecting objects found within one or more zones, from the webcam interface." +
Chr(10) + Chr(10) + "- SPACEBAR: Toggle between foreground/background view." +
Chr(10) + Chr(10) + "- [ 1 - 9 ] KEYS: Toggle zones." +
Chr(10) + Chr(10) + "- [ G ] KEY: Toggle grid." +
Chr(10) + Chr(10) + "- [ T ] KEY: Toggle tracking." +
Chr(10) + Chr(10) + "- [ V ] KEY: Change PIP view."
Structure ZONE_RECT
xBegin.l
xEnd.l
yBegin.l
yEnd.l
status.l
EndStructure
ProcedureC WindowCallback(hWnd, Msg, wParam, lParam)
Shared exitCV.b
Select Msg
Case #WM_COMMAND
Select wParam
Case 10
exitCV = #True
EndSelect
Case #WM_DESTROY
exitCV = #True
EndSelect
ProcedureReturn CallWindowProc_(lpPrevWndFunc, hWnd, Msg, wParam, lParam)
EndProcedure
ProcedureC CvMouseCallback(event, x, y, flags, *param.USER_INFO)
Select event
Case #CV_EVENT_RBUTTONDOWN
DisplayPopupMenu(0, *param\uValue)
EndSelect
EndProcedure
Repeat
nCreate + 1
*capture = cvCreateCameraCapture(0)
Until nCreate = 5 Or *capture
If *capture
cvNamedWindow(#CV_WINDOW_NAME, #CV_WINDOW_AUTOSIZE)
window_handle = cvGetWindowHandle(#CV_WINDOW_NAME)
*window_name = cvGetWindowName(window_handle)
lpPrevWndFunc = SetWindowLongPtr_(window_handle, #GWL_WNDPROC, @WindowCallback())
If CreatePopupImageMenu(0, #PB_Menu_ModernLook)
MenuItem(10, "Exit")
EndIf
hWnd = GetParent_(window_handle)
opencv = LoadImage_(GetModuleHandle_(0), @"icons/opencv.ico", #IMAGE_ICON, 35, 32, #LR_LOADFROMFILE)
SendMessage_(hWnd, #WM_SETICON, 0, opencv)
wStyle = GetWindowLongPtr_(hWnd, #GWL_STYLE)
SetWindowLongPtr_(hWnd, #GWL_STYLE, wStyle & ~(#WS_MAXIMIZEBOX | #WS_MINIMIZEBOX | #WS_SIZEBOX))
cvMoveWindow(#CV_WINDOW_NAME, 20, 20)
ToolTip(window_handle, #CV_DESCRIPTION)
FrameWidth = cvGetCaptureProperty(*capture, #CV_CAP_PROP_FRAME_WIDTH)
FrameHeight = cvGetCaptureProperty(*capture, #CV_CAP_PROP_FRAME_HEIGHT)
Dim guardZone.ZONE_RECT(9)
zoneW = (FrameWidth - 2) / 3
zoneH = (FrameHeight - 2) / 3
For y = 0 To 3 - 1
For x = 0 To 3 - 1
xy + 1
guardZone(xy)\xBegin = zoneW * x + 2
guardZone(xy)\xEnd = guardZone(xy)\xBegin + zoneW - 2
guardZone(xy)\yBegin = zoneH * y + 2
guardZone(xy)\yEnd = guardZone(xy)\yBegin + zoneH - 2
guardZone(xy)\status = 1
Next
Next
INIT_TIME = 50
BG_RATIO.d = 0.02
OBJ_RATIO.d = 0.005
zeta.d = 10
opacity.d = 0.1
PIP = 2
grid = 1
*imgAverage.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
*imgSgm.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
*imgTmp.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
*img_lower.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
*img_upper.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
*imgSilhouette.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 1)
*imgResult.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
iRatio.d = 150 / FrameWidth
iWidth = FrameWidth * iRatio
iHeight = FrameHeight * iRatio
*PIP.IplImage = cvCreateImage(iWidth, iHeight, #IPL_DEPTH_8U, 3)
*image.IplImage
cvSetZero(*imgAverage)
*param.USER_INFO = AllocateMemory(SizeOf(USER_INFO))
*param\uValue = window_handle
cvSetMouseCallback(*window_name, @CvMouseCallback(), *param)
font.CvFont
cvInitFont(@font, #CV_FONT_HERSHEY_COMPLEX | #CV_FONT_ITALIC, 1, 1, #Null, 1, #CV_AA)
cvPutText(*imgAverage, "Initiating Zones...", 20, 40, @font, 0, 255, 255, 0)
cvShowImage(#CV_WINDOW_NAME, *imgAverage)
cvWaitKey(100)
For rtnCount = 0 To INIT_TIME
*image = cvQueryFrame(*capture)
If *image
cvFlip(*image, #Null, 1)
cvAcc(*image, *imgAverage, #Null)
EndIf
Next
cvConvertScale(*imgAverage, *imgAverage, 1.0 / INIT_TIME, 0)
cvSetZero(*imgSgm)
For rtnCount = 0 To INIT_TIME
*image = cvQueryFrame(*capture)
If *image
cvFlip(*image, #Null, 1)
cvConvert(*image, *imgTmp)
cvSub(*imgTmp, *imgAverage, *imgTmp, #Null)
cvPow(*imgTmp, *imgTmp, 2)
cvConvertScale(*imgTmp, *imgTmp, 2, 0)
cvPow(*imgTmp, *imgTmp, 0.5)
cvAcc(*imgTmp, *imgSgm, #Null)
EndIf
Next
cvConvertScale(*imgSgm, *imgSgm, 1.0 / INIT_TIME, 0)
*kernel.IplConvKernel = cvCreateStructuringElementEx(3, 3, 1, 1, #CV_SHAPE_RECT, #Null)
*storage.CvMemStorage = cvCreateMemStorage(0)
*contours.CvContour
Repeat
*image = cvQueryFrame(*capture)
If *image
cvFlip(*image, #Null, 1)
cvConvert(*image, *imgTmp)
cvSub(*imgAverage, *imgSgm, *img_lower, #Null)
cvSubS(*img_lower, zeta, zeta, zeta, zeta, *img_lower, #Null)
cvAdd(*imgAverage, *imgSgm, *img_upper, #Null)
cvAddS(*img_upper, zeta, zeta, zeta, zeta, *img_upper, #Null)
cvInRange(*imgTmp, *img_lower, *img_upper, *imgSilhouette)
cvSub(*imgTmp, *imgAverage, *imgTmp, #Null)
cvPow(*imgTmp, *imgTmp, 2)
cvConvertScale(*imgTmp, *imgTmp, 2, 0)
cvPow(*imgTmp, *imgTmp, 0.5)
cvRunningAvg(*image, *imgAverage, BG_RATIO, *imgSilhouette)
cvRunningAvg(*imgTmp, *imgSgm, BG_RATIO, *imgSilhouette)
cvNot(*imgSilhouette, *imgSilhouette)
cvRunningAvg(*imgTmp, *imgSgm, OBJ_RATIO, *imgSilhouette)
cvErode(*imgSilhouette, *imgSilhouette, *kernel, 1)
cvDilate(*imgSilhouette, *imgSilhouette, *kernel, 2)
cvErode(*imgSilhouette, *imgSilhouette, *kernel, 1)
cvMerge(*imgSilhouette, *imgSilhouette, *imgSilhouette, #Null, *imgResult)
cvClearMemStorage(*storage)
nContours = cvFindContours(*imgSilhouette, *storage, @*contours, SizeOf(CvContour), #CV_RETR_LIST, #CV_CHAIN_APPROX_SIMPLE, 0, 0)
For zoneN = 1 To 9
If grid
cvRectangle(*image, guardZone(zoneN)\xBegin, guardZone(zoneN)\yBegin, guardZone(zoneN)\xEnd, guardZone(zoneN)\yEnd, 255, 0, 0, 0, 1, #CV_AA, #Null)
EndIf
If guardZone(zoneN)\status = 0
*select.IplImage = cvCloneImage(*image)
cvRectangle(*image, guardZone(zoneN)\xBegin, guardZone(zoneN)\yBegin, guardZone(zoneN)\xEnd, guardZone(zoneN)\yEnd, 0, 0, 255, 0, #CV_FILLED, #CV_AA, #Null)
cvAddWeighted(*image, opacity, *select, 1 - opacity, 0, *image)
cvReleaseImage(@*select)
EndIf
Next
If nContours
For rtnCount = 0 To nContours - 1
area.d = cvContourArea(*contours, 0, #CV_WHOLE_SEQ_END_INDEX, 0)
If area >= 500
For zoneN = 1 To 9
If guardZone(zoneN)\xBegin <= *contours\rect\x + *contours\rect\width And
guardZone(zoneN)\xEnd >= *contours\rect\x And
guardZone(zoneN)\yBegin <= *contours\rect\y + *contours\rect\height And
guardZone(zoneN)\yEnd >= *contours\rect\y And guardZone(zoneN)\status
cvDrawContours(*imgResult, *contours, 255, 155, 0, 0, 155, 255, 0, 0, #CV_FILLED, 2, #CV_AA, 0, 0)
*select.IplImage = cvCloneImage(*image)
cvRectangle(*image, guardZone(zoneN)\xBegin, guardZone(zoneN)\yBegin, guardZone(zoneN)\xEnd, guardZone(zoneN)\yEnd, 0, 255, 0, 0, #CV_FILLED, #CV_AA, #Null)
cvAddWeighted(*image, opacity, *select, 1 - opacity, 0, *image)
cvReleaseImage(@*select)
If tracking
cvRectangleR(*image, *contours\rect\x, *contours\rect\y, *contours\rect\width, *contours\rect\height, 0, 255, 255, 0, 1, #CV_AA, #Null)
PlaySound(snd)
EndIf
EndIf
Next
EndIf
*contours = *contours\h_next
Next
Select PIP
Case 0
If foreground
cvResize(*image, *PIP, #CV_INTER_AREA)
cvSetImageROI(*imgResult, 20, 20, iWidth, iHeight)
cvAndS(*imgResult, 0, 0, 0, 0, *imgResult, #Null)
cvAdd(*imgResult, *PIP, *imgResult, #Null)
cvResetImageROI(*imgResult)
cvRectangleR(*imgResult, 19, 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
cvShowImage(#CV_WINDOW_NAME, *imgResult)
Else
cvResize(*imgResult, *PIP, #CV_INTER_AREA)
cvSetImageROI(*image, 20, 20, iWidth, iHeight)
cvAndS(*image, 0, 0, 0, 0, *image, #Null)
cvAdd(*image, *PIP, *image, #Null)
cvResetImageROI(*image)
cvRectangleR(*image, 19, 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
cvShowImage(#CV_WINDOW_NAME, *image)
EndIf
Case 1
If foreground
cvResize(*image, *PIP, #CV_INTER_AREA)
cvSetImageROI(*imgResult, *image\width - (150 + 20), 20, iWidth, iHeight)
cvAndS(*imgResult, 0, 0, 0, 0, *imgResult, #Null)
cvAdd(*imgResult, *PIP, *imgResult, #Null)
cvResetImageROI(*imgResult)
cvRectangleR(*imgResult, *image\width - (150 + 21), 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
cvShowImage(#CV_WINDOW_NAME, *imgResult)
Else
cvResize(*imgResult, *PIP, #CV_INTER_AREA)
cvSetImageROI(*image, *image\width - (150 + 20), 20, iWidth, iHeight)
cvAndS(*image, 0, 0, 0, 0, *image, #Null)
cvAdd(*image, *PIP, *image, #Null)
cvResetImageROI(*image)
cvRectangleR(*image, *image\width - (150 + 21), 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
cvShowImage(#CV_WINDOW_NAME, *image)
EndIf
Case 2
If foreground : cvShowImage(#CV_WINDOW_NAME, *imgResult) : Else : cvShowImage(#CV_WINDOW_NAME, *image) : EndIf
EndSelect
keyPressed = cvWaitKey(10)
Select keyPressed
Case 32
foreground ! 1
Case 49 To 57
guardZone(keyPressed - 48)\status ! 1
Case 71, 103
grid ! 1
Case 84, 116
tracking ! 1
Case 86, 118
PIP = (PIP + 1) % 3
EndSelect
EndIf
Else
Break
EndIf
Until keyPressed = 27 Or exitCV
FreeSound(snd)
FreeMemory(*param)
cvReleaseMemStorage(@*storage)
cvReleaseStructuringElement(@*kernel)
cvReleaseImage(@*PIP)
cvReleaseImage(@*imgResult)
cvReleaseImage(@*imgSilhouette)
cvReleaseImage(@*img_upper)
cvReleaseImage(@*img_lower)
cvReleaseImage(@*imgTmp)
cvReleaseImage(@*imgSgm)
cvReleaseImage(@*imgAverage)
cvDestroyWindow(#CV_WINDOW_NAME)
cvReleaseCapture(@*capture)
EndIf
I tried running it quickly by just copy paste to a new file in my OpenCV folder from which I run all JHPJHP's examples. However, when I try running it, I get error about missing opencv_core249.dll? All other examples any my own code works properly, what am I missing?applePi wrote:yes it works okay with sound, let the webcam directed to static scene, then choose zone by pressing 5 as an example, then press T. now wave a pencil in front the cam and the speaker will play "Windows Logon Sound.wav"
allow me JHPJHP to repost your example but adding the sound init and playsound for those who want quick test
note that you should wave the pencil once every time else the sound will continue for longer time. or use a flag to play it once such as
Code: Select all
If InitSound() = 0
MessageRequester("Error", "Sound system is not available", 0)
End
EndIf
SoundFileName$ = OpenFileRequester("Choose a .wav file", "", "Wave files|*.wav",0)
If SoundFileName$
If LoadSound(0, SoundFileName$)
For i=1 To 5
PlaySound(0 )
Sleep_(300)
Next
MessageRequester("Sound", "Playing the sound (loop)..."+#LF$+"Click to quit..", 0)
Else
MessageRequester("Error", "Can't load the sound.", 0)
EndIf
EndIf
End