PureBasic Interface to OpenCV

Developed or developing a new product in PureBasic? Tell the world about it.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Hi AAT,

Very nice addition...

You might have missed a previous update where I fixed a problem that you pointed out earlier. The following prevents 1 channel images from crashing; I will shortly incorporate the new example into the fold.

Code: Select all

*resize = cvCreateImage(iWidth, iHeight, #IPL_DEPTH_8U, *image\nChannels)
...
If *image\nChannels = 1
  *gray = cvCloneImage(*image)
Else
  *gray = cvCreateImage(*image\width, *image\height, #IPL_DEPTH_8U, 1)
  cvCvtColor(*image, *gray, #CV_BGR2GRAY, 1)
EndIf
-------------------------------------------------------

A new function and a new example have been added (cv_contours_1b.pb) - both contributed by AAT.
- the example is an extension of cv_contours_1a.pb (formally cv_contours.pb)
-- a filter has been applied so that "noise" is excluded
This example scans an image for contours building each contour into an array of points. It then applies the data to a blank matrices using predefined filters, creating a "drawn" version of the image (filled or outlined).
Last edited by JHPJHP on Sat Nov 30, 2013 1:36 am, edited 3 times in total.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: OpenCV (v2)

Post by AAT »

JHPJHP wrote:The following prevents 1 channel images from crashing;
I have tested monocrome images with 1 bit, 4 bit, 8 bit per pixel (saved with MS Paint from colour 24 bit image).
There is no difference: 1 bit - crash, others - work fine.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Hi AAT,

I'm using Channel to describe the success / failure of the image because that's where it failed in the code. When I created / cloned an image based on the Structure's nChannels return value my tests passed. Without the update some images caused a program error (Windows XP) or crashed the program (Windows 7).

NB*: I don't think bit-depth is the problem as indicated by the included files.
- [ with change.pb ] & [ without change.pb ]: folder \Pass\ (image files work)
- [ with change.pb ]: folder \Fail\ (image files work)
- [ without change.pb ]: folder \Fail\ (image files don't work)

Thanks!
Last edited by JHPJHP on Sat Nov 30, 2013 10:06 pm, edited 1 time in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

New Functions and a new example (cv_contours_1c.pb) have been added.
- adds a Bounding Rectangle to all the points returned from the cvFindContours Function

There is still an issue with using cvMinAreaRect2, but the CvContour Structure has a CvRect parameter readily available.
- the limitation is that there is no Angle parameter compared with CvBox2D which allows for a Rotated-Rectangle
-- a workaround would only require a little math and the cvPolyLine Function (see example: cv_shapes.pb)

Cheers!
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: OpenCV (v2)

Post by AAT »

Hi, JHPJHP
JHPJHP wrote:Can you test the following: https://www.dropbox.com/s/lgw4uohnzleq6oi/Test.zip
I have tested your programm "with change.pb", it works fine.
I found a copy-paste error in my code, so it works too.
JHPJHP wrote:There is still an issue with using cvMinAreaRect2, but the CvContour Structure has a CvRect parameter readily available...
Thank you, but CvRect is bounding rectangle.
I need a rotated rectangle (center & angle parameters) i.e in cvMinAreaRect2 function...
Perhaps, moments can help me.

Addition:
JHPJHP, you can add more functions and structure.

Code: Select all

Structure CvMoments
  m00.d
  m10.d
  m01.d
  m20.d
  m11.d
  m02.d
  m30.d
  m21.d
  m12.d
  m03.d
  mu20.d
  mu11.d
  mu02.d
  mu30.d
  mu21.d
  mu12.d
  mu03.d
  inv_sqrt_m00.d
EndStructure

ImportC "opencv_core247.lib"
  cvAnd(*src1Array, *src2Array, *dstArray, *maskArray)
  cvXor(*src1Array, *src2Array, *dstArray, *maskArray)
  cvNot(*srcArr, *dstArr)
EndImport 

ImportC "opencv_imgproc247.lib"
   cvMoments(*arr, *moments, binary.i)
EndImport 
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Hi AAT,

Sorry, it must have been the way I worded it, I didn't need you to test it for me - I knew it worked, I only made it available as a test / comparison for you to fix your code, demonstrating my results.
I have tested your programm "with change.pb", it works fine.
Either I misunderstood your requirements, or you missed this in my last post:
a workaround would only require a little math and the cvPolyLine Function
- I'll update the package later with a new example, including the Structure & Functions from your post (thanks!)
- you said rotated, not rotated and bound (too include bounded requires a little more math)
- I noticed a small caveat - if the lines are aliasing, use the context menu to open the image a second time (something still too figure out)

Code: Select all

Dim pts.CvPoint(4)
npts = ArraySize(pts())
angle.d = 0.45

For rtnCount = 1 To nContours
  area.d = cvContourArea(*contours, 0, $3fffffff, 0)

  If area >= 35
    cvDrawContours(*contour, *contours, 255, 155, 0, 0, 155, 255, 0, 0, -1, 1, #CV_AA, 0, 0)
    x = Round(*contours\rect\x + *contours\rect\width / 2, #PB_Round_Up)
    y = Round(*contours\rect\y + *contours\rect\height / 2, #PB_Round_Up)    width = *contours\rect\width
    height = *contours\rect\height
    pts(0)\x = Round(x + (width / 2) * Cos(angle) - (height / 2) * Sin(angle), #PB_Round_Nearest)
    pts(0)\y = Round(y + (height / 2) * Cos(angle) + (width / 2) * Sin(angle), #PB_Round_Nearest)
    pts(1)\x = Round(x - (width / 2) * Cos(angle) - (height / 2) * Sin(angle), #PB_Round_Nearest)
    pts(1)\y = Round(y + (height / 2) * Cos(angle) - (width / 2) * Sin(angle), #PB_Round_Nearest)
    pts(2)\x = Round(x - (width / 2) * Cos(angle) + (height / 2) * Sin(angle), #PB_Round_Nearest)
    pts(2)\y = Round(y - (height / 2) * Cos(angle) - (width / 2) * Sin(angle), #PB_Round_Nearest)
    pts(3)\x = Round(x + (width / 2) * Cos(angle) + (height / 2) * Sin(angle), #PB_Round_Nearest)
    pts(3)\y = Round(y - (height / 2) * Cos(angle) + (width / 2) * Sin(angle), #PB_Round_Nearest)
    cvPolyLine(*contour, pts(), @npts, 1, #True, 0, 0, 255, 0, 1, #CV_AA, #Null)
    cvRectangleR(*contour, *contours\rect\x, *contours\rect\y, *contours\rect\width, *contours\rect\height, 0, 255, 255, 0, 1, #CV_AA, #Null)
  EndIf
  *contours = *contours\h_next
Next
Last edited by JHPJHP on Sun Dec 01, 2013 4:40 am, edited 3 times in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Added Constants, Structures, and Functions (jointly contributed by AAT).

Combined examples cv_contours_1a.pb & cv_contours_1b.pb, renamed example cv_contours_1c.pb to cv_contours_1b.pb, added a new example: cv_contours_1c.pb.
- the Contour Filter contributed by AAT now incorporated into all Contour examples.

The new example draws a Rotated-Rectangle (by degree) around the contour array of an image.
- see previous post for more information

Cheers!
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Added Constants, Structures, Macros, Functions (jointly contributed by AAT)

Renamed / modified example: cv_contours_1c.pb to cv_contours_1x.pb (experimental / jointly contributed by AAT).

2 new examples (contributed by AAT)
- cv_contours_1c.pb: applies a bounding circle around the contour array of an image
- cv_sequence.pb: creates a sequence array of circles and applies a bounding circle around them
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: OpenCV (v2)

Post by Little John »

Many thanks for your ongoing work!
pjay
Enthusiast
Enthusiast
Posts: 163
Joined: Thu Mar 30, 2006 11:14 am

Re: OpenCV (v2)

Post by pjay »

Ditto.

I've really enjoyed playing with this, appreciate the work & continued updates, thanks.

One thing worth watching though, is that you release any generated images when processing in a loop (any projects utilizing a webcam for example) or you end up with quite a severe memory leak.

On the pb_effects examples:
Adding:

Code: Select all

If *gray : cvReleaseImage(@*gray) : EndIf
to the beginning of the Repeat:Until section does the job.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Thanks guys.

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

Renamed examples that utilize the webcam with _cam.

Updated pb_effects_1_cam.pb & pb_effects_2_cam.pb due to a memory leak (thanks pjay).

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

I would also like to mention the amount of work AAT has put into this project, and is continuing to put into it.
- most of his work, and our correspondence is outside of this forum, so it may not have been completely obvious

Cheers!
Last edited by JHPJHP on Tue Dec 03, 2013 2:20 pm, edited 1 time in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Added 1 Structure and 1 Function, updated 1 example: cv_contours_1x.pb, and added 1 example: cv_contours_2.pb.

The new example finds the convex hull of a point set.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: OpenCV (v2)

Post by Mythros »

This is AMAZING, JHP & AAT! Keep up the GREAT work!

However, is there an easy way to implement sound into the AVI videos? A video is no good without sound!

Thank You!
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Added Constants, Structures, Function, and 3 examples.

In the first example: cv_contours_3a.pb: Contours & Approximating Polygonal Curves Image Effect - small circles are drawn at each Contour (x/y) point with precision set to 5.

In the second example: cv_contours_3b.pb: Contours & Approximating Polygonal Curves Image Effect with Bounding Rectangle - precision is set to 0 returning the most points; extracted from the list of points are coordinates used to draw a bounding rectangle around the image.

How this is different from other examples is the ability to capture the return values (x/y points), which can range from hundreds to just a few depending on the precision setting.

In the third example: cv_contours_4.pb: Contours & Convexity Defects Image Effect - small circles are draw at each Convexity Defect. A part of the script that deserves some attention is the cvCvtSeqToArray function; is converts the information stored in the CvSeq Structure returned from CvConvexityDefect to an array storing various (x/y) points.

NB*: In every update I do, there may be other small changes to examples or includes that are not mentioned, so it's best to replace the entire package.

Cheers!
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: OpenCV (v2)

Post by JHPJHP »

Added Functions, renamed, deleted, replaced, added examples (too many changes to remember).
-- cv_cam_logo.pb: adds an image to top left corner of webcam interface (inspired by work AAT was doing with cvAnd & cvXor)
-- cv_contours_4.pb: used exclusively for certain types of squares, rectangles
-- cv_ROI.pb: takes a region from an image and displays it as its own image
-- pb_cam_effects_3.pb: showcases the cvAnd & cvXor Functions (contributed by AAT)

Cheers!
Locked