PureBasic Interface to OpenCV

Developed or developing a new product in PureBasic? Tell the world about it.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: PureBasic Interface to OpenCV

Post by Mythros »

Hi, JHP! This really doesn't help me much..... Isn't there something a bit easier in your examples?

Thanks!

Mythros
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi Little John,

That put a smile on my face... You're welcome!

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

Hi AAT,

Version 3.1 at least, my thoughts exactly.

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

Hi Mythros,

The first example I provided: cv_cam_convexitydefects.pb - while it would be more dynamic with controlling the mouse pointer, does require a lot more coding to achieve accurate results, especially if working with skin tones.

But the second example I provided: cv_cam_track_color.pb - should be easier to get started with and grow from there depending on your requirements. I've added a single line of code that will move the mouse pointer to where the line is being drawn. Grab something red and move it across the view of your webcam, you should see a red line being drawn. Toggle the Spacebar to turn ON / OFF mouse tracking.

You need to give yourself more time to work with the examples, go through each section of code to understand what is being done. When I first started this project I spent a minimum of 40 hours just trying to figure out how to add color to an object, but once that clicked everything else became a little easier. If you have questions on what a bit of code is doing, or what would be a good direction to achieve a desired result, I'm more then happy to offer some advice.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
sphinx
Enthusiast
Enthusiast
Posts: 120
Joined: Thu Oct 26, 2006 6:34 pm
Contact:

Re: PureBasic Interface to OpenCV

Post by sphinx »

Keep up the great work JHPJHP.

We need someone to write some tutorials of your interface to OpenCV for people who have zero/little knowledge in image processing and machine vision, I might do that after getting more familiar with your interface which was a dream for me for a long time before I take the lead to .NET world :(

Actually I am writing (almost finished) a machine vision software using C# and AForge (performance is slower than OpenCV of course) which threshold, detect objects and get their Major/Minor Axis, Circularity....etc and then determine abnormalities according to a normal range and finally says if the specimen processed passed or failed.

A big thank you for you :)
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

PureBasic Interface to OpenCV

Post by JHPJHP »

Hi sphinx,

Thank you for the positive post, I really appreciate the acknowledgement.

Tutorials are a good idea, I sometimes forget that there can be a learning curve when starting a project this size; although there's a lot of documentation floating around the internet. My suggestion for anyone interested in learning about "PureBasic's Interface to OpenCV" is not to approach it as a whole, but to tackle each example seperately.

Writing your own machine vision software is impressive. Predominately I've written my examples on the backs of others, those who have carried the weight of OpenCV's powerful algorithm's. I'm just thankful for what I've learned along the way, translating the various examples to PureBasic.

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

Updated 09/04/14 08:20 PM
- deleted 2 example
-- cv_canny_1.pb
-- cv_convexhull_1.pb
- renamed 2 example
-- cv_canny_2.pb to cv_canny.pb
-- cv_convexhull_3.pb to cv_convexhull_1.pb
- added 1 example
-- cv_fractal_4.pb: generates an iterative Mandelbrot Set, the most popular geometrical fractal

cv_fractal_4.pb: Based on script found here.
- this is an impressive algorithm considering the speed with which it iterates through the fractal.

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

I've renamed some of the numbered examples, the ones better demonstrating the desired result were assigned first.

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

Updated: cv_fractal_1.pb (formally cv_fractal_4.pb)
- corrected a scaling problem
- added the ability to show previous selections
- added the ability to flip between RGB and BGR

NB*: Various memory issues have also been addressed in numerous examples.
Last edited by JHPJHP on Thu Sep 18, 2014 3:19 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: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Updated:
- added 2 examples
-- cv_distortion_1.pb: distort a color image by manipulating pixel locations
-- cv_distortion_2.pb: distort a grayscale image by manipulating pixel locations

Based on script found here.

NB*: Additional changes occurred after the last update, and prior to this one; see previous post.
Last edited by JHPJHP on Wed Sep 10, 2014 7:47 am, edited 2 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.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: PureBasic Interface to OpenCV

Post by applePi »

Hi JHPJHP
if we directed the tv remote control to the webcam we will see a white point, that is because the remote control led produce an infra red light which can be seen only with a webcam, or the phone cam.
suppose i want to check for the presence of ghosts in a dark room using the webcam with opencv with purebasic. is it possible to fire the speakers sound when the webcam see something new in the dark room (ghosts, mice, cosmic ray,... etc) and then begins to record the scene to avi file?.
PS: i am not hurry, there is no ghosts in my room, it is just an idea.
AAT
Enthusiast
Enthusiast
Posts: 259
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi applePi.
All you need you will find in last pack:
- cv_cam_zone_tracking.pb - guard system
- cv_cam_writeframe_1.pb - how to write video
Enjoy!

P.S.Some words about ghosts: they are real! :shock:
When I switched my IP-camera into an IR mode with IR illumination, I saw quickly flying translucent objects...
It were small particles of a dust. :D
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi applePi,

I shouldn't be surprised with your out-of-the-box ideas, considering all the great work you're doing in the 3D Programming forum. Using the "PureBasic's Interface to OpenCV" platform as a ghost detector would make for a great example. AAT's suggestions are definitely a good place to start, but if you have any additional questions I'm more then happy to help.

The following link may be helpful: http://www.wikihow.com/Make-a-Webcam-In ... red-Camera

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

Updated the examples: cv_distortion_1.pb & cv_distortion_2.pb:
- added the ability to select an area on an image to distort
-- better results when there is a sold background with multiple objects
- resetting the selection to full-window, but retaining the distortion can be done by clicking anywhere on the image

NB*: Can now switch between distortion methods without losing previous distortions.
Last edited by JHPJHP on Wed Sep 10, 2014 5:42 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.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: PureBasic Interface to OpenCV

Post by applePi »

thank you AAT, you are right it is the example cv_cam_zone_tracking.pb what is needed.
in fact in windows xp it is dangerous to do test with sound since my system have frozen completely twice. so i have resorted to windows 7 which i think i will begins to use permanently.
the only place i can see is this:
If tracking
...
EndIf

i have tested this:

Code: Select all

If tracking
                  cvRectangleR(*image, *contours\rect\x, *contours\rect\y, *contours\rect\width, *contours\rect\height, 0, 255, 255, 0, 1, #CV_AA, #Null)
                  cvPutText(*imgAverage, "ghost detected, wake up...", 20, 40, @font, 0, 255, 255, 0)
                  cvShowImage(#CV_WINDOW_NAME, *imgAverage)
                  cvWaitKey(2)
                EndIf
when i choose the zone 5, then begins the trace with T. at any movement from me the message "ghost detected" are flashing. if i calm again it will stop. so this is the beginning.
i will think about the sound triggering later. since my desktop in win xp have frozen twice. i think it will be better in win 7. such as calling the playSound only once using a flag.
once again what i will do if the program at the midnight triggered, i think i don't have enough courage to see what is happening.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: PureBasic Interface to OpenCV

Post by applePi »

a million thanks JHPJHP, it seems you have made the opencv a perfect and contains everything.
i have a hobby with simple electronics, and this is an opportunity to use opencv with purebasic
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: PureBasic Interface to OpenCV

Post by applePi »

yes it works okay with sound, let the webcam directed to static scene, then choose zone by pressing 5 as an example, then press T. now wave a pencil in front the cam and the speaker will play "Windows Logon Sound.wav"
allow me JHPJHP to repost your example but adding the sound init and playsound for those who want quick test
note that you should wave the pencil once every time else the sound will continue for longer time. or use a flag to play it once such as
if flag = 0
PlaySound(snd)
endif
flag =
1
cv_cam_zone_tracking.pb with sound
PS: if you copy and past the following to a new file then look JHPJHP notes in the next reply , so the program can see the opencv_core249.dll

Code: Select all

IncludeFile "includes/cv_functions.pbi"
InitSound()
LoadSound(snd, "c:\windows\media\Windows Logon Sound.wav")
Global lpPrevWndFunc

#CV_WINDOW_NAME = "PureBasic Interface to OpenCV"
#CV_DESCRIPTION = "Calculates the weighted sum of the input image and the accumulator creating a silhouette " +
                  "effect, detecting objects found within one or more zones, from the webcam interface." +
                  Chr(10) + Chr(10) + "- SPACEBAR: Toggle between foreground/background view." +
                  Chr(10) + Chr(10) + "- [ 1 - 9 ] KEYS: Toggle zones." +
                  Chr(10) + Chr(10) + "- [ G ] KEY: Toggle grid." +
                  Chr(10) + Chr(10) + "- [ T ] KEY: Toggle tracking." +
                  Chr(10) + Chr(10) + "- [ V ] KEY: Change PIP view."

Structure ZONE_RECT
  xBegin.l
  xEnd.l
  yBegin.l
  yEnd.l
  status.l
EndStructure

ProcedureC WindowCallback(hWnd, Msg, wParam, lParam)
  Shared exitCV.b

  Select Msg
    Case #WM_COMMAND
      Select wParam
        Case 10
          exitCV = #True
      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
      DisplayPopupMenu(0, *param\uValue)
  EndSelect
EndProcedure

Repeat
  nCreate + 1
  *capture = cvCreateCameraCapture(0)
Until nCreate = 5 Or *capture

If *capture
  cvNamedWindow(#CV_WINDOW_NAME, #CV_WINDOW_AUTOSIZE)
  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(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))
  cvMoveWindow(#CV_WINDOW_NAME, 20, 20)
  ToolTip(window_handle, #CV_DESCRIPTION)
  FrameWidth = cvGetCaptureProperty(*capture, #CV_CAP_PROP_FRAME_WIDTH)
  FrameHeight = cvGetCaptureProperty(*capture, #CV_CAP_PROP_FRAME_HEIGHT)
  Dim guardZone.ZONE_RECT(9)
  zoneW = (FrameWidth - 2) / 3
  zoneH = (FrameHeight - 2) / 3

  For y = 0 To 3 - 1
    For x = 0 To 3 - 1
      xy + 1
      guardZone(xy)\xBegin = zoneW * x + 2
      guardZone(xy)\xEnd = guardZone(xy)\xBegin + zoneW - 2
      guardZone(xy)\yBegin = zoneH * y + 2
      guardZone(xy)\yEnd = guardZone(xy)\yBegin + zoneH - 2
      guardZone(xy)\status = 1
    Next
  Next
  INIT_TIME = 50
  BG_RATIO.d = 0.02
  OBJ_RATIO.d = 0.005
  zeta.d = 10
  opacity.d = 0.1
  PIP = 2
  grid = 1
  *imgAverage.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
  *imgSgm.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
  *imgTmp.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
  *img_lower.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
  *img_upper.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_32F, 3)
  *imgSilhouette.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 1)
  *imgResult.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
  iRatio.d = 150 / FrameWidth
  iWidth = FrameWidth * iRatio
  iHeight = FrameHeight * iRatio
  *PIP.IplImage = cvCreateImage(iWidth, iHeight, #IPL_DEPTH_8U, 3)
  *image.IplImage
  cvSetZero(*imgAverage)
  *param.USER_INFO = AllocateMemory(SizeOf(USER_INFO))
  *param\uValue = window_handle
  cvSetMouseCallback(*window_name, @CvMouseCallback(), *param)
  font.CvFont
  cvInitFont(@font, #CV_FONT_HERSHEY_COMPLEX | #CV_FONT_ITALIC, 1, 1, #Null, 1, #CV_AA)
  cvPutText(*imgAverage, "Initiating Zones...", 20, 40, @font, 0, 255, 255, 0)
  cvShowImage(#CV_WINDOW_NAME, *imgAverage)
  cvWaitKey(100)

  For rtnCount = 0 To INIT_TIME
    *image = cvQueryFrame(*capture)
  
    If *image
      cvFlip(*image, #Null, 1)
      cvAcc(*image, *imgAverage, #Null)
    EndIf
  Next
  cvConvertScale(*imgAverage, *imgAverage, 1.0 / INIT_TIME, 0)
  cvSetZero(*imgSgm)
  
  For rtnCount = 0 To INIT_TIME
    *image = cvQueryFrame(*capture)
  
    If *image
      cvFlip(*image, #Null, 1)
      cvConvert(*image, *imgTmp)
      cvSub(*imgTmp, *imgAverage, *imgTmp, #Null)
      cvPow(*imgTmp, *imgTmp, 2)
      cvConvertScale(*imgTmp, *imgTmp, 2, 0)
      cvPow(*imgTmp, *imgTmp, 0.5)
      cvAcc(*imgTmp, *imgSgm, #Null)
    EndIf
  Next
  cvConvertScale(*imgSgm, *imgSgm, 1.0 / INIT_TIME, 0)
  *kernel.IplConvKernel = cvCreateStructuringElementEx(3, 3, 1, 1, #CV_SHAPE_RECT, #Null)
  *storage.CvMemStorage = cvCreateMemStorage(0)
  *contours.CvContour

  Repeat
    *image = cvQueryFrame(*capture)

    If *image
      cvFlip(*image, #Null, 1)
      cvConvert(*image, *imgTmp)
      cvSub(*imgAverage, *imgSgm, *img_lower, #Null)
      cvSubS(*img_lower, zeta, zeta, zeta, zeta, *img_lower, #Null)
      cvAdd(*imgAverage, *imgSgm, *img_upper, #Null)
      cvAddS(*img_upper, zeta, zeta, zeta, zeta, *img_upper, #Null)
      cvInRange(*imgTmp, *img_lower, *img_upper, *imgSilhouette)
      cvSub(*imgTmp, *imgAverage, *imgTmp, #Null)
      cvPow(*imgTmp, *imgTmp, 2)
      cvConvertScale(*imgTmp, *imgTmp, 2, 0)
      cvPow(*imgTmp, *imgTmp, 0.5)
      cvRunningAvg(*image, *imgAverage, BG_RATIO, *imgSilhouette)
      cvRunningAvg(*imgTmp, *imgSgm, BG_RATIO, *imgSilhouette)
      cvNot(*imgSilhouette, *imgSilhouette)
      cvRunningAvg(*imgTmp, *imgSgm, OBJ_RATIO, *imgSilhouette)
      cvErode(*imgSilhouette, *imgSilhouette, *kernel, 1)
      cvDilate(*imgSilhouette, *imgSilhouette, *kernel, 2)
      cvErode(*imgSilhouette, *imgSilhouette, *kernel, 1)
      cvMerge(*imgSilhouette, *imgSilhouette, *imgSilhouette, #Null, *imgResult)
      cvClearMemStorage(*storage)
      nContours = cvFindContours(*imgSilhouette, *storage, @*contours, SizeOf(CvContour), #CV_RETR_LIST, #CV_CHAIN_APPROX_SIMPLE, 0, 0)
      
      
      For zoneN = 1 To 9
        If grid
          cvRectangle(*image, guardZone(zoneN)\xBegin, guardZone(zoneN)\yBegin, guardZone(zoneN)\xEnd, guardZone(zoneN)\yEnd, 255, 0, 0, 0, 1, #CV_AA, #Null)
        EndIf

        If guardZone(zoneN)\status = 0
          *select.IplImage = cvCloneImage(*image)
          cvRectangle(*image, guardZone(zoneN)\xBegin, guardZone(zoneN)\yBegin, guardZone(zoneN)\xEnd, guardZone(zoneN)\yEnd, 0, 0, 255, 0, #CV_FILLED, #CV_AA, #Null)
          cvAddWeighted(*image, opacity, *select, 1 - opacity, 0, *image)
          cvReleaseImage(@*select)
          
        EndIf
      Next

      If nContours
        For rtnCount = 0 To nContours - 1
          area.d = cvContourArea(*contours, 0, #CV_WHOLE_SEQ_END_INDEX, 0)

          If area >= 500
            For zoneN = 1 To 9
              If guardZone(zoneN)\xBegin <= *contours\rect\x + *contours\rect\width And
                 guardZone(zoneN)\xEnd >= *contours\rect\x And
                 guardZone(zoneN)\yBegin <= *contours\rect\y + *contours\rect\height And
                 guardZone(zoneN)\yEnd >= *contours\rect\y And guardZone(zoneN)\status
                cvDrawContours(*imgResult, *contours, 255, 155, 0, 0, 155, 255, 0, 0, #CV_FILLED, 2, #CV_AA, 0, 0)
                *select.IplImage = cvCloneImage(*image)
                cvRectangle(*image, guardZone(zoneN)\xBegin, guardZone(zoneN)\yBegin, guardZone(zoneN)\xEnd, guardZone(zoneN)\yEnd, 0, 255, 0, 0, #CV_FILLED, #CV_AA, #Null)
                cvAddWeighted(*image, opacity, *select, 1 - opacity, 0, *image)
                cvReleaseImage(@*select)

                If tracking
                  cvRectangleR(*image, *contours\rect\x, *contours\rect\y, *contours\rect\width, *contours\rect\height, 0, 255, 255, 0, 1, #CV_AA, #Null)
                  
                  PlaySound(snd)
                  
                EndIf
              EndIf
            Next
          EndIf
          *contours = *contours\h_next
        Next
        
       
        Select PIP
          Case 0
            If foreground
              cvResize(*image, *PIP, #CV_INTER_AREA)
              cvSetImageROI(*imgResult, 20, 20, iWidth, iHeight)
              cvAndS(*imgResult, 0, 0, 0, 0, *imgResult, #Null)
              cvAdd(*imgResult, *PIP, *imgResult, #Null)
              cvResetImageROI(*imgResult)
              cvRectangleR(*imgResult, 19, 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
              cvShowImage(#CV_WINDOW_NAME, *imgResult)
            Else
              cvResize(*imgResult, *PIP, #CV_INTER_AREA)
              cvSetImageROI(*image, 20, 20, iWidth, iHeight)
              cvAndS(*image, 0, 0, 0, 0, *image, #Null)
              cvAdd(*image, *PIP, *image, #Null)
              cvResetImageROI(*image)
              cvRectangleR(*image, 19, 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
              cvShowImage(#CV_WINDOW_NAME, *image)
            EndIf
          Case 1
            If foreground
              cvResize(*image, *PIP, #CV_INTER_AREA)
              cvSetImageROI(*imgResult, *image\width - (150 + 20), 20, iWidth, iHeight)
              cvAndS(*imgResult, 0, 0, 0, 0, *imgResult, #Null)
              cvAdd(*imgResult, *PIP, *imgResult, #Null)
              cvResetImageROI(*imgResult)
              cvRectangleR(*imgResult, *image\width - (150 + 21), 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
              cvShowImage(#CV_WINDOW_NAME, *imgResult)
            Else
              cvResize(*imgResult, *PIP, #CV_INTER_AREA)
              cvSetImageROI(*image, *image\width - (150 + 20), 20, iWidth, iHeight)
              cvAndS(*image, 0, 0, 0, 0, *image, #Null)
              cvAdd(*image, *PIP, *image, #Null)
              cvResetImageROI(*image)
              cvRectangleR(*image, *image\width - (150 + 21), 19, iWidth + 2, iHeight + 2, 0, 255, 255, 0, 1, #CV_AA, #Null)
              cvShowImage(#CV_WINDOW_NAME, *image)
            EndIf
          Case 2
            If foreground : cvShowImage(#CV_WINDOW_NAME, *imgResult) : Else : cvShowImage(#CV_WINDOW_NAME, *image) : EndIf
        EndSelect
        keyPressed = cvWaitKey(10)

        Select keyPressed
          Case 32
            foreground ! 1
          Case 49 To 57
            guardZone(keyPressed - 48)\status ! 1
          Case 71, 103
            grid ! 1
          Case 84, 116
            tracking ! 1
          Case 86, 118
            PIP = (PIP + 1) % 3
        EndSelect
      EndIf
    Else
      Break
    EndIf
  Until keyPressed = 27 Or exitCV
  FreeSound(snd)
  FreeMemory(*param)
  cvReleaseMemStorage(@*storage)
  cvReleaseStructuringElement(@*kernel)
  cvReleaseImage(@*PIP)
  cvReleaseImage(@*imgResult)
  cvReleaseImage(@*imgSilhouette)
  cvReleaseImage(@*img_upper)
  cvReleaseImage(@*img_lower)
  cvReleaseImage(@*imgTmp)
  cvReleaseImage(@*imgSgm)
  cvReleaseImage(@*imgAverage)
  cvDestroyWindow(#CV_WINDOW_NAME)
  cvReleaseCapture(@*capture)
EndIf
Last edited by applePi on Wed Sep 10, 2014 12:41 pm, edited 1 time in total.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PureBasic Interface to OpenCV

Post by bbanelli »

applePi wrote:yes it works okay with sound, let the webcam directed to static scene, then choose zone by pressing 5 as an example, then press T. now wave a pencil in front the cam and the speaker will play "Windows Logon Sound.wav"
allow me JHPJHP to repost your example but adding the sound init and playsound for those who want quick test
note that you should wave the pencil once every time else the sound will continue for longer time. or use a flag to play it once such as
I tried running it quickly by just copy paste to a new file in my OpenCV folder from which I run all JHPJHP's examples. However, when I try running it, I get error about missing opencv_core249.dll? All other examples any my own code works properly, what am I missing?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi applePi,

I'm happy to see that the interface is being use.

Thank you for sharing.

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

Hi bbanelli,

Add the following:
- [MENU: Compiler] Compiler Options--Compile/Run--Current directory: binaries\

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

Updated:
- renamed 1 example
-- cv_thinning.pb to cv_thinning_1.pb
- added 2 examples
-- cv_analog_clock.pb: creates an analog clock synchronized with the system time
-- cv_thinning_2.pb: a five stage two-dimensional filter applied to a grayscale image producing a skeleton effect

cv_analog_clock.pb: Based on script found here.
cv_thinning_2.pb: Based on script found here.
Last edited by JHPJHP on Fri Sep 12, 2014 5:44 am, edited 2 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

Post by AAT »

Hi applePi, thank you for sharing.

I think a new alarm sound shouldn't be started before a previous sound isn't ended. Isn't it?

Good luck!
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: PureBasic Interface to OpenCV

Post by applePi »

Hi AAT
look at this example from the PB examples but with 5 loops and time

Code: Select all

If InitSound() = 0
  MessageRequester("Error", "Sound system is not available",  0)
  End
EndIf

SoundFileName$ = OpenFileRequester("Choose a .wav file", "", "Wave files|*.wav",0)
If SoundFileName$
  If LoadSound(0, SoundFileName$)
    For i=1 To 5
    PlaySound(0 )
  Sleep_(300)  
  Next
  
  
    MessageRequester("Sound", "Playing the sound (loop)..."+#LF$+"Click to quit..", 0)
  Else
    MessageRequester("Error", "Can't load the sound.", 0)
  EndIf
EndIf
End   
choose the same "Windows Logon Sound.wav" inside windows/media (in win 7 system), it seems every subsequent sounds begins before the previous one ends.
change Sleep_(300) to Sleep_(1000) and we will hear full 5 sounds. change it again to Sleep_(10) and we will hear 1 sounds, seems all the 5 sounds go inside each other.
i have found that the playsound inside the "if tracking" are executed several times when we wave a pencil and that depends on how speedily we wave the pencil. so i think it is better to restrict the sound playing to once by adding a condition flag, this is useful if we want to play loudly a file like " i catch you i catch you" so all the city are awake. i don't know if my cam is faulty since sometimes the program does not respond in the initialization process until i move the cam slightly , or i should install the cam special driver instead of the general windows 7 drivers !!.
i was planning to measure the gravity acceleration with this program using a simple pendulum and the equation T = 2pi*SQRT(L/g) "T is the period ie full swing, L: pendulum lenght, g: the acceleration. i want to use something like this inside the "if tracking":
NumOfHits + 1
If firstTiming=0
time1.f = ElapsedMilliseconds()
firstTiming=1
EndIf

If NumOfHits = 10
time2 = ElapsedMilliseconds()
time2 - time1
cvPutText(*imgAverage, "Timing = " + StrD(time2/5), 20, 40, @font, 0, 0, 255, 0)
cvShowImage(#CV_WINDOW_NAME, *imgAverage)
cvWaitKey(1000)
EndIf

ie every time the pendulum are passing in front the camera it increase NumOfHits by 1, this is done for 10 number of hits which means 5 oscillations.
but the problem that each hit trigger many loops. so i want to find a work around, or to narrow the active area to a thin slot.
the JHPJHP program is a great program even we can detect an earthquake by detecting the picture vibration. too many usages in science and can replace very expensive devices.

PS:since Debug does not work here , and the the messageRequester are not suitable here. i have found OpenConsole are suitable for output for checking and testing
so at the begining
OpenConsole()
and after PlaySound(snd) in the above example
r+1
PrintN(Str(r))

just position the console window in a suitable place, we can see how many times the loop are executed. this way is useful for debugging.
Locked