PureBasic Interface to OpenCV

Developed or developing a new product in PureBasic? Tell the world about it.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

JHPJHP wrote:Converted another four examples
Thanks for the new examples. :)
JHPJHP wrote:NB*: I hope this thread finds OSX users that can benefit from the work being done here.
That would indeed be nice.
Or someone who is interested in OpenCV on Linux. The implementations of OpenCV on OSX and Linux are quite similar.
The OSX examples and imports can be helpful for them as well.
Windows (x64)
Raspberry Pi OS (Arm64)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi wilbert,

You're very much welcome.

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

All the examples have been updated for better stability, including four additional examples.
- cv_convexhull_1.pb: finds the convex hull of a point set
- cv_setimageROI.pb: simulates zooming a section of a resized image by loading the Region Of Interest (ROI) for a given rectangle from the original image
- cv_stereobm.pb: computes disparity using the BM (Block Matching) algorithm for a rectified stereo pair of images
- cv_transparent_1.pb: using the watershed algorithm, an object can be extracted and saved to a transparent PNG file

Instructions for cv_transparent_1.pb
- draw a connected outline around the inside of Lena (accuracy not needed)
- draw a vertical line to the left of Lena
- draw a vertical line to the right of Lena
- draw a horizontal line above Lena
- press the Spacebar to apply the marked regions
- double-click the center of Lena to extract her from the image
- press the Spacebar if you want to modify the selected area
-- connecting new lines to previous lines will expand the marked region
- save the image to a PNG (transparent) file
- press enter at anytime to reset the image

Image

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

There was a problem with one of the new examples accessing the Double-Clicked event...

The following information was provided by wilbert:
wilbert wrote:If you look at the cvMouseEvent function in the window_cocoa.mm file, You will see OpenCV simply doesn't implement double click on OSX.

OSX has no special double click event. Instead it counts the amount of clicks that are in between a double click interval.
That way double clicks can be detected but also triple or more.

You can set a global value for NSApp since it doesn't change while the application is running

Code: Select all

Global NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
Now in the mouse callback, try this

Code: Select all

Debug CocoaMessage(0, CocoaMessage(0, NSApp, "currentEvent"), "clickCount")
You'll see it shows the number of mouse clicks.
Last edited by JHPJHP on Mon May 30, 2016 8:56 am, edited 3 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

JHPJHP wrote:All the examples have been updated for better stability, including four additional examples.
32 examples already for OSX :)
Windows (x64)
Raspberry Pi OS (Arm64)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Updated the OSX interface with another 4 examples converted from the Windows interface.
- cv_DCT.pb: performs a discrete cosine transform of a 1D array, first displaying its power spectrum then incrementally reducing noise
- cv_DFT.pb: performs a discrete Fourier transform of a 1D floating-point array, displaying its power spectrum
- cv_HOG.pb: Histogram of Oriented Gradients (HOG) is used to detect objects
- cv_LBP.pb: Local Binary Pattern: An efficient texture operator thresholding the neighborhood of each pixel

NB*: Update also includes some minor changes to various examples, but nothing worth documenting.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

JHPJHP wrote:cv_HOG.pb: Histogram of Oriented Gradients (HOG) is used to detect objects
Looks a bit like a weather map, indicating wind directions. :)
Windows (x64)
Raspberry Pi OS (Arm64)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi wilbert,

Your expertise is needed again, I'm having trouble with another Function...

Two more examples have been converted.
- cv_CAPTCHA.pb: CAPTCHA: Completely Automated Public Turing test to tell Computers and Humans Apart
- cv_inpaint.pb: restores the selected region in an image using the region neighborhood

The example cv_CAPTCHA.pb mostly works, but I'm having trouble with the Function cv2DRotationMatrix in the Procedure Rotate.
- pressing the Spacebar builds a new CAPTCHA: randomly some of the characters are missing; I believe the center parameter is defined incorrectly

The example cv_inpaint.pb offers an interesting solution for fixing images needing repair due to scratches.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

JHPJHP wrote:I'm having trouble with the Function cv2DRotationMatrix in the Procedure Rotate.
I'm not 100% sure because I don't know exactly how it should look but you can try this.

Import

Code: Select all

cv2DRotationMatrix(center.d, angle.d, scale.d, *map_matrix)
Usage

Code: Select all

  c.d
  PokeF(@c, center\x)
  PokeF(@c + 4, center\y)
  cv2DRotationMatrix(c, angle, 1, *rotate)
Windows (x64)
Raspberry Pi OS (Arm64)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi wilbert,

That did it, thank you :!:

How did you know to use a decimal when the Function called for a Structure of FLOATs :?:

Updated the OSX interface with the fix posted by wilbert, including an additional two examples.
- cv_lomo.pb: simulates the LOMO (Leningrad Optical Mechanical Association) camera lens effect
- cv_vignette.pb: applies the Vignette effect, the process by which there is loss in clarity towards the corners and sides of an image
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

Hi JHPJHP,
JHPJHP wrote:How did you know to use a decimal when the Function called for a Structure of FLOATs :?:
It was an experiment. A double (.d) is passed to a xmm register if there is one available.
A structure of two floats (.f) is combined into a single xmm register.
If this workaround hadn't worked, it would have been required to use asm instructions to load the xmm registers manually.
Windows (x64)
Raspberry Pi OS (Arm64)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi wilbert,

Thank you again for your support and the invaluable information you provided; it's always nice to learn something new.

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

Updated the OSX interface:
- cv_canny.pb: finds edges in an image using the Canny86 algorithm
- cv_kalman_1.pb: the Kalman filter is an algorithm which operates recursively on streams of noisy input data
- cv_kalman_2.pb: tracks a rotating point at a constant rotation speed where both state and measurement vectors are a 1D point angle
- cv_kmeans_1.pb: image segmentation by k-means clustering and color reduction
- cv_kmeans_2.pb: generates an image with random points grouped by a random number of cluster centers
- cv_rotate_1.pb: calculates an affine matrix of 2D rotation, applying the transformation to an image
- cv_rotate_2.pb: retrieves pixel quadrangle with sub-pixel accuracy, applying 2D rotation to an image
- cv_sobel.pb: calculates the first, second, third, or mixed image derivatives using an extended Sobel operator
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

JHPJHP wrote:Updated the OSX interface
A lot of algorithms I never heard of before :shock: :)
The examples are working fine.
JHPJHP wrote:it's always nice to learn something new.
I agree. That's what I also like.
Windows (x64)
Raspberry Pi OS (Arm64)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi wilbert,

Thank you for testing the examples.

Check out this video on the Kalman filter. Notice how tracking is not interrupted by occlusions, due to the predictive measurements returned from the Kalman algorthium.
- https://www.youtube.com/watch?v=sG-h5ONsj9s

There are another couple Functions in a new example I'm having trouble with, can you please take a look.
- I believe the problem to be with the [ pt ] parameter, but trying your previous solutions I was unsuccessful

Example: le_delaunay_voronoi.pb
- Procedure: LocatePoint
-- Function: cvSubdiv2DLocate
- Procedure: RunDelaunay
-- Function : cvSubdivDelaunay2DInsert

NB*: The Functions are currently REM'd out.

Thank you again.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

JHPJHP wrote:I believe the problem to be with the [ pt ] parameter, but trying your solutions I was unsuccessful
Did you try with the double ?

Import

Code: Select all

  cvInitSubdivDelaunay2D(*subdiv, rect_xy, rect_wh)

  cvSubdiv2DLocate(*subdiv, pt.d, *edge, *vertex)
  cvSubdivDelaunay2DInsert(*subdiv, pt.d)
Usage

Code: Select all

  PokeL(@xy, x) : PokeL(@xy + 4, y) : PokeL(@wh, width) : PokeL(@wh + 4, height)
  cvInitSubdivDelaunay2D(*subdiv, xy, wh)

  PokeF(@pt.d, x) : PokeF(@pt + 4, y)
  cvSubdiv2DLocate(*subdiv, pt, @edge, @*vertex)

  PokeF(@pt.d, x) : PokeF(@pt + 4, y)
  cvSubdivDelaunay2DInsert(*subdiv, pt)
Windows (x64)
Raspberry Pi OS (Arm64)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi wilbert,

That was the first thing I tried, but I didn't notice the other Function ( cvInitSubdivDelaunay2D ) that needed converting as well.

This is what happens when I spend a day looking at a computer screen, to come home and spend the evening converting eight examples, and think I have a couple more in me.

I think it may be time to take a break, at least for a couple weeks.

Thanks for all your help.

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

Updated OSX interface:
- le_delaunay_1.pb: predefined construction of delaunay triangulation
- le_delaunay_voronoi.pb: iterative construction of delaunay triangulation and voronoi tesselation
Last edited by JHPJHP on Wed Jun 01, 2016 9:23 pm, edited 3 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PureBasic Interface to OpenCV

Post by wilbert »

JHPJHP wrote:I think it may be time to take a break, at least for a couple weeks.
You did already convert a lot of examples. No need to convert all of them.
What you did already is a great help if someone wants to use OpenCV on OSX.
Windows (x64)
Raspberry Pi OS (Arm64)
Locked