PureBasic Interface to OpenCV

Developed or developing a new product in PureBasic? Tell the world about it.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi, bbanelli
bbanelli wrote: Namely, I wanted to have multiple display sources in one window, something like this.
If you use multiple webcams you will get troubles with an USB passband.
...In addition to that, is there a better way to handle resize of picture, since capture is 640x480 and I'd like to display 320x240 images (or any other resolution)? Image size must be 640x480 since I use CopyMemory and if resolution is changed, there's a memory violation.
You can use ROI like in the JHPJHP's example "cv_cam_split_merge.pb":

Code: Select all

IncludeFile "includes/cv_functions.pbi"

Global lpPrevWndFunc

#CV_WINDOW_NAME = "PureBasic Interface to OpenCV"
#CV_DESCRIPTION = "Merge 4 images to 1 with ROI"

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) / 2
  FrameHeight = cvGetCaptureProperty(*capture, #CV_CAP_PROP_FRAME_HEIGHT) / 2

  *image.IplImage  
  *dst.IplImage = cvCreateImage(FrameWidth * 2, FrameHeight * 2, #IPL_DEPTH_8U, 3)  
  *img1.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
  *img2.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
  *img3.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
  *img4.IplImage = cvCreateImage(FrameWidth, FrameHeight, #IPL_DEPTH_8U, 3)
  
  *param.USER_INFO = AllocateMemory(SizeOf(USER_INFO))
  *param\uValue = window_handle
  cvSetMouseCallback(*window_name, @CvMouseCallback(), *param)

  Repeat
    *image = cvQueryFrame(*capture)

    If *image
      cvResize(*image, *img1, #CV_INTER_AREA)   
      cvFlip(*img1, *img2, -1)
      cvFlip(*img1, *img3, 0)
      cvFlip(*img1, *img4, 1)

      cvSetImageROI(*dst, 0, 0, FrameWidth, FrameHeight)
      cvAndS(*img1, 0, 0, 0, 0, *dst, #Null)
      cvAdd(*dst, *img1, *dst, #Null)
      cvResetImageROI(*dst)
      
      cvSetImageROI(*dst, FrameWidth, 0, FrameWidth, FrameHeight)
      cvAndS(*img2, 0, 0, 0, 0, *dst, #Null)
      cvAdd(*dst, *img2, *dst, #Null)
      cvResetImageROI(*dst)
      
      cvSetImageROI(*dst, FrameWidth, FrameHeight, FrameWidth, FrameHeight)
      cvAndS(*img3, 0, 0, 0, 0, *dst, #Null)
      cvAdd(*dst, *img3, *dst, #Null)
      cvResetImageROI(*dst)
      
      cvSetImageROI(*dst, 0, FrameHeight, FrameWidth, FrameHeight)
      cvAndS(*img4, 0, 0, 0, 0, *dst, #Null)
      cvAdd(*dst, *img4, *dst, #Null)      
      cvResetImageROI(*dst)
      
      cvShowImage(#CV_WINDOW_NAME, *dst)
      keyPressed = cvWaitKey(10)

    EndIf
  Until keyPressed = 27 Or exitCV
  FreeMemory(*param)
  cvReleaseImage(@*image)
  cvReleaseImage(@*dst)
  cvReleaseImage(@*img1)
  cvReleaseImage(@*img2)
  cvReleaseImage(@*img3)
  cvReleaseImage(@*img4)
  cvDestroyWindow(#CV_WINDOW_NAME)
  cvReleaseCapture(@*capture)
Else
  MessageBox_(0, "Unable to connect to a webcam - operation cancelled.", #CV_WINDOW_NAME, #MB_ICONERROR)
EndIf
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi, bbanelli!
About threads:
try to setup Intel TBB https://www.threadingbuildingblocks.org/ and build your variant of OpenCV libraries with TBB.

http://docs.opencv.org/doc/tutorials/in ... stall.html
Last edited by AAT on Sat May 09, 2015 3:34 pm, edited 1 time in total.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PureBasic Interface to OpenCV

Post by bbanelli »

AAT wrote:Hi, bbanelli
bbanelli wrote: Namely, I wanted to have multiple display sources in one window, something like this.
If you use multiple webcams you will get troubles with an USB passband.
Hi AAT, first of all, thank you for your reply!

So far, I've been using 3 cameras and that's about how much I need - no problems, luckily! :)
You can use ROI like in the JHPJHP's example "cv_cam_split_merge.pb":
This seems to be exactly what I need without any particular issues with threads! Thank you so much!

https://youtu.be/9M9w2WXN0W0

I will read what you've sent on links about threads, thanks, I've never worked with that tool before!
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PureBasic Interface to OpenCV

Post by bbanelli »

I've managed to recompile, it was rather easy, x86 version works like a charm with TBB! Thanks a lot AAT!

However, I tried making x64 version, but most examples throw errors like:

Code: Select all

Problem signature:
  Problem Event Name:	APPCRASH
  Application Name:	PureBasic_Compilation0.exe
  Application Version:	0.0.0.0
  Application Timestamp:	5550b023
  Fault Module Name:	KERNELBASE.dll
  Fault Module Version:	6.1.7601.18839
  Fault Module Timestamp:	553e8c17
  Exception Code:	e06d7363
  Exception Offset:	000000000001aaad
  OS Version:	6.1.7601.2.1.0.256.48
  Locale ID:	1050
  Additional Information 1:	94d0
  Additional Information 2:	94d0c2b578f17e42ec3729d3db856c2c
  Additional Information 3:	8d02
  Additional Information 4:	8d02aa58e0bbaed907e20891d9b93d9c
OTOH, pb_gl_cam_cube.pb, pb_gl_parametric_curve.pb, cv_thinning_1.pb, cv_smooth_edges_1.pb, cv_morphology.pb and some others work fine.

Any clue what could be wrong? Does anything needs to be changed include files for x64 version?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi, bbanelli!

JHPJHP doesn't recommend to use the 64-bit version of the OpenCV.
At the very beginning of the project problems were found and not all worked correctly.
I can't explain you why, sorry.

Good luck!
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PureBasic Interface to OpenCV

Post by bbanelli »

AAT wrote:Hi, bbanelli!

JHPJHP doesn't recommend to use the 64-bit version of the OpenCV.
At the very beginning of the project problems were found and not all worked correctly.
I can't explain you why, sorry.

Good luck!
I was being merely curious anyway! :) Thanks for the explanation and your help regarding threads. ;)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi AAT,

Thank you again for the PM, it's motivated me to at least read some of the posts I've missed:
bbanelli wrote:However, I tried making x64 version, but most examples throw errors
...
I was being merely curious anyway!
AAT wrote:JHPJHP doesn't recommend to use the 64-bit version of the OpenCV.
I've started a 64bit version of "PureBasic Interface to OpenCV"

It may be some time before anything is released due to the tediousness of the task.
- requires a modified declaration of the Functions, closer to the documented version
- each example will need to be updated to reflect the changes to the Functions
- unforeseen changes to other areas of the interface are probable
Last edited by JHPJHP on Sun May 17, 2015 5:57 pm, edited 3 times in total.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PureBasic Interface to OpenCV

Post by bbanelli »

JHPJHP wrote:If there is a need for a 64bit version of the interface I can post the results, otherwise it may be some time before anything is released due to the tediousness of the task.
- requires a modified declaration of the Functions, closer to the documented version
- each example will need to be updated to reflect the changes to the Functions
- unforeseen changes to other areas of the interface are probable
Greetings JHPJHP,

could you be a bit more verbose about those changes and perhaps give some hint, I am far from being perfect but would be willing to assist within my knowledge. :)

BTW, will OCV 3.0 support only C++ or there will be C support as well?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Hi bbanelli,

Thank you for the offer to help create a 64bit version of "PureBasic Interface to OpenCV", but currently I'm processing the changes by example. While some of the changes are repetitive, currently it only makes sense to modify the functions individually.

OpenCV 3.0 supports C, but instead of having a number of DLLs a single "world" DLL hosts most of the available functions.
- current version is 3.0 RC1
- I don't think the legacy DLL was carried over
- I'm not sure if additional Functions have been moved to legacy
Last edited by JHPJHP on Sun May 17, 2015 5:54 pm, edited 3 times in total.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: PureBasic Interface to OpenCV

Post by AAT »

Hi JHPJHP!
You are here again and i'm glad to read your posts! :D
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: PureBasic Interface to OpenCV

Post by JHPJHP »

Thank you AAT, that was kind of you to say.

-----------------------------------------------------------
JHPJHP wrote:I've started a 64bit version of "PureBasic Interface to OpenCV"
I've now converted 100 examples from "PureBasic Interface to OpenCV" to work in a 64bit environment, and should have the last 100 completed in approx. 2 weeks.

While converting and testing each example I've notice and corrected several errors in the original package, which will be updated when I post the 64bit version.

NB*: This will be a completely separate package; download not yet available.
sphinx
Enthusiast
Enthusiast
Posts: 120
Joined: Thu Oct 26, 2006 6:34 pm
Contact:

Re: PureBasic Interface to OpenCV

Post by sphinx »

Great, thanks JHPJHP
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: PureBasic Interface to OpenCV

Post by chris319 »

Here is an enhanced version of the webcam control program.

It uses constants to set the horizontal and vertical resolutions, making it possible to have 16:9 images. The resolutions were previously hard coded.

There is an oscilloscope-like display to check video levels. I'm not sure why there are black horizontal lines in the scope display.

There are graticules at 254, 235, 16 and 1. In Rec.709, values of 0 and 255 are forbidden; values between 16 and 235 are known as "studio swing".

It may yet be a little buggy. It sometimes crashes upon closing -- not sure why.

Code: Select all

IncludeFile "includes/cv_functions.pbi"
InitKeyboard():InitSprite()
#WIDTH =1280/2 ;1920/2
#HEIGHT =720/2 ;1080/2
#KR = 0.2126:#KG = 0.7152:#KB = 0.0722 ;REC.709 COEFFICIENTS
#SCOPE_Y_OFFSET = 275
Dim scopeLum(#HEIGHT*2,#WIDTH*2)
Enumeration 20
  #gadBrightness
  #gadContrast    
  #gadGamma
  #gadSaturation
  #gadHue
  #gadSharpness
  #gadExposure
  #scopeGadget
  #gadBright2
  #gadCont2
EndEnumeration

    Global *capture
    Global DefBrightness.d, MaxBrightness.d, MinBrightness.d, DefContrast.d, MaxContrast.d, MinContrast.d
    Global DefGamma.d, MaxGamma.d, MinGamma.d, DefSaturation.d, MaxSaturation.d, MinSaturation.d
    Global DefHue.d, MaxHue.d, MinHue.d, DefSharpness.d, MaxSharpness.d, MinSharpness.d
    Global DefExposure.d, MaxExposure.d, MinExposure.d

#CV_WINDOW_NAME = "WebCam Settings"
#CamDataName = "camdata.dat"

Procedure ReadCamData()
      If OpenPreferences(GetCurrentDirectory() + #CamDataName)
        DefBrightness = ReadPreferenceLong("DefBrightness", 1)
        DefContrast =  ReadPreferenceLong("DefContrast", 1)
        DefGamma = ReadPreferenceLong("DefGamma", 1)
        DefSaturation = ReadPreferenceLong("DefSaturation", 1)
        DefHue = ReadPreferenceLong("DefHue", 1)
        DefSharpness = ReadPreferenceLong("DefSharpness", 1)
        DefExposure = ReadPreferenceLong("DefExposure", 1)
       
        MaxBrightness = ReadPreferenceLong("MaxBrightness", 1)
        MaxContrast = ReadPreferenceLong("MaxContrast", 1)
        MaxGamma = ReadPreferenceLong("MaxGamma", 1)
        MaxSaturation = ReadPreferenceLong("MaxSaturation", 1)
        MaxHue = ReadPreferenceLong("MaxHue", 1)
        MaxSharpness = ReadPreferenceLong("MaxSharpness", 1)
       
        MinBrightness = ReadPreferenceLong("MinBrightness", 1)
        MinContrast =  ReadPreferenceLong("MinContrast", 1)
        MinGamma = ReadPreferenceLong("MinGamma", 1)
        MinSaturation = ReadPreferenceLong("MinSaturation", 1)
        MinHue = ReadPreferenceLong("MinHue", 1)
        MinSharpness = ReadPreferenceLong("MinSharpness", 1)
        MaxExposure = ReadPreferenceLong("MaxExposure", 1)
        MinExposure = ReadPreferenceLong("MinExposure", 1)   
       
cvSetCaptureProperty(*capture, #CV_CAP_PROP_BRIGHTNESS, DefBrightness)
cvSetCaptureProperty(*capture, #CV_CAP_PROP_CONTRAST, DefContrast)
cvSetCaptureProperty(*capture, #CV_CAP_PROP_GAMMA, DefGamma)
cvSetCaptureProperty(*capture, #CV_CAP_PROP_SATURATION, DefSaturation)
cvSetCaptureProperty(*capture, #CV_CAP_PROP_HUE, DefHue)
cvSetCaptureProperty(*capture, #CV_CAP_PROP_SHARPNESS, DefSharpness)
;THERE IS A BUG HERE cvSetCaptureProperty(*capture, #CV_CAP_PROP_EXPOSURE, DefExposure)
      Else   
        CreatePreferences(GetCurrentDirectory()+"camdata.dat")
        retvalue.d = 0
        value.d = 0
        DefValue.d
       
    ;{ #CV_CAP_PROP_BRIGHTNESS   
        DefValue = cvGetCaptureProperty(*capture,#CV_CAP_PROP_BRIGHTNESS)
        WritePreferenceLong("DefBrightness", DefValue)
        DefBrightness = DefValue
        For k=DefValue To 1000 Step 1
          value = k
          cvSetCaptureProperty(*capture, #CV_CAP_PROP_BRIGHTNESS, value)   
          retvalue=cvGetCaptureProperty(*capture, #CV_CAP_PROP_BRIGHTNESS)     
          If value > retvalue
            Break
          EndIf
        Next     
        WritePreferenceLong("MaxBrightness", cvGetCaptureProperty(*capture,#CV_CAP_PROP_BRIGHTNESS))   
        MaxBrightness = retvalue
        For k=DefValue To 0 Step -1
          value = k
          cvSetCaptureProperty(*capture, #CV_CAP_PROP_BRIGHTNESS, value)   
          retvalue=cvGetCaptureProperty(*capture, #CV_CAP_PROP_BRIGHTNESS)     
          If value > retvalue
            Break
          EndIf
        Next
        WritePreferenceLong("MinBrightness", cvGetCaptureProperty(*capture,#CV_CAP_PROP_BRIGHTNESS))
        MinBrightness = retvalue
        cvSetCaptureProperty(*capture, #CV_CAP_PROP_BRIGHTNESS, DefValue)   

    ;{ #CV_CAP_PROP_CONTRAST
        DefValue = cvGetCaptureProperty(*capture,#CV_CAP_PROP_CONTRAST) 
        WritePreferenceLong("DefContrast", DefValue)
        DefContrast = DefValue
        For k=DefValue To 1000 Step 1
          value = k
          cvSetCaptureProperty(*capture, #CV_CAP_PROP_CONTRAST, value)   
          retvalue=cvGetCaptureProperty(*capture, #CV_CAP_PROP_CONTRAST)     
          If value > retvalue
            Break
          EndIf
        Next   
        WritePreferenceLong("MaxContrast", cvGetCaptureProperty(*capture,#CV_CAP_PROP_CONTRAST))
        MaxContrast = retvalue
        For k=DefValue To 0 Step -1
          value = k
          cvSetCaptureProperty(*capture, #CV_CAP_PROP_CONTRAST, value)   
          retvalue=cvGetCaptureProperty(*capture, #CV_CAP_PROP_CONTRAST)     
          If value < retvalue
            Break
          EndIf
        Next   
        WritePreferenceLong("MinContrast", cvGetCaptureProperty(*capture,#CV_CAP_PROP_CONTRAST))
        MinContrast = retvalue
        cvSetCaptureProperty(*capture, #CV_CAP_PROP_CONTRAST, DefValue)
    ;}   
       
    ;{ #CV_CAP_PROP_GAMMA   
        DefValue = cvGetCaptureProperty(*capture,#CV_CAP_PROP_GAMMA)
        WritePreferenceLong("DefGamma", DefValue)
        DefGamma = DefValue
        For k = DefValue To 1000 Step 5
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_GAMMA, value)   
          retvalue=cvGetCaptureProperty(*capture, #CV_CAP_PROP_GAMMA)     
          If value > retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MaxGamma", cvGetCaptureProperty(*capture,#CV_CAP_PROP_GAMMA))
        MaxGamma = retvalue   
        For k = DefValue To 0 Step -5
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_GAMMA, value)   
          retvalue=cvGetCaptureProperty(*capture, #CV_CAP_PROP_GAMMA)     
          If value < retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MinGamma", cvGetCaptureProperty(*capture,#CV_CAP_PROP_GAMMA))
        MinGamma = retvalue
        cvSetCaptureProperty(*capture, #CV_CAP_PROP_GAMMA, DefValue)
    
       
    ;{  #CV_CAP_PROP_SATURATION 
        DefValue = cvGetCaptureProperty(*capture,#CV_CAP_PROP_SATURATION)
        WritePreferenceLong("DefSaturation", DefValue)
        DefSaturation = DefValue
        For k = DefValue To 1000 Step 1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_SATURATION, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_SATURATION)
          If value > retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MaxSaturation" , cvGetCaptureProperty(*capture,#CV_CAP_PROP_SATURATION))
        MaxSaturation = retvalue
        For k = DefValue To 0 Step -1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_SATURATION, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_SATURATION)     
          If value < retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MinSaturation", cvGetCaptureProperty(*capture,#CV_CAP_PROP_SATURATION))
        MinSaturation = retvalue
        cvSetCaptureProperty(*capture, #CV_CAP_PROP_SATURATION, DefValue)
           
    ;{  #CV_CAP_PROP_HUE 
        DefValue = cvGetCaptureProperty(*capture,#CV_CAP_PROP_HUE)
        WritePreferenceLong("DefHue", DefValue)
        DefHue = DefValue
        For k = DefValue To 1000 Step 1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_HUE, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_HUE)
          If value > retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MaxHue", cvGetCaptureProperty(*capture,#CV_CAP_PROP_HUE))
        MaxHue = retvalue
        For k = DefValue To 0 Step -1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_HUE, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_HUE)
          If value < retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MinHue", cvGetCaptureProperty(*capture,#CV_CAP_PROP_HUE))
        MinHue = retvalue
        cvSetCaptureProperty(*capture, #CV_CAP_PROP_HUE, DefValue)
                 
    ;{  #CV_CAP_PROP_SHARPNESS
        DefValue = cvGetCaptureProperty(*capture,#CV_CAP_PROP_SHARPNESS)
        WritePreferenceLong("DefSharpness", DefValue)
        DefSharpness = DefValue
        For k = DefValue To 1000 Step 1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_SHARPNESS, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_SHARPNESS)
          If value > retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MaxSharpness", cvGetCaptureProperty(*capture,#CV_CAP_PROP_SHARPNESS))
        MaxSharpness = retvalue
        For k = DefValue To 0 Step -1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_SHARPNESS, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_SHARPNESS)
          If value < retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MinSharpness", cvGetCaptureProperty(*capture,#CV_CAP_PROP_SHARPNESS))
        MinSharpness = retvalue
        cvSetCaptureProperty(*capture, #CV_CAP_PROP_SHARPNESS, DefValue)
    
    ;{  #CV_CAP_PROP_EXPOSURE
        DefValue = cvGetCaptureProperty(*capture,#CV_CAP_PROP_EXPOSURE)
        WritePreferenceLong("DefExposure", DefValue)
        DefExposure = DefValue
        For k = DefValue To 1000 Step 1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_EXPOSURE, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_EXPOSURE)
          If value > retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MaxExposure", cvGetCaptureProperty(*capture,#CV_CAP_PROP_EXPOSURE))
        MaxExposure = retvalue
        For k = DefValue To 0 Step -1
          value = k
          cvSetCaptureProperty(*capture,#CV_CAP_PROP_EXPOSURE, value)   
          retvalue = cvGetCaptureProperty(*capture, #CV_CAP_PROP_EXPOSURE)
          If value < retvalue
            Break
          EndIf     
        Next
        WritePreferenceLong("MinExposure", cvGetCaptureProperty(*capture,#CV_CAP_PROP_EXPOSURE))
        MinExposure = retvalue
        cvSetCaptureProperty(*capture, #CV_CAP_PROP_EXPOSURE, DefValue)
        CloseWindow(1)
    
      EndIf 
    EndProcedure

    Procedure Spin20Handler();BRIGHTNESS
      Shared window_handle 
      val.d = GetGadgetState(20)
      cvSetCaptureProperty(*capture,#CV_CAP_PROP_BRIGHTNESS, val)
      WritePreferenceLong("DefBrightness", val)
    EndProcedure

    Procedure Spin21Handler();CONTRAST
      Shared window_handle 
      val.d = GetGadgetState(21)
      cvSetCaptureProperty(*capture,#CV_CAP_PROP_CONTRAST, val)
      WritePreferenceLong("DefContrast", val) 
    EndProcedure

    Procedure Spin22Handler();GAMMA
      Shared window_handle 
      val.d = GetGadgetState(22)
      cvSetCaptureProperty(*capture,#CV_CAP_PROP_GAMMA, val)
      WritePreferenceLong("DefGamma", val)
    EndProcedure

    Procedure Spin23Handler()
      Shared window_handle 
      val.d = GetGadgetState(23);SATURATION
      cvSetCaptureProperty(*capture,#CV_CAP_PROP_SATURATION, val)
      WritePreferenceLong("DefSaturation", val)
    EndProcedure

    Procedure Spin24Handler();HUE
      Shared window_handle 
      val.d = GetGadgetState(24)
      cvSetCaptureProperty(*capture,#CV_CAP_PROP_HUE, val)
      WritePreferenceLong("DefHue", val)
    EndProcedure

    Procedure Spin25Handler();SHARPNESS
      Shared window_handle 
      val.d = GetGadgetState(25)
      cvSetCaptureProperty(*capture,#CV_CAP_PROP_SHARPNESS, val)
      WritePreferenceLong("DefSharpness", val)
    EndProcedure

    Procedure Spin26Handler();EXPOSURE
      Shared window_handle 
      val.d = GetGadgetState(26)
      cvSetCaptureProperty(*capture,#CV_CAP_PROP_EXPOSURE, val)
      WritePreferenceLong("DefExposure", val)
    EndProcedure

    Procedure Spin27Handler();BRIGHT2
;       Shared window_handle 
;       val.d = GetGadgetState(#gadBright2)
;        cvSetCaptureProperty(*capture,#CV_CAP_PROP_BRIGHTNESS, val)
;        WritePreferenceLong("DefBrightness", val)
    EndProcedure

    Procedure Spin28Handler();CONT2
;      Shared window_handle 
;       val.d = GetGadgetState(#gadCont2)
;       cvSetCaptureProperty(*capture,#CV_CAP_PROP_CONTRAST, val)
;       WritePreferenceLong("DefContrast", val) 
     EndProcedure

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

    If *capture
      *image.IplImage
      *edges.IplImage
      cvNamedWindow(#CV_WINDOW_NAME, #CV_WINDOW_NORMAL)
      ;cvMoveWindow(#CV_WINDOW_NAME, -1000, -1000)
      cvResizeWindow(#CV_WINDOW_NAME, #WIDTH, #HEIGHT) 
     window_handle = cvGetWindowHandle(#CV_WINDOW_NAME)
      hWnd = GetParent_(window_handle)
      ShowWindow_(hWnd, #SW_HIDE)
      ReadCamData()   
      cvSetCaptureProperty(*capture, #CV_CAP_PROP_FRAME_WIDTH,#WIDTH)
      cvSetCaptureProperty(*capture, #CV_CAP_PROP_FRAME_HEIGHT,#HEIGHT)
     
If OpenWindow(0, 0, 0, #WIDTH+200, #HEIGHT*2, #CV_WINDOW_NAME, #PB_Window_SystemMenu)
;OpenWindowedScreen(WindowID(0), 0, 0, #WIDTH+200, #HEIGHT*2)
        SetParent_(window_handle, WindowID(0))     
        TextGadget(10, 670, 25, 75, 20, "Brightness", #PB_Text_Right)
        TextGadget(11, 670, 50, 75, 20, "Contrast", #PB_Text_Right)
        TextGadget(12, 670, 75, 75, 20, "Gamma", #PB_Text_Right)
        TextGadget(13, 670, 100, 75, 20, "Saturation", #PB_Text_Right)
        TextGadget(14, 670, 125, 75, 20, "Hue", #PB_Text_Right)
        TextGadget(15, 670, 150, 75, 20, "Sharpness", #PB_Text_Right)
        TextGadget(16, 670, 175, 75, 20, "Exposure", #PB_Text_Right)
        ;TextGadget(17, 670, 405, 75, 20, "Bright")
        ;TextGadget(18, 670, 405, 75, 20, "Cont")
AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape) ;"ESCAPE" KEY TO QUIT PROGRAM
             
        SpinGadget(#gadBrightness, 750, 20, 50, 20, MinBrightness, MaxBrightness, #PB_Spin_Numeric) : SetGadgetState(20, DefBrightness)
        SpinGadget(#gadContrast, 750, 45, 50, 20, MinContrast, MaxContrast, #PB_Spin_Numeric) : SetGadgetState(21, DefContrast)
        SpinGadget(#gadGamma, 750, 70, 50, 20, MinGamma, MaxGamma, #PB_Spin_Numeric) : SetGadgetState(22, DefGamma)
        SpinGadget(#gadSaturation, 750, 95, 50, 20, MinSaturation, MaxSaturation, #PB_Spin_Numeric) : SetGadgetState(23, DefSaturation)
        SpinGadget(#gadHue, 750, 120, 50, 20, MinHue, MaxHue, #PB_Spin_Numeric) : SetGadgetState(24, DefHue)
        SpinGadget(#gadSharpness, 750, 145, 50, 20, MinSharpness, MaxSharpness, #PB_Spin_Numeric) : SetGadgetState(25, DefSharpness)
        SpinGadget(#gadExposure, 750, 170, 50, 20, MinExposure, MaxExposure, #PB_Spin_Numeric) : SetGadgetState(26, DefExposure)
;        TrackBarGadget(#gadBright2, 650,200,30,200,0,255,#PB_TrackBar_Vertical)
;        TrackBarGadget(#gadCont2, 700,200,30,200,0,255,#PB_TrackBar_Vertical)
   
        BindGadgetEvent(#gadBrightness, @Spin20Handler())
        BindGadgetEvent(#gadContrast, @Spin21Handler())
        BindGadgetEvent(#gadGamma, @Spin22Handler())
        BindGadgetEvent(#gadSaturation, @Spin23Handler())
        BindGadgetEvent(#gadHue, @Spin24Handler())
        BindGadgetEvent(#gadSharpness, @Spin25Handler())
        BindGadgetEvent(#gadExposure, @Spin26Handler())
;        BindGadgetEvent(#gadBright2, @Spin27Handler())
;        BindGadgetEvent(#gadCont2, @Spin28Handler())

CreateImage(2,#WIDTH,#HEIGHT,24)
ImageGadget(#scopeGadget,0,#HEIGHT+4,#WIDTH,#HEIGHT/2,ImageID(2))
        Repeat
          *image = cvQueryFrame(*capture)

          If *image
            cvShowImage(#CV_WINDOW_NAME, *image)

StartDrawing(WindowOutput(0))
GrabDrawingImage(1,0,1,#WIDTH,#HEIGHT)
StopDrawing()
StartDrawing(ImageOutput(1))
For y=0 To #HEIGHT-1: For X=0 To #WIDTH-1
  pixCol = Point(x,y)
rf.f=Red(pixCol):gf.f=Green(pixCol):bf.f=Blue(pixCol)
;scopeLum(x,y) = (Red(pixCol)*#KR) + (Green(pixCol)*#KG) + (Blue(pixCol)*#KB)
scopeLum(x,y) = (rf*#KR) + (gf*#KG) + (bf*#KB)
Next:Next
StopDrawing()

StartDrawing(ImageOutput(2))
Box(0,0,#WIDTH, #HEIGHT,#Black) ;BLANK SCOPE
For y=0 To #HEIGHT-1: For x=0 To #WIDTH-1
Plot(x, (#SCOPE_Y_OFFSET - scopeLum(x,y)), $00cc00)
Next:Next

LineXY(30,#SCOPE_Y_OFFSET-254, #WIDTH,#SCOPE_Y_OFFSET-254);MAX LEGAL
LineXY(30,#SCOPE_Y_OFFSET-235, #WIDTH,#SCOPE_Y_OFFSET-235);STUDIO SWING
LineXY(30,#SCOPE_Y_OFFSET-16, #WIDTH,#SCOPE_Y_OFFSET-16);STUDIO SWING
LineXY(30,#SCOPE_Y_OFFSET-1, #WIDTH,#SCOPE_Y_OFFSET-1);MIN LEGAL
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(2,265-254, "254", $00ffff)
DrawText(2,265-235, "235", $00ffff)
DrawText(2,265-16, "16", $00ffff)
DrawText(2,265-1, "  1", $00ffff)
StopDrawing()      
SetGadgetState(#scopeGadget,ImageID(2))
            cvWaitKey(1)
          EndIf
        Until WindowEvent() = #PB_Event_CloseWindow
      EndIf
      cvDestroyWindow(#CV_WINDOW_NAME)
      FreeGadget(#scopeGadget)
      CloseWindow(0)
      FreeImage(1):FreeImage(2)
      cvReleaseCapture(@*capture)
      ;cvReleaseImage(@*image);NOT NEEDED
      ClosePreferences()
EndIf
Last edited by chris319 on Tue May 26, 2015 7:56 am, edited 5 times in total.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: PureBasic Interface to OpenCV

Post by bbanelli »

chris319 wrote:It may yet be a little buggy. It sometimes crashes upon closing -- not sure why.

Code: Select all

cvReleaseImage(@*image)
It's true you do have to release images that you create, but in this particular case, when you handle the image with video, you don't have to do it. Just remove it and your program won't crash while exiting.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: PureBasic Interface to OpenCV

Post by chris319 »

It's true you do have to release images that you create, but in this particular case, when you handle the image with video, you don't have to do it. Just remove it and your program won't crash while exiting.
Thanks for that. I have edited my previous post and removed it.
Locked