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

Post by AAT »

Hi JHPJHP.
Image
four minutes ago !
I'm the first, isn't it? :)
-----------------
Your last example cv_cam_pbdatabase.pb is exellent. I'm using SQLite too, it's small and fast.
Last edited by AAT on Fri Apr 11, 2014 11:21 pm, edited 1 time in total.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

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

Post by JHPJHP »

Hi AAT,
four minutes ago !
I'm the first, isn't it?
I think so, but since it was your idea in the first place - it's only fitting.

Let me know if the new example was helpful, either way it's a good addition to the package - thanks.
Last edited by JHPJHP on Fri Apr 11, 2014 11:49 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.
AAT
Enthusiast
Enthusiast
Posts: 259
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

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

Post by AAT »

JHPJHP wrote:...I think so, but since it was your idea...
It's my bad english :oops: I mean, i'm the first in waiting list for downloading. :)

I've tested your example cv_cam_writeframe_1.pb. It works fast: about 7 frames/sec. for me.

Thanks for cv_cam_pbdatabase.pb example: for now i know how to set compression level.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

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

Post by JHPJHP »

Hi AAT,

I know we sometimes have trouble understanding one another because of the language barrier, but we both speak PureBasic pretty well. :!:

I remember when I first started this... your post was my first introduction to OpenCV: http://www.purebasic.fr/english/viewtop ... 12&t=48212.

... and it's been a good collaboration ever since!

Cheers.

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

Post by AAT »

Image
User avatar
minimy
Enthusiast
Enthusiast
Posts: 552
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

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

Post by minimy »

Hi, how pass a frame to pb_image?

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

Post by JHPJHP »

Hi minimy,
how pass a frame to pb_image
The example cv_cam_pbdatabase.pb uses a Procedure that does just that - See: includes/pb_procedures.pbi...

Code: Select all

ImportImage(*image, name.s, extension.s = ".jpg")
;...
;encode image to memory (OpenCV)
  *mat.CvMat = cvEncodeImage(extension, *image, @params)
;load image from memory (PureBasic)
  pbimage = CatchImage(#PB_Any, *mat\ptr)
;...
-----------------------------------------

Updated:
- added 1 example
-- cv_floodfill.pb: demonstration of the floodfill function, filling a connected component with a given color

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

NB*: Update also includes various bug fixes and improvements.

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 ---- 04/14/14 01:40 AM

Post by AAT »

Hi!

Many times I wanted to see the tree of calling openCV functions in examples. So, i have wrote small program, that makes 4 files:
- cvFuncList.txt - the list of cv function in cv_functions.pbi file;
- ExamplesList.txt - the list of examples;
- ExampleFuncList.txt - the Example <->> cvFunstions tree;
- FuncExampleList.txt - the cvFunstion <->> Examples tree.
You have to copy "cv_functions.pbi" to a folder with this program or to setup full path to this file, and to copy all the openCV examples to any directory, for example "d:\00_ex".

Code: Select all

; OpenCV funsctions - Examples call trees.
;

Structure funcfile
  cvFunc.s
  exampleName.s
EndStructure

Dim pos.l(2)
Dim funcInFile.s(1)
fifcntr.l=0
Dim funcList.s(1)
cntr.l=0
Dim exampleList.s(1)
fecntr.l=0
Dim FuncExampleList.funcfile(1)
fflcntr.l=0
Dim exmpls.s(1)
exmplscntr.l = 0

; Path to OpenCV examples
Directory$ = "d:\00_ex";            

; Path to OpenCV includes
ReadFile(0, "d:\00\includes\cv_functions.pbi")
CreateFile(1, "cvFuncList.txt")
While Not Eof(0)
  str$ = ReadString(0)
  If Not FindString(str$, "cv")
    Continue
  EndIf  
  If FindString(str$, "cv_")
    Continue
  EndIf 
  startpos = FindString(str$, "cv")  
  func$=Trim(Mid(str$, startpos))
  pos(0) = FindString(func$, ".")
  pos(1) = FindString(func$, "(")
  pos(2) = FindString(func$, "-")
  SortArray(pos(),#PB_Sort_Ascending)
  If pos(0) <> 0
    pos = pos(0)
  ElseIf pos(1) <> 0
    pos = pos(1)
  Else
    pos = pos(2)
  EndIf  
  If pos
    ReDim funcList(cntr + 1)
    funcList(cntr) = Left(func$, pos-1)
    cntr + 1
  EndIf  
Wend
cntr - 1
ReDim funcList(cntr)
SortArray(funcList(),#PB_Sort_Ascending + #PB_Sort_NoCase)
prevfunc2$ = ""

Dim funcListR.s(1)
cntrR.l=0

For i=0 To cntr
  func2$ = funcList(i)
  If func2$ <> prevfunc2$
    WriteStringN(1,func2$)
    prevfunc2$ = func2$
    funcListR(cntrR) = func2$
    cntrR+1
    ReDim funcListR(cntrR)
  EndIf  
Next
CloseFile(0)
CloseFile(1)

CreateFile(2, "ExamplesList.txt")
CreateFile(3, "ExampleFuncList.txt")
If ExamineDirectory(4, Directory$, "*.*")  
  Debug "Files found:"
  While NextDirectoryEntry(4)
    If DirectoryEntryType(4) = #PB_DirectoryEntry_File
      Debug DirectoryEntryName(4)
      WriteStringN(2, DirectoryEntryName(4))      
      res = ReadFile(5, Directory$+"\"+DirectoryEntryName(4))
      WriteStringN(3, DirectoryEntryName(4)+":") 
      
      ReDim funcInFile(1)
      fifcntr.l=0
      While Not Eof(5)
        fileline$ =  ReadString(5) 
        For k=0 To cntr 
          If FindString(fileline$, funcList(k), 1,  #PB_String_NoCase)
            ReDim funcInFile(fifcntr+1) 
            funcInFile(fifcntr) = funcList(k) 
            fifcntr + 1
          EndIf          
        Next k         
      Wend
      CloseFile(5) 
      fifcntr - 1  
      ReDim funcInFile(fifcntr) 
      SortArray(funcInFile(),#PB_Sort_Ascending + #PB_Sort_NoCase)
      prevfunc3$ = ""

      For m=0 To fifcntr  
        func3$ = funcInFile(m)
        If func3$ <> prevfunc3$
          WriteStringN(3,"    "+func3$)   
          prevfunc3$ = func3$
          
          ReDim FuncExampleList(fflcntr + 1)         
          FuncExampleList(fflcntr)\cvFunc = func3$
          FuncExampleList(fflcntr)\exampleName = DirectoryEntryName(4)
          fflcntr + 1
        EndIf  
      Next
      WriteStringN(3,"    ") 
      exampleList(fecntr) = DirectoryEntryName(4)
      fecntr + 1
      ReDim exampleList(fecntr)
    EndIf
  Wend
  FinishDirectory(4)
EndIf
CloseFile(2)
CloseFile(3)

CreateFile(5, "FuncExampleList.txt")
ReDim FuncExampleList(fflcntr - 1)
SortStructuredArray(FuncExampleList(), #PB_Sort_NoCase+#PB_Sort_Ascending, OffsetOf(funcfile\cvFunc), TypeOf(funcfile\cvFunc))
prevfunc4$ = ""
For p=0 To cntrR
  func4$ = funcListR(p)
  exmplscntr = 0     
  ReDim exmpls(0)
  For q = 0 To fflcntr-1
    If Trim(FuncExampleList(q)\cvFunc) = Trim(func4$)
      exmpls(exmplscntr) = FuncExampleList(q)\exampleName
      exmplscntr + 1
      ReDim exmpls(exmplscntr)
    EndIf    
    If Trim(FuncExampleList(q)\cvFunc) > Trim(func4$)
      If exmplscntr > 1
        ReDim exmpls(exmplscntr-1)
        SortArray(exmpls(),#PB_Sort_Ascending + #PB_Sort_NoCase)
      EndIf
      WriteStringN(5,funcListR(p)+":")
      For r = 0 To exmplscntr - 1
        WriteStringN(5,"    "+exmpls(r))
      Next r
      WriteStringN(5,"    ")
      Break
    EndIf
  Next q
Next p
CloseFile(5)
Debug "========================================="
Debug "Search completed."
There are many openCV function not used in examples, so it is possible to write more examples :)

P.S. It's possible to make database Examples <<->> cvFunstion with many to many relations. But i have no time :(

Good luck!
User avatar
minimy
Enthusiast
Enthusiast
Posts: 552
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: PureBasic Interface to OpenCV ---- 04/14/14 01:40 AM

Post by minimy »

Thanks JHP JHP! I have many examples, but i dont have this file. Please, can show me the url or code.
And another few quests, openCV is only video, no audio. Or have audio too?
Is posible play movies into openCV window?

Thanks again for your time and patience! :)
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 ---- 04/14/14 11:15 PM

Post by JHPJHP »

Hi AAT,

That's a great bit of code, and very useful... I didn't realize how many function haven't been used; I can't help but think that you may have had an ulterior motive for writing it. :P
There are many openCV function not used in examples, so it is possible to write more examples
-----------------------------------------

Hi minimy,

In the same folder as the examples is the includes folder, containing the following files:
- cv_constants.pbi
- cv_functions.pbi
- cv_macros.pbi
- cv_structures.pbi
- pb_procedures.pbi

The file pb_procedures.pbi hosts the ImportImage Procedure...

You are correct that OpenCV does not support audio - it's exclusively an "Open Source Computer Vision Library".

Let me know if you need any help with putting together some script.

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

Updated:
- added 1 Function
- added 2 examples
-- cv_pyrmeanshift.pb: performing the meanshift segmentation of an image, a cartoon effect is achieved
-- le_pyrsegmentation.pb: implementing image segmentation by pyramids, a cartoon effect is achieved

The new examples perform pretty much the same function, but I was interested to see if there was a difference in output / speed, so I coded the legacy version as well.

NB*: Update also includes various bug fixes and improvements.

Hi AAT - cvPyrMeanShiftFiltering was one of the functions I noticed wasn't used in an example after I ran your script. :wink:

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 ---- 04/14/14 11:15 PM

Post by AAT »

Hi, JHPJHP!

I like your new examples!
The example cv_pyrmeanshift.pb is pleasant to me more then example le_pyrsegmentation.pb: the colours looks more natural.
And i set cvSmooth parameter to 3.

P.S. I am glad that there is a sense from my small utility ;)

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

Re: PureBasic Interface to OpenCV ---- 04/14/16 03:30 PM

Post by JHPJHP »

Hi AAT,

Thanks for the feedback.

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

Updated:
- renamed 1 example
-- cv_cam_game.pb to cv_cam_fallingballs.pb
- added 1 example
-- cv_solvemaze.pb: maze game / solve a maze using a morphological transformation
- added 3 images

cv_solvemaze.pb was based on script found here.

This is one of those examples that could easily be taken to the next level, but the main point of it was to show how a simple morphological transformation can be used to solve a "Perfect Maze".

Cheers!

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 ---- 04/14/16 03:30 PM

Post by minimy »

Hi JHPJHP, thank you very match for your help.
When i send a movie to cvnamedwindow, when correct aspect ratio, I see the window background.
Im trying to change the back color of the cvnamedwindow with SetBkColor_(hWnd,0), but not work. Can help me? please, and thanks again.

Another quest, how i know the number of capture device in the computer?.
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 ---- 04/16/14 03:30 PM

Post by JHPJHP »

Hi minimy,

Have you tried:

Code: Select all

SetBkColor_(window_handle, 0)
how i know the number of capture device in the computer
I need more information on what you're trying to do.

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

Updated:
- renamed 1 example
-- cv_rotate.pb to cv_rotate_1.pb
- added 1 example
-- cv_rotate_2.pb: retrieves pixel quadrangle with sub-pixel accuracy, applying 2D rotation to an image

Based on script found here.

Another Function [ cvGetQuadrangleSubPix ] not previously used in any other example; cheers AAT. :wink:

NB*: The output difference between cv_rotate_1.pb and cv_rotate_2.pb is not evident with their default images; cv_rotate_1.pb allows for a color border while cv_rotate_2.pb replicates the border region.
Last edited by JHPJHP on Wed Apr 23, 2014 8:21 pm, 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 ---- 04/18/14 07:30 PM

Post by AAT »

Hi JHPJHP!

Thanks for new example! I have a question about it.
I read such explanation (http://www.shervinemami.info/imageTransforms.html):
"The function below will rotate an image using cvGetQuadrangleSubPix(), which is supposed to be the faster way of rotating images in OpenCV compared to cvWarpAffine()".
The example "cv_rotate_2.pb" with cvGetQuadrangleSubPix() works much slower then "cv_rotate_1.pb" with cvWarpAffine() for me.
That do think about it? Perhaps this is due to computing trigonometric functions and floating point computing in the procedure RotateImage()?

Good luck!
Locked