PureBasic Interface to OpenCV

Developed or developing a new product in PureBasic? Tell the world about it.
AAT
Enthusiast
Enthusiast
Posts: 259
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi, JHPJHP!

I tested your last pack and found an error in the example cv_minarea_2.pb:
PureBasic - Linker error
POLINK: error: Unresolved external symbol '_cvMinEnclosingTriangle'
POLINK: fatal error: 1 unresolved external(s).

Windows XP 32 bit.

Good luck!
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi infratec,

Thank you very much, that means a lot coming from you.

----------------------------------------------------

Hi AAT,

As always you're my second set of eyes. Thank you for all your support :: starting from the first example.

----------------------------------------------------

Updated:
- PureBasic Interface to OpenCV 3.0 Gold (32bit / 64bit)
-- fix the problem reported by AAT in the previous post

In my hast to release a clean working set of binaries with the least amount of changes, I neglected to apply my initial update.

NB*: If there's a requirement for the updated [ OpenCV 3.0 Gold ] individual binaries I can post a link.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
AAT
Enthusiast
Enthusiast
Posts: 259
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi, JHPJHP!

cv_minarea_2.pb - it's OK now.

Many thanks and good luck!
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: PureBasic Interface to OpenCV

Post by marc_256 »

Hello,

I hoop to find some time this vacation to do some software for my little robot.

One of the challenges in the robot club are,
to find 5 cans (cola, red ones) and to bring them to one central point on the robot zone.

So,
the robot is moving around and the cans are static on the ground.
is this possible to detect cans with OpenCV,
even as the image of the cans are changing during the robot vision.

What I want to say is that the front view of the cans and the top view are very different.
top views are circular.

I already made some 3D simulation program,
now I only need to integrate the OpenCV camera detections.

Image

thanks,
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi marc_256,

I think the example closest to what your looking for is le_nearest_neighbor.pb, but you may want to combine techniques:
- cv_cam_track_color.pb: modify the script to test for matching colors
- cv_find_shapes.pb: test for shapes with a certain number of points
- cv_cam_haar_training_1.pb: this example is geared towards faces, but with a few small changes a file can be created to match your criteria

NB*: A general understanding of the process would help chances for returning accurate results; the reference file haarcascade.txt is a good start.

The following examples may also provide some benefit:
- cv_norm.pb
- cv_template.pb

Good luck.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: PureBasic Interface to OpenCV

Post by marc_256 »

hi JHPJHP,

thanks for the tips,

marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: PureBasic Interface to OpenCV

Post by dobro »

Hello ,

in the example "cv_cam_track_color.pb"
there is a memory leak apparently,
when checking the gestionaire of taches (Ctrl+Alt+Del), the memory occupied, increases continuously

how to fix this problem? :)
Last edited by dobro on Mon Jul 13, 2015 12:55 pm, edited 1 time in total.
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: PureBasic Interface to OpenCV

Post by dobro »

fixed here to avoid leaking memory :)

Code: Select all

IncludeFile "includes/cv_functions.pbi"

Global lpPrevWndFunc, *imgTracking.IplImage, lastX, lastY, nCursor

#CV_WINDOW_NAME = "PureBasic Interface to OpenCV"
#CV_DESCRIPTION = "Tracks red objects demonstrated by drawing a line that traces its location." + Chr(10) + Chr(10) +
"- SPACEBAR: Toggle mouse tracking." + Chr(10) + Chr(10) +
"- ENTER: Clear the traced line."

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.l, y.l, flags, *param.USER_INFO)
	Select event
		Case #CV_EVENT_RBUTTONDOWN
		DisplayPopupMenu(0, *param\uValue)
	EndSelect
EndProcedure

ProcedureC GetThresholdedImage(*imgHSV.IplImage)
	*imgThresh.IplImage = cvCreateImage(*imgHSV\width, *imgHSV\height, #IPL_DEPTH_8U, 1)
	cvInRangeS(*imgHSV, 160, 140, 40, 0, 179, 255, 255, 0, *imgThresh)
	ProcedureReturn *imgThresh
EndProcedure

ProcedureC TrackObject(*imgThresh.IplImage)
	moments.CvMoments
	cvMoments(*imgThresh, @moments, 1)
	moment10.d = moments\m10
	moment01.d = moments\m01
	area.d = moments\m00
	
	If area > 1000
		posX = moment10 / area
		posY = moment01 / area
		
		If lastX >= 0 And lastY >= 0 And posX >= 0 And posY >= 0
			cvLine(*imgTracking, posX, posY, lastX, lastY, 0, 0, 255, 0, 4, #CV_AA, #Null)
			
			If nCursor : SetCursorPos_(posX + 25, posY + 45) : EndIf
			
		EndIf
		lastX = posX
		lastY = posY
	EndIf
EndProcedure

Repeat
	nCreate + 1
	*capture.CvCapture = cvCreateCameraCapture(#CV_CAP_ANY)
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)
	*imgTracking = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
	cvSetZero(*imgTracking)
	lastX = -1
	lastY = -1
	*imgHSV.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
	*imgThresh.IplImage
	*image.IplImage
	*param.USER_INFO = AllocateMemory(SizeOf(USER_INFO))
	*param\uValue = window_handle
	cvSetMouseCallback(*window_name, @CvMouseCallback(), *param)
	
	Repeat
		*image = cvQueryFrame(*capture)
		
		If *image
			cvFlip(*image, #Null, 1)
			cvSmooth(*image, *image, #CV_GAUSSIAN, 3, 3, 0, 0)
			cvCvtColor(*image, *imgHSV, #CV_BGR2HSV, 1)
			*imgThresh = GetThresholdedImage(*imgHSV)
			cvSmooth(*imgThresh, *imgThresh, #CV_GAUSSIAN, 3, 3, 0, 0)
			TrackObject(*imgThresh)
			cvAdd(*image, *imgTracking, *image, #Null)
			cvShowImage(#CV_WINDOW_NAME, *image)
			keyPressed = cvWaitKey(10)
			cvReleaseImage(@*imgThresh) ; <<<<<<<<<<<<<<<<<fixed here to avoid leaking memory  (By Dobro )
			
			Select keyPressed
				Case 13
				cvSetZero(*imgTracking)
				lastX = -1
				lastY = -1
				Case 32
				nCursor ! 1
			EndSelect
		EndIf
	Until keyPressed = 27 Or exitCV
	FreeMemory(*param)
	cvReleaseImage(@*imgThresh)
	cvReleaseImage(@*imgHSV)
	cvReleaseImage(@*imgTracking)
	cvDestroyAllWindows()
	cvReleaseCapture(@*capture)
Else
	MessageBox_(0, "Unable to connect to a webcam - operation cancelled.", #CV_WINDOW_NAME, #MB_ICONERROR)
EndIf
; IDE Options = PureBasic 5.31 (Windows - x64)
; CursorPosition = 1
; Folding = -
; EnableXP
; DisableDebugger
; CurrentDirectory = binaries\





; Epb
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
AAT
Enthusiast
Enthusiast
Posts: 259
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi, dobro.

I think, that the problem is in the procedure GetThresholdedImage: мemory is allocated for each procedure call and is not released
*imgThresh.IplImage = cvCreateImage...

Look at this code with little changes marked by ";!!!", I think it will fix a memory leakage.

Code: Select all

  Repeat
    *image = cvQueryFrame(*capture)

    If *image
      cvFlip(*image, #Null, 1)
      cvSmooth(*image, *image, #CV_GAUSSIAN, 3, 3, 0, 0)
      cvCvtColor(*image, *imgHSV, #CV_BGR2HSV, 1)
;      *imgThresh = GetThresholdedImage(*imgHSV)                                             ;!!!!       
      *imgThresh.IplImage = cvCreateImage(*imgHSV\width, *imgHSV\height, #IPL_DEPTH_8U, 1)  ;!!!!        
      cvInRangeS(*imgHSV, 160, 140, 40, 0, 179, 255, 255, 0, *imgThresh)                    ;!!!!
      cvSmooth(*imgThresh, *imgThresh, #CV_GAUSSIAN, 3, 3, 0, 0)
      TrackObject(*imgThresh)
      cvAdd(*image, *imgTracking, *image, #Null)
      cvShowImage(#CV_WINDOW_NAME, *image)          
      cvReleaseImage(@*imgThresh)                                                           ;!!!!   
      keyPressed = cvWaitKey(10)

      Select keyPressed
        Case 13
          cvSetZero(*imgTracking)
          lastX = -1
          lastY = -1
        Case 32
          nCursor ! 1
      EndSelect
    EndIf
  Until keyPressed = 27 Or exitCV
  FreeMemory(*param)
;  cvReleaseImage(@*imgThresh)                                                            ;!!!! 
  cvReleaseImage(@*imgHSV)
  cvReleaseImage(@*imgTracking)
Good luck!

P.S. Dobro, i was late :D
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: PureBasic Interface to OpenCV

Post by dobro »

yes, in my code above, I had finally found the solution

I added "

cvReleaseImage (@ * imgThresh); <<<<<<<<<<<<<<<<< fixed here to Avoid leaking memory (By Dobro) "

Code: Select all

 cvFlip(*image, #Null, 1)
         cvSmooth(*image, *image, #CV_GAUSSIAN, 3, 3, 0, 0)
         cvCvtColor(*image, *imgHSV, #CV_BGR2HSV, 1)
         *imgThresh = GetThresholdedImage(*imgHSV)
         cvSmooth(*imgThresh, *imgThresh, #CV_GAUSSIAN, 3, 3, 0, 0)
         TrackObject(*imgThresh)
         cvAdd(*image, *imgTracking, *image, #Null)
         cvShowImage(#CV_WINDOW_NAME, *image)
         keyPressed = cvWaitKey(10)
         cvReleaseImage(@*imgThresh) ; <<<<<<<<<<<<<<<<<fixed here to avoid leaking memory  (By Dobro )


Thanks :)

ps: no problems :D
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi dobro, AAT,

Thank you for error-checking the example: cv_cam_track_color.pb, pointing out the problem and fix.

NB*: The various packages have been updated.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: PureBasic Interface to OpenCV

Post by dobro »

@JHPJHP :
Essayez avec cette orthographe : Merci à toi, pour ton travail
Thank you to you, for your work :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
minimy
Enthusiast
Enthusiast
Posts: 552
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: PureBasic Interface to OpenCV

Post by minimy »

Hi! I see in examples how change resolution.
But how can set fps to capture card, is this posible?

Thanks!
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi minimy,

If you need to set the FPS while recording webcam frames or copying movie frames, see the examples:
- cv_cam_writeframe_1.pb, cv_cam_writeframe_2.pb, cv_mov_writeframe.pb

/references/manual.pdf:
CvVideoWriter* cvCreateVideoWriter(const char* filename, int fourcc, double fps, CvSize frame_size, int is_color=1 )
Otherwise the following functions can be used with the Constant: #CV_CAP_PROP_FPS
- cvGetCaptureProperty, cvSetCaptureProperty

/references/manual.pdf:
double cvGetCaptureProperty(CvCapture* capture, int property_id)

int cvSetCaptureProperty(CvCapture* capture, int property_id, double value)

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 552
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: PureBasic Interface to OpenCV

Post by minimy »

Yes! a lot of thanks!!
Work perfect, thank again!
If translation=Error: reply="Sorry, Im Spanish": Endif
Locked