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: PureBasic Interface to OpenCV

Post by JHPJHP »

Updated:
- OpenCV v3.1 to v3.2 (rebuilt the 32 bit and 64 bit binaries)
-- various C++ source files were modified to get the webcam and other examples working

This package includes the world LIB and DLL files.
-- instead of individual libraries for each module, all the modules are incorporated into a single LIB and DLL

NB*: Update includes the Microsoft vc2015 (x86/x64) runtime files.

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

Updated OpenCV 3.2 (x86/x64):
- fixed 4 examples
-- cv_edges.pb, cv_emboss_1.pb, cv_motion_blur.pb, cv_sharpen.pb

The cvFilter2D Function wasn't correctly processing the filter data passed by the Procedure cvMat.
- substituted the Procedure cvMat with the Functions cvCreateMatHeader and cvSetData
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Updated (Windows and OSX interfaces):
- updated 1 example
-- cv_sharpen.pb: add various edge filters to an image giving it different sharpened effects (sharpen / excessive / realistic)
- renamed 1 example
-- cv_addweighted.pb to cv_addweighted_1.pb
- added 1 example
-- cv_addweighted_2.pb: blend a blurred copy of an image with the original

cv_sharpen.pb: added 2 additional filters (excessive & realistic) found here.

cv_addweighted_2.pb: based on a post found here.

Image
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: PureBasic Interface to OpenCV

Post by Keya »

This is easily one of my favorite PB threads to follow, always something fascinating around every corner with each update thanks to your absolutely brilliant hard work JHP², and it doesn't hurt that OpenCV is just a damn cool lib :) and BSD too, take that GPL.

I was reading OpenCV's wiki page today and noticed it says it uses k-nearest neighbor, and the wiki page for that says "not to be confused with k-means", but in your package there's two demos for k-means ?
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi Keya,

Thank you for your positive comments.

Take a look at the example le_nearest_neighbor.pb. This demonstrates a good use of a nearest neighbor variant, comparing two images regardless of angle, size, and color. It uses the OpenCV Function cvExtractSURF (Speeded Up Robust Features) to retrieve comparable points (le_cam_extractSURF.pb, le_extractSURF.pb).

The above algorithms were used in the stitching examples (le_stitching_1.pb, le_stitching_2.pb).

Applied a small update to the examples cv_kmeans_1.pb and cv_kmeans_2.pb.
- cv_kmeans_1.pb: change the #MAX_CLUSTERS Constant to filter the number of grouped colors
- cv_kmeans_2.pb: change the cluster_count parameter to increase/decrease the number of color groups

NB*: The legacy (le_*.pb) examples are not included in the OpenCV 3.2 package.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: PureBasic Interface to OpenCV

Post by Keya »

so it has both k-nearest neighbor and k-means? (Wiki's OpenCV page didn't mention k-means just k-nearest neighbor but i don't know how comprehensive that list is!)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi Keya,

Included in all packages, located in the reference folder, are copies the OpenCV Manual with over 900 pages of information.

NB*: While it's maintained that OpenCV has moved to a C++ interface, I believe the power of the C interface is still very much in play.
Last edited by JHPJHP on Sun Feb 05, 2017 6:36 pm, edited 1 time in total.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: PureBasic Interface to OpenCV

Post by Keya »

that's twice you've avoided my question :) ok i'll read the 900 page manual
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi Keya,

Sorry about that. After re-reading my previous comment, I realize that I came across as sarcastic, and that was not my intention. I read your posts as rhetorical, and only responded to add additional information for anyone else reading this thread.

Both kMeans and kNN are a part of OpenCV, but the kNN Functions are only available from the C++ interface; the algorithms would need to be converted in order to be accessible from PureBasic.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: PureBasic Interface to OpenCV

Post by Keya »

perfect, thankyou hehe :) kmeans++ gives really high quality results anyway from what i've seen in my tests so I don't know how much better (if at all) KNN is but it seems there's not much room for improvement anyway! :)
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: PureBasic Interface to OpenCV

Post by chris319 »

I have a question about OpenCV for PB.

I am trying to create an .avi video file by writing individual video frames to the file. The frames are generated by my own PB program. Can OpenCV help me do this?

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

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi chris319,

As long as you don't require sound, see the example cv_cam_writeframe_1.pb.
- creates an AVI video file from a webcam feed
- use the context menu to start / stop capture

NB*: The example cv_cam_writeframe_2.pb generates PNG images from a webcam feed.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: PureBasic Interface to OpenCV

Post by chris319 »

Thank you for the prompt reply.

Where would the webcam feed for OpenCV come from? I am using escapi to capture the camera's output and test a new color encoding scheme. I can deliver a buffer full of encoded samples, or individual color samples in groups of three, or write those samples to disk. How would OpenCV pick up these samples? It would have to do so in sync with the camera's frame rate.

Alternatively, perhaps I could build my encoder into OpenCV using PureBasic? I would need direct access to the unencoded video buffer/samples. No audio is needed.

What I don't want is OpenCV directly recording the camera's output bcause it would bypass my color encoding scheme.

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

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi chris319,

Everything you asked in your post is available from OpenCV.

Capturing webcam frames is inherent, as is buffering, writing to disk, FPS settings, and literally hundreds of other options.

If you decide to try OpenCV and have any trouble along the way, post your questions and I will lend a hand; most likely, I will just point you to an existing example.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

chris319 wrote:When a frame of video arrives, read each pixel in the buffer, re-encode to another color space, write the newly-encoded pixel back to the buffer. This would be done in a loop like so:
- video frame arrives
- draw a series of colord rectangles over camera image (for calibration)
- draw text over rectangles described above
- for yCoord = 0 to m
-- for xCoord = 0 to n
--- read pixel B, G and R values from buffer
--- modify pixel B, G and R values
--- write pixel back to buffer (three bytes -- different color space)
-- next
- next
Modify the example cv_cam_writeframe_1.pb to include your Color Space algorithm; call it after the cvFlip Function:
- simple swap of BGR to RGB with a rectangle and some text; it should give you a start

Code: Select all

Procedure ColorFilter(*image.IplImage, *font.CvFont)
  For y = 0 To *image\height - 1
    For x = 0 To *image\width - 1
      nB = PeekA(@*image\imageData\b + (y * *image\widthStep) + x * 3 + 0)
      nG = PeekA(@*image\imageData\b + (y * *image\widthStep) + x * 3 + 1)
      nR = PeekA(@*image\imageData\b + (y * *image\widthStep) + x * 3 + 2)
      PokeA(@*image\imageData\b + (y * *image\widthStep) + x * 3 + 0, nR)
      PokeA(@*image\imageData\b + (y * *image\widthStep) + x * 3 + 1, nG)
      PokeA(@*image\imageData\b + (y * *image\widthStep) + x * 3 + 2, nB)
    Next
  Next
  cvRectangleR(*image, 10, 10, *image\width - 20, *image\height - 20, 255, 0, 0, 0, 1, #CV_AA, #Null)
  cvPutText(*image, "JHPJHP", 60, 60, *font, 0, 0, 255, 0)
  ProcedureReturn *image
EndProcedure

font.CvFont : cvInitFont(@font, #CV_FONT_HERSHEY_DUPLEX, 1, 1, #Null, 1, #CV_AA)

Repeat
  *image = cvQueryFrame(*capture)

  If *image
    cvFlip(*image, #Null, 1)
    *image = ColorFilter(*image, font)

    If captureCV : cvWriteFrame(*writer, *image) : EndIf

    cvShowImage(#CV_WINDOW_NAME, *image)
    keyPressed = cvWaitKey(100)
  EndIf
Until keyPressed = 27 Or exitCV
chris319 wrote:Also, I was wondering if it is possible to turn an OpenCV image into a standard PureBasic image.
Download the package PureBasic Interface to OpenCV 2.4.13 :: Windows Demo (32bit / 64bit)
- see the example pb_webcam.pb
Last edited by JHPJHP on Thu Mar 02, 2017 6:32 am, edited 6 times in total.
dige
Addict
Addict
Posts: 1256
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: PureBasic Interface to OpenCV

Post by dige »

Hi JHPJHP, would OpenCV also be suitable for 4K videoplayback and fast framecapture?

With directshow, capturing is very slow and since microsofts new media foundation
framework, codec changing is very tricky and needs admin rights..

greetz dige
"Daddy, I'll run faster, then it is not so far..."
Locked