Page 20 of 60

Re: PureBasic Interface to OpenCV

Posted: Fri Jul 25, 2014 5:51 am
by marc_256
Hello,

I finely have some (hours) free, 8)

So I like to install this software and play with it,
I use Win 8.1 and PB 5.22, PB5.30

What do I need to install, and where to copy ??

thanks,
marc

Re: PureBasic Interface to OpenCV

Posted: Fri Jul 25, 2014 6:10 am
by JHPJHP
Hi marc_256,

"PureBasic Interface to OpenCV" is a complete enclosed package, demonstrated through examples.
- place the downloaded / extracted folder anywhere on your computer
- run the examples from their current location

All 'C' Functions have been imported, and most of the relevant Structures, Constants, and Macros have been declared.
- does not include 'C++' Functions not easily supported by PureBasic

Each example requiring an image has a default one assigned to better demonstrate the effect.
- most examples have a context menu available, opened from a right-mouse click anywhere on the window
- each example has a tooltip giving additional information

NB*: Review the various folders to better understand my approach to this example based interface.

Let me know if you need any additional information, I'm happy to help.

Cheers.

Re: PureBasic Interface to OpenCV

Posted: Fri Jul 25, 2014 6:27 am
by marc_256
Thanks JHPJHP for the quick answer,
I admire you work so, I go playing with it this WE...

marc,

Re: PureBasic Interface to OpenCV

Posted: Sat Jul 26, 2014 7:10 am
by JHPJHP
Updated:
- added 2 examples
-- cv_fractal_1.pb: generates a Mandelbrot Set, the most popular geometrical fractal
-- cv_fractal_2.pb: generates a Julia Set, a popular geometrical fractal

Based on script found here.

After seeing this interesting post by Little John, I decided to script the above two examples.

NB*: "PureBasic Interface to OpenCV" includes many examples that can be rewritten exclusively using PureBasic, without the need for OpenCV.

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

Updated:
- added various small improvements to the new examples
- added an enhancement to the example: cv_seam_carving.pb

PureBasic Interface to OpenCV

Posted: Sun Jul 27, 2014 1:21 pm
by JHPJHP
Hi bbanelli,

I read your post, thank you for the acknowledgement.

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

Updated:
- renamed 1 example
-- cv_transparent.pb to cv_transparent_1.pb
- added 1 example
-- cv_transparent_2.pb: using the watershed algorithm, an object can be extracted and saved to a transparent PNG file

The new example may be of some use if you're interested in or have the need for a GrabCut function.

Instructions:
1. draw a rough connected outline within (not around) Lena
2. draw a single line to the left of Lena
3. draw a single line to the right of Lena
4. draw a single line above Lena
5. press the Spacebar
-- you should now have a pretty accurate area to extract, viewable in the right window
-- you can continue to block off more objects, or move to the next step
6. double-click the area of Lena in the left window, that matches the blocked off area in the right window you want extracted
-- you should now see the extracted object surrounded in black
-- you can continue to double click more areas to extract more objects
-- touchup the image using the black-brush now available
-- you can press the Spacebar to return back before Step 5 (lose any touchups)
-- you can press the Enter key to reset both windows
7. right-mouse-click on the window, choose Save from the context menu
-- must be saved as a PNG file (default) for transparent background

Tip*: If part of an object you want extracted is not being cooperative, because it lies in the same shadow as another object, or something similar. Outline the other object, then draw a line from the area you want included, connecting to the object you want extracted.

Re: PureBasic Interface to OpenCV

Posted: Wed Jul 30, 2014 7:31 am
by JHPJHP
Updated:
- added 1 example
-- cv_vignette.pb: applies the Vignette effect, the process by which there is loss in clarity towards the corners and sides of an image
- added 2 images

Based on script found here.

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

Updated cv_transparent_2.pb:
- included transparency on the second image when choosing objects
- added a smoothing algorithm to the extracted objects

NB*: Update also includes various script changes to multiple examples.

PureBasic Interface to OpenCV

Posted: Mon Aug 04, 2014 6:34 am
by JHPJHP
I did a rewrite of the code for the example: cv_seam_carving.pb:
~ "Deleting vertical seams in an image by calculating the intensity of color pixels (experimental)."

Updated cv_seam_carving.pb:
- added the ability to select an exclusion area
- added the ability to modify the aperture size of the Sobel function
- added the ability to modify the x/y order of the Sobel function
- added the ability to resize the image to the original width after seam carving completes

Instructions:
1. highlight an area around the person looking at the castle
2. highlight an area around the castle
- use the 'A' key to modify the aperture size of the Sobel function
- use the 'S' key to modify the x/y order of the Sobel function
3. press the Spacebar to initiate Seam-Carving
4. press the Spacebar (after Seam-Carving completes) to resize the image

Re: PureBasic Interface to OpenCV

Posted: Thu Aug 07, 2014 11:06 pm
by JHPJHP
Updated:
- renamed 2 example
-- cv_overlay.pb to cv_overlay_1.pb
-- cv_seam_carving.pb to cv_seam_carving_1.pb
- added 4 examples
-- cv_overlay_2.pb: apply a perspective transform calculated from four pairs of points to an image, overlayed onto another image.
-- cv_seam_carving_2.pb: deleting vertical seams in an image by calculating the intensity of gray-scale pixels (experimental)
-- cv_saliency.pb: a fine-grained saliency algorithm is applied to an image
-- cv_lomo.pb: simulates the LOMO (Leningrad Optical Mechanical Association) camera lens effect
- deleted 1 image
- added 3 images

cv_saliency.pb: based on script found here.
cv_lomo.pb: based on script found here.

cv_overlay_2.pb: I added a basic algorithm to reorder x/y points. If you mark a four sided shape to match a track section it should display the overlay correctly, with any other configuration the image may be distorted.
- after setting 4 points and pressing the Spacebar to apply the overlay the process can be repeated

I was reading up on the different techniques used to perform seam carving, and saliency was mentioned as a good choice. But when I applied it to a new example the results didn't reflect what I expected; the script was sound so I created a standalone saliency example. The second seam carving example uses a single channel gradient as opposed to the color one used in the first example.

While reading about different image manipulation techniques I came across LOMO, and seeing that my strongest supporter for a "PureBasic Interface to OpenCV" is from Russia... :wink:

Re: PureBasic Interface to OpenCV

Posted: Fri Aug 08, 2014 4:37 am
by AAT
Hi, JHPJHP!
Glad to test your new examples. Thanks!
While reading about different image manipulation techniques I came across LOMO, and seeing that my strongest supporter for a "PureBasic Interface to OpenCV" is from Russia... :wink:
You can to google about "OpenCV" project: Intel, Willow Garage, NSTL (Nizhniy Software Laboratory), Itseez. :)

LOMO, lomography (http://en.wikipedia.org/wiki/Lomography) is too modern technique.
I took my first b/w photos by "Smena" (it means "young or new generation") from my uncle. It was very cool! :D

Image

Good luck!

Re: PureBasic Interface to OpenCV

Posted: Sat Aug 09, 2014 6:32 pm
by JHPJHP
Hi AAT,

Thank you again for being a part of this, I would have ended this project awhile ago if not for your interest and the support of a few others.

"OpenCV" project: Intel, Willow Garage, NSTL (Nizhniy Software Laboratory), Itseez"
- very cool, this may be something marc_256 also finds interesting
-- have you checked out his site: http://www.marc-systems.be/new_2010/hob ... _deel1.htm
I took my first b/w photos by "Smena" (it means "young or new generation") from my uncle. It was very cool!
I have a feeling we're around the same age, the image of the camera you uploaded reminds me of days long past.

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

Updated:
- added 2 examples
-- cv_mov_runningavg.pb_1: calculates the weighted sum of the input image and the accumulator creating a silhouette effect used to detect objects of a certain size
-- cv_mov_runningavg.pb_2: calculates the weighted sum of the input image and the accumulator so that the new image becomes a running average of a frame sequence

cv_mov_runningavg.pb_1 is based on script found here.
cv_mov_runningavg.pb_2 is based on script found here.

The nice thing about these examples is that they run similar to [ le_mov_bgstatmodel_1.pb ] and [ le_mov_bgstatmodel_2.pb ], but are not restricted by legacy requirements.

NB*: The new examples use a function not previously found in any other example: cvRunningAvg.

Re: PureBasic Interface to OpenCV

Posted: Sun Aug 10, 2014 12:58 am
by Mythros
You're welcome for getting you started on the idea of OpenCV. >< LOL!

Re: PureBasic Interface to OpenCV

Posted: Sun Aug 10, 2014 10:16 am
by marc_256
hi JHPJHP,

I like to detect edges from doors and windows in hallway using camera image,
is this possible ?

So I can detect openings in the walls to pass ...

PS: I admire your work, very huge project ... thanks for your work.

marc,

Re: PureBasic Interface to OpenCV

Posted: Sun Aug 10, 2014 11:23 am
by JHPJHP
Hi marc_256,

Thank you for your kind words.
detect edges from doors and windows in hallway using camera image
Here are a few places to start:
- cv_approxpoly_1.pb
- cv_cam_goodfeatures.pb
- cv_contours_1.pb
- cv_cornerharris_2.pb
- cv_find_shapes.pb
- cv_find_squares.pb
- cv_houghlines_1.pb
- cv_precornerdetect_2.pb

You will need to combined some of the above techniques (and possibly others) with some added filters to achieve the desired results.
- here are a few questions you may need to ask (finer points)
-- is the camera stationary, scanning, moving
-- what is the distance of the hallway relative to the size of object to be scanned
-- do angles / perspective need to be considered (squares / rectangles)
-- could there be obstructions blocking partial view, open doors

NB*: If you put together some code and would like me to look at it - I'd be happy to help.

Re: PureBasic Interface to OpenCV

Posted: Sun Aug 10, 2014 1:20 pm
by AAT
Hi JHPJHP!
JHPJHP wrote: - added 2 examples
-- cv_mov_runningavg.pb_1: calculates the weighted sum of the input image and the accumulator creating a silhouette effect used to detect objects of a certain size
-- cv_mov_runningavg.pb_2: calculates the weighted sum of the input image and the accumulator so that the new image becomes a running average of a frame sequence
Thank you for your excellent examples!

The cv_mov_runningavg.pb_1 with minimal additions will give a guard system with alarm zones based on a webcam.
----------------------------
Update:
the code is changed under JHPJHP edition

Code: Select all

IncludeFile "includes/cv_functions.pbi"

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 used to detect objects of a certain size." + Chr(10) + Chr(10) +
                  "- [ Z ] KEY: Toggle alarm zone (green rectangle)" + Chr(10) + Chr(10) +
                  "- SPACEBAR: Toggle between foreground/background view." + Chr(10) + Chr(10) +
                  "- [ V ] KEY: Change PIP view."

Structure zone
  xbeg.l
  xend.l
  ybeg.l
  yend.l
EndStructure

Dim guardzone.zone(8)

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)
  
; 9 alarm zones  released
  zoneW = FrameWidth/3
  zoneH = FrameHeight/3 
  zoneN = 4                   ; start zone  
  For zoney = 0 To 2
    For zonex = 0 To 2
      xy = zoney*3 + zoneX
      guardzone(xy)\xbeg = zonex * zoneW
      guardzone(xy)\xend = guardzone(xy)\xbeg + zoneW - 1
      guardzone(xy)\ybeg = zoney * zoneH
      guardzone(xy)\yend = guardzone(xy)\ybeg + zoneH - 1      
    Next zonex    
  Next zoney
    
  INIT_TIME = 50
  BG_RATIO.d = 0.02
  OBJ_RATIO.d = 0.005
  Zeta.d = 10
  *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)

  For rtnCount = 0 To INIT_TIME
    *image = cvQueryFrame(*capture)
  
    If *image : 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
      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)
  *storage.CvMemStorage = cvCreateMemStorage(0)
  *contours.CvContour
  *param.USER_INFO = AllocateMemory(SizeOf(USER_INFO))
  *param\uValue = window_handle
  cvSetMouseCallback(*window_name, @CvMouseCallback(), *param)

  Repeat
    *image = cvQueryFrame(*capture)

    If *image
      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, #Null, 1)
      cvDilate(*imgSilhouette, *imgSilhouette, #Null, 2)
      cvErode(*imgSilhouette, *imgSilhouette, #Null, 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)
      
      cvRectangleR(*image, guardzone(zoneN)\xbeg, guardzone(zoneN)\ybeg, zoneW-1, zoneH-1, 0, 255, 0, 0, 1, #CV_AA, #Null) 
      
      If nContours
        For rtnCount = 0 To nContours - 1
          area.d = cvContourArea(*contours, 0, #CV_WHOLE_SEQ_END_INDEX, 0)
          If area >= 400 And area <= 100000            
            If (*contours\rect\x + *contours\rect\width/2) => guardzone(zoneN)\xbeg And
               (*contours\rect\x + *contours\rect\width/2) <= guardzone(zoneN)\xend And
               (*contours\rect\y + *contours\rect\height/2) => guardzone(zoneN)\ybeg And
               (*contours\rect\y + *contours\rect\height/2) <= guardzone(zoneN)\yend                     
                cvDrawContours(*imgResult, *contours, 255, 155, 0, 0, 155, 255, 0, 0, -1, 2, #CV_AA, 0, 0)
                cvRectangleR(*image, *contours\rect\x, *contours\rect\y, *contours\rect\width, *contours\rect\height, 0, 0, 255, 0, 2, #CV_AA, #Null)            
            EndIf             
          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 86, 118
            PIP = (PIP + 1) % 3
          Case 90, 122          
            zoneN = (zoneN + 1) % 9 
        EndSelect
      EndIf
    Else
      Break
    EndIf
  Until keyPressed = 27 Or exitCV
  FreeMemory(*param)
  cvReleaseMemStorage(@*storage)
  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
Good luck!

Re: PureBasic Interface to OpenCV

Posted: Mon Aug 11, 2014 9:03 am
by JHPJHP
Hi AAT,

Fantastic work... that's exactly why I decided on an example based interface; to assist with finding a practical use. I realize that you and I have the added benefit of seeing this from the beginning, but I believe anyone with a little time and some interest could really push the limits.

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

Updated:
- added 1 example (jointly contributed by AAT)
-- cv_cam_zone_tracking.pb: 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

If there is a question on why a zone is highlighted when it seems that nothing has passed through that area, be sure to turn on the tracking option to view the bounding rectangles.

NB*: Not sure if anyone noticed, but I added a toggle PIP window option to all relevant examples.