PureBasic Interface to OpenCV

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

Re: PureBasic Interface to OpenCV ---- 02/04/14 06:00 PM

Post by JHPJHP »

Updated:
- added Constants, Structures, Macros, Functions, Inline Functions (converted to Procedures)
- added 1 example
-- cv_kalman.pb: tracks a rotating point at a constant rotation speed

Original Code: kalman.c - found in a previous version of OpenCV.
Tracking of rotating point.
Rotation speed is constant.
Both state and measurements vectors are 1D (a point angle),
Measurement is the real point angle + gaussian noise.
The real and the estimated points are connected with yellow line segment,
the real and the measured points are connected with red line segment.
(if Kalman filter works correctly, the yellow segment should be shorter than the red one).
This was another difficult example to convert and get working, but well worth it as it helped me to correct some Structure declarations, and various other inconstancies.

Even though the example may seem like it's producing a very basic function, that's not the case. See the following article for more information: http://cs.unc.edu/~welch/media/pdf/kalman_intro.pdf

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
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV ---- 02/11/14 12:20 AM

Post by JHPJHP »

Updated:
- added Macros
- added 1 example
-- cv_squares.pb: find squares in an image

Original Code: squares.c - found in a previous version of OpenCV.

I've also added a Save context menu option to some examples that didn't previously include it, such as le_delaunay.pb.

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
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV ---- 02/11/14 08:40 PM

Post by JHPJHP »

Updated:
- added Macros, Constants
- added 1 example
-- cv_cam_flow_2.pb: dense optical flow technique using the Gunnar Farneback algorithm

Original Code: fback_c.c found in the current version of OpenCV.

Note:
- the Gunnar Farneback algorithm runs slower (on my system) then the other flow examples

NB*: My motivation now is to fill-in some of the holes I'm noticing in the package, the examples are a way to test the various includes.
Last edited by JHPJHP on Wed Feb 12, 2014 11:21 pm, edited 1 time in total.

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
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV ---- 02/12/14 05:00 PM

Post by JHPJHP »

Updated:
- added 1 Function
- updated Structures
- added 1 example
-- cv_cam_motion.pb: gradients of motion history are used to detect direction of motion

Original Code: motempl.c found in the current version of OpenCV.
This program demonstrates the use of motion templates -- basically using the gradients
of thresholded layers of decaying frame differencing. New movements are stamped on top
with floating system time code and motions too old are thresholded away. This is the
'motion history file'. Gradients of motion history are used to detect direction of motion.

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
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV ---- 02/18/14 10:30 PM

Post by JHPJHP »

Updated:
- added 1 Function, 1 Inline Function (converted to a Procedure), 1 Procedure
- added 1 example
-- cv_drawing.pb: a demonstration of OpenCV's drawing and text output functions

Original Code: drawing.c - found in a previous version of OpenCV.

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

Updated: Fixed an issue with the handling of signed to unsigned...

NB*: Handling of Modulo operations: https://en.wikipedia.org/wiki/Modulo_operation.
... there are two possible choices for the remainder, one negative and the other positive, and there are also two possible choices for the quotient. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language...

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 ---- 02/19/14 10:40 AM

Post by AAT »

Hi, JHPJHP!
Thank you for your hard work and for your examples!

What do you think about ability to change FONT_HERSHEY to another font? For example TimesRoman?
Hershey font is not supporting other codepage than "english", so i can't to write any text in russian. :(

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

Re: PureBasic Interface to OpenCV ---- 02/19/14 10:40 AM

Post by JHPJHP »

Hi AAT,

http://en.wikipedia.org/wiki/Hershey_font
Hershey Font:

The fonts include Latin, Greek, Cyrilic, Japanese (kanji, hiragana and katakana), and symbol glyphs.
From what I read cvPutText only supports ASCII characters, for special character OpenCV needs to be compiled with Qt support:
http://opencv.jp/opencv-2.2_org/c/highg ... tml#FontQt

The CvFont Structure has Greek and Cyrillic parameters:

Code: Select all

Structure CvFont Align #PB_Structure_AlignC
  nameFont.s
  blue.d
  green.d
  red.d
  alpha.d
  font_face.l
  ascii.l
  greek.l
  cyrillic.l
  hscale.f
  vscale.f
  shear.f
  thickness.l
  dx.f
  line_type.l
EndStructure
--------------------------------------------------

Updated:
- added Constants, Functions, Inline Functions (converted to Procedures)
- added 1 example
-- cv_kmeans.pb: generates an image with random points grouped by a random number of cluster centers

Original Code: kmeans.c - found in a previous version of OpenCV.

Note: CvMat Structure / Data Pointers...

For anyone interested, I mean AAT :) you may find the following links informative:
- http://note.sonots.com/OpenCV/MatrixOperations.html
- http://stackoverflow.com/questions/1712 ... ncvs-cvmat

Cheers!
Last edited by JHPJHP on Fri Feb 21, 2014 11:08 am, edited 3 times in total.

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 ---- 02/20/14 11:20 PM

Post by AAT »

Hi, JHPJHP!
Qt is very heavy to use it to write text in openCV window :shock:
So, i found more easy solution in openCV examples :)
Look:
Image

Code: Select all

IncludeFile "includes/cv_functions.pbi"

Global GetCV.b, *save.IplImage, ExitCV.b, lpPrevWndFunc

#CV_WINDOW_NAME = "PureBasic Interface to OpenCV"
#CV_DESCRIPTION = "How to draw text in openCV with any font."

Procedure WindowCallback(hWnd, Msg, wParam, lParam)
  Select Msg
    Case #WM_COMMAND
      Select wParam
        Case 1
          GetCV = #True
          keybd_event_(#VK_ESCAPE, 0, 0, 0)
        Case 2
          FileName.s = SaveFile()

          If FileName
            params.SAVE_INFO

            Select LCase(GetExtensionPart(FileName))
              Case "jpeg", "jpg", "jpe"
                params\paramId = #CV_IMWRITE_JPEG_QUALITY
                params\paramValue = 95
              Case "png"
                params\paramId = #CV_IMWRITE_PNG_COMPRESSION
                params\paramValue = 3
              Case "ppm", "pgm", "pbm"
                params\paramId = #CV_IMWRITE_PXM_BINARY
                params\paramValue = 1
              Default
                Select SelectedFilePattern()
                  Case 0
                    FileName + ".jpg"
                    params\paramId = #CV_IMWRITE_JPEG_QUALITY
                    params\paramValue = 95
                  Case 1
                    FileName + ".png"
                    params\paramId = #CV_IMWRITE_PNG_COMPRESSION
                    params\paramValue = 3
                  Case 2
                    FileName + ".ppm"
                    params\paramId = #CV_IMWRITE_PXM_BINARY
                    params\paramValue = 1
                EndSelect
            EndSelect
            cvSaveImage(FileName, *save, @params)
          EndIf
        Case 10
          keybd_event_(#VK_ESCAPE, 0, 0, 0)
      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
      *save.IplImage = *param\uPointer1
      DisplayPopupMenu(0, *param\uValue)
  EndSelect
EndProcedure

Procedure OpenCV(ImageFile.s)
  If FileSize(ImageFile) > 0
    cvNamedWindow(#CV_WINDOW_NAME, #CV_WINDOW_NORMAL)
    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(1, "Open")
      MenuBar()
      MenuItem(2, "Save")
      MenuBar()
      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))
    
    *image.IplImage = cvLoadImage(ImageFile, #CV_LOAD_IMAGE_ANYDEPTH | #CV_LOAD_IMAGE_ANYCOLOR)
    dtWidth = DesktopWidth(0)
    dtHeight = DesktopHeight(0)

    If *image\width >= dtWidth - 100 Or *image\height >= dtHeight - 100
      iWidth = dtWidth - 100
      iRatio1.d = iWidth / *image\width
      iHeight = dtHeight - 100
      iRatio2.d = iHeight / *image\height

      If iRatio1 < iRatio2
        iWidth = *image\width * iRatio1
        iHeight = *image\height * iRatio1
      Else
        iWidth = *image\width * iRatio2
        iHeight = *image\height * iRatio2
      EndIf
      cvResizeWindow(#CV_WINDOW_NAME, iWidth, iHeight)
      *resize.IplImage = cvCreateImage(iWidth, iHeight, #IPL_DEPTH_8U, *image\nChannels)
      cvResize(*image, *resize, #CV_INTER_AREA)
    Else
      cvResizeWindow(#CV_WINDOW_NAME, *image\width, *image\height)
      *resize.IplImage = cvCloneImage(*image)
    EndIf
    cvMoveWindow(#CV_WINDOW_NAME, 20, 20)
    ToolTip(window_handle, #CV_DESCRIPTION)
  
    *mask.IplImage = cvCreateImage(*resize\width, 80, #IPL_DEPTH_8U, *resize\nChannels)           
    cvSet(*mask, 0, 0, 0, 0, #Null)       

    Font = LoadFont(#PB_Any, "BetinaScriptCTT", 36, #PB_Font_Bold)
       
    pbimage = CreateImage(#PB_Any, *mask\width, 80, 24 , RGB($00,$00,$00))  
    StartDrawing(ImageOutput(pbimage))
    DrawingFont(FontID(Font))       
    DrawText((*mask\width-480)/2, 5, "Hello, any font in openCV!", RGB($FF,$FF,$FF))        
    *mask\imageData = DrawingBuffer()
    StopDrawing()
    cvFlip(*mask, #Null, 0)    
    
    cvSetImageROI(*resize, 0, *resize\height-100, *resize\width, 80)
    cvAddS(*resize, 70,70,70,0, *resize, 0)
    cvSub(*resize, *mask, *resize, 0)     
    cvResetImageROI(*resize)   
    
    *param.USER_INFO = AllocateMemory(SizeOf(USER_INFO))
    *param\uPointer1 = *resize
    *param\uValue = window_handle
    cvSetMouseCallback(*window_name, @CvMouseCallback(), *param)

    Repeat
      If *resize       
        cvShowImage(#CV_WINDOW_NAME, *resize)
        keyPressed = cvWaitKey(0)
      EndIf
    Until keyPressed = 27 Or ExitCV
    FreeMemory(*param)
    cvReleaseImage(@*resize)
    cvReleaseImage(@*image)
    cvDestroyWindow(#CV_WINDOW_NAME)    

    If GetCV
      GetCV = #False
      ExitCV = #False
      OpenCV(GetImage())
    EndIf
  EndIf
EndProcedure

ExamineDesktops()
OpenCV(GetImage())
Thank you for links, i will read they.

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

Re: PureBasic Interface to OpenCV ---- 02/20/14 11:20 PM

Post by JHPJHP »

Hi AAT,

Very nice work - thinking outside the box; just added your font script to the package, as well as your Plate Recognition script posted awhile ago.

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

Updated:
- added 2 examples (contributed by AAT)
-- cv_pbfont.pb: draw text using PureBasic commands with any font onto an OpenCV generated image
-- cv_plate.pb: number plate recognition (experimental)
- added 2 default images

Thanks again AAT.

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
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV ---- 02/22/14 04:10 PM

Post by JHPJHP »

Updated:
- renamed 1 example
-- cv_kmeans.pb to cv_kmeans_1a.pb
- added 1 example
-- cv_kmeans_1b.pb: image segmentation by k-means clustering and color reduction

Original source can be found here.

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

Update: There's a conditional argument (iReshape = #False) that is hardcoded because method 2 seems to work with all images, while method 1 crashes the example with some images.
- you would think a platform function would be more stable then direct memory access

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
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV ---- 02/22/14 11:50 PM

Post by JHPJHP »

Updated:
- added Constants
- updated 1 Function
- added 1 example
-- cv_template.pb: compares a template against overlapped image regions; matching one face against many faces
- added 1 default image

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 ---- 02/22/14 11:50 PM

Post by AAT »

Hi, JHPJHP
JHPJHP wrote:-- cv_template.pb: compares a template against overlapped image regions; matching one face against many faces
Very interesting! This function has some resistance to the template rotation. It gives strange but fun results sometimes.
Try with Image :)

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

Re: PureBasic Interface to OpenCV ---- 02/22/14 11:50 PM

Post by JHPJHP »

Hi AAT,
compares a template against overlapped image regions
That's what I figured by the above quote. For me it's saying that cvMatchTemplate does a one to one (overlapped) pixel comparison for the closest result, but it's good that you demonstrated the functions limitations with an easily testable example.

You already know this, but for anyone else following, a better approach to image comparison where rotation, pixel intensity, etc. is a factor would be SURF (Nearest Neighbor).

Thanks AAT.

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

NB*: Most of the example's main functions have parameters with multiple settings which may improve results, and should already be declared in the file: constants.pbi.

The cvMatchTemplate function can be set with anyone of the following values:

Code: Select all

#CV_TM_SQDIFF 
#CV_TM_SQDIFF_NORMED
#CV_TM_CCORR
#CV_TM_CCORR_NORMED
#CV_TM_CCOEFF
#CV_TM_CCOEFF_NORMED
*** This information can be found in the reference folder: manual.pdf ***
Last edited by JHPJHP on Wed Mar 05, 2014 8:10 am, edited 1 time in total.

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
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV ---- 02/24/14 08:20 PM

Post by JHPJHP »

Updated:
- added 1 example
-- cv_cam_trackcolor.pb: tracks red objects, drawing a line that traces the objects location

Original source can be found here.

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

Hi AAT,

I remember you had some trouble getting cv_cam_angle.pb to work... this is using some of the same techniques, so I'm wondering if it works for you?

As always - thanks!

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 ---- 02/24/14 08:20 PM

Post by AAT »

Hi JHPJHP,

Nice example, thanks!
I had to change the white balance in the web-camera (make my face look green) to prevent tracking of my nose. :D
The red pen and pink marker are traced well.

Update:
The example cv_cam_angle.pb (with pink and blue markers) works fine now

Good luck!
Last edited by AAT on Tue Feb 25, 2014 5:03 am, edited 2 times in total.
Locked