OpenCV Problems

Just starting out? Need help? Post your questions and find answers here.
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

OpenCV Problems

Post by Xombie »

I'm trying OpenCV and building the edge.c example in PB.

Code: Select all

;-
EnableExplicit
OnErrorGoto(?HandleError)
;-
;{ IPL Image Declarations
#IPL_DEPTH_SIGN = $80000000

#IPL_DEPTH_1U = 1
#IPL_DEPTH_8U = 8
#IPL_DEPTH_16U = 16
#IPL_DEPTH_32F = 32

#IPL_DEPTH_8S  = (#IPL_DEPTH_SIGN | 8)
#IPL_DEPTH_16S = (#IPL_DEPTH_SIGN | 16)
#IPL_DEPTH_32S = (#IPL_DEPTH_SIGN | 32)

#IPL_DATA_ORDER_PIXEL = 0
#IPL_DATA_ORDER_PLANE = 1

#IPL_ORIGIN_TL = 0
#IPL_ORIGIN_BL = 1

#IPL_ALIGN_4BYTES = 4
#IPL_ALIGN_8BYTES = 8
#IPL_ALIGN_16BYTES = 16
#IPL_ALIGN_32BYTES = 32

#IPL_ALIGN_DWORD = #IPL_ALIGN_4BYTES
#IPL_ALIGN_QWORD = #IPL_ALIGN_8BYTES

#IPL_BORDER_CONSTANT = 0
#IPL_BORDER_REPLICATE = 1
#IPL_BORDER_REFLECT = 2
#IPL_BORDER_WRAP = 3
;}
;
;{ Color Conversion Constants
#CV_BGR2BGRA = 0
#CV_RGB2RGBA = #CV_BGR2BGRA

#CV_BGRA2BGR = 1
#CV_RGBA2RGB = #CV_BGRA2BGR

#CV_BGR2RGBA = 2
#CV_RGB2BGRA = #CV_BGR2RGBA

#CV_RGBA2BGR = 3
#CV_BGRA2RGB = #CV_RGBA2BGR

#CV_BGR2RGB =  4
#CV_RGB2BGR =  #CV_BGR2RGB

#CV_BGRA2RGBA = 5
#CV_RGBA2BGRA = #CV_BGRA2RGBA

#CV_BGR2GRAY = 6
#CV_RGB2GRAY = 7
#CV_GRAY2BGR = 8
#CV_GRAY2RGB = #CV_GRAY2BGR
#CV_GRAY2BGRA = 9
#CV_GRAY2RGBA = #CV_GRAY2BGRA
#CV_BGRA2GRAY = 10
#CV_RGBA2GRAY = 11

#CV_BGR2BGR565 = 12
#CV_RGB2BGR565 = 13
#CV_BGR5652BGR = 14
#CV_BGR5652RGB = 15
#CV_BGRA2BGR565 = 16
#CV_RGBA2BGR565 = 17
#CV_BGR5652BGRA = 18
#CV_BGR5652RGBA = 19

#CV_GRAY2BGR565 = 20
#CV_BGR5652GRAY = 21

#CV_BGR2BGR555  = 22
#CV_RGB2BGR555  = 23
#CV_BGR5552BGR  = 24
#CV_BGR5552RGB  = 25
#CV_BGRA2BGR555 = 26
#CV_RGBA2BGR555 = 27
#CV_BGR5552BGRA = 28
#CV_BGR5552RGBA = 29

#CV_GRAY2BGR555 = 30
#CV_BGR5552GRAY = 31

#CV_BGR2XYZ =  32
#CV_RGB2XYZ =  33
#CV_XYZ2BGR =  34
#CV_XYZ2RGB =  35

#CV_BGR2YCrCb = 36
#CV_RGB2YCrCb = 37
#CV_YCrCb2BGR = 38
#CV_YCrCb2RGB = 39

#CV_BGR2HSV =  40
#CV_RGB2HSV =  41

#CV_BGR2Lab =  44
#CV_RGB2Lab =  45

#CV_BayerBG2BGR = 46
#CV_BayerGB2BGR = 47
#CV_BayerRG2BGR = 48
#CV_BayerGR2BGR = 49

#CV_BayerBG2RGB = #CV_BayerRG2BGR
#CV_BayerGB2RGB = #CV_BayerGR2BGR
#CV_BayerRG2RGB = #CV_BayerBG2BGR
#CV_BayerGR2RGB = #CV_BayerGB2BGR

#CV_BGR2Luv =  50
#CV_RGB2Luv =  51
#CV_BGR2HLS =  52
#CV_RGB2HLS =  53

#CV_HSV2BGR =  54
#CV_HSV2RGB =  55

#CV_Lab2BGR =  56
#CV_Lab2RGB =  57
#CV_Luv2BGR =  58
#CV_Luv2RGB =  59
#CV_HLS2BGR =  60
#CV_HLS2RGB =  61

#CV_COLORCVT_MAX  = 100
;}
;
#CV_LOAD_IMAGE_UNCHANGED = -1
; /* 8bit, color Or Not */
#CV_LOAD_IMAGE_GRAYSCALE = 0
; /* 8bit, gray */
#CV_LOAD_IMAGE_COLOR = 1
; /* ?, color */
#CV_LOAD_IMAGE_ANYDEPTH = 2
; /* any depth, ? */ 
#CV_LOAD_IMAGE_ANYCOLOR = 4
; /* ?, any color */
;/
#CV_WINDOW_AUTOSIZE = 1
;/
#CV_BLUR_NO_SCALE = 0
#CV_BLUR  = 1
#CV_GAUSSIAN = 2
#CV_MEDIAN = 3
#CV_BILATERAL = 4
;/
#CV_CANNY_L2_GRADIENT = (1 << 31)
;/
;-
Structure IplROI
   ;
   coi.l
   ; /* 0 - no COI (all channels are selected), 1 - 0th channel is selected ...*/
   xOffset.l
   yOffset.l
   width.l
   height.l
   ;
EndStructure
Structure IplImage
   ;
   nSize.l
   ; sizeof(IplImage) 
   id.l
   ; version (=0)
   nChannels.l
   ; Most of OpenCV functions support 1,2,3 or 4 channels 
   alphaChannel.l
   ; ignored by OpenCV 
   depth.l
   ; pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F And IPL_DEPTH_64F are supported 
   colorModel.b[4]
   ; ignored by OpenCV 
   channelSeq.b[4]
   ; ditto 
   dataOrder.l
   ; 0 - interleaved color channels, 1 - separate color channels.  cvCreateImage can only create interleaved images 
   origin.l
   ; 0 - top-left origin, 1 - bottom-left origin (Windows bitmaps style) 
   align.l
   ; Alignment of image rows (4 or 8).   OpenCV ignores it And uses widthStep instead 
   width.l
   ; image width in pixels 
   height.l
   ; image height in pixels 
   roi.l
   ; image ROI. if NULL, the whole image is selected 
   maskROI.l
   ; must be NULL 
   imageId.l
   ; ditto 
   tileInfo.l
   ; ditto 
   imageSize.l
   ; image data size in bytes (==image->height*image->widthStep in Case of interleaved Data)
   imageData.l
   ; pointer to aligned image data 
   widthStep.l
   ; size of aligned image row in bytes 
   BorderMode.l[4]
   ; ignored by OpenCV 
   BorderConst.l[4]
   ; ditto 
   imageDataOrigin.l
   ; pointer to very origin of image data (Not necessarily aligned) - needed For correct deallocation 
EndStructure
Structure CvSize
   width.l
   height.l
EndStructure
;-
Import "c:\programs\opencv\lib\highgui.lib"
  ; function declarations 
  cvLoadImage(filename.s, iscolor.l = #CV_LOAD_IMAGE_COLOR)
  cvNamedWindow(name.s, flags.l = #CV_WINDOW_AUTOSIZE)
  cvDestroyWindow(name.s)
  cvCreateTrackbar(trackbar_name.s, window_name.s, *Value.l, count.l, *cvTrackbarCallback.l)
  cvShowImage(name.s, image.l)
  ;
  cvWaitKey(delay.l = 0) 
  ; Wait for key event infinitely (delay<=0) or for "delay" milliseconds
EndImport
Import "c:\programs\opencv\lib\cxcore.lib"
   ;
   cvReleaseImage(*Image.l)
   cvCreateImage(width.l, height.l, depth.l, channels.l)
   ; Creates IPL image (header and data)
   cvNot(src.l, dst.l)
   cvSetZero(arr.l)
   cvCopy(src.l, dst.l, mask.l = #Null)
   ;
EndImport
Import "c:\programs\opencv\lib\cv.lib"
   ;
   cvCvtColor(src.l, dst.l, code.l)
   cvSmooth(*src.l, *dst.l, smoothtype.l = #CV_GAUSSIAN, param1.l = 3, param2.l = 0, param3.d = 0, param4.d = 0)
   cvCanny(image.l, edges.l, threshold1.d, threshold2.d, aperture_size.l = 3)
   ;
EndImport
;-
Global *Image.IplImage, *cedge.IplImage, *gray.IplImage, *edge.IplImage
;
Global edge_thresh.l = 1
;
;-
Procedure.l on_trackbar(h.l)
   ;
   ; cvSmooth(*gray, *edge, #CV_BLUR, 3, 3, 0, 0)
   ; ;
   ; cvNot(*gray, *edge)
   ; ;
   ; cvCanny(*gray, *edge, edge_thresh, edge_thresh * 3, 3)
   ; ;
   ; cvSetZero(*cedge)
   ; ;
   ; cvCopy(*Image, *cedge, *edge)
   ; ;
   ; cvShowImage("Edge", *cedge)
   ;
EndProcedure
;-
Define.CvSize HoldCVSize
;
*Image = cvLoadImage("C:\Programs\OpenCV\samples\c\fruits.jpg", 1)
;
If *Image
   ;
   Debug "Loaded"
   With *Image
      Debug \nSize
      Debug \width
      Debug \height
      Debug \nChannels
      Debug \depth
      Debug \dataOrder
      Debug \origin
      Debug \imageSize
      Debug \widthStep
   EndWith
   ;
   HoldCVSize\width = *Image\width
   HoldCVSize\height = *Image\height
   ;
   *cedge = cvCreateImage(*Image\width, *Image\height, #IPL_DEPTH_8U, 3)
   ;
   If *cedge
      ;
      Debug "cedge Create : "+Str(*cedge)
      ;
      *gray = cvCreateImage(*Image\width, *Image\height, #IPL_DEPTH_8U, 1)
      *edge = cvCreateImage(*Image\width, *Image\height, #IPL_DEPTH_8U, 1)
      ;
      If *gray And *edge
         ;
         Debug "Edge and Gray create"
         ;
         cvCvtColor(*Image, *gray, #CV_BGR2GRAY)
         ;
         cvNamedWindow("Edge", 1)
         ;
         cvCreateTrackbar("Threshold", "Edge", @edge_thresh, 100, @on_trackbar())
         ;
         on_trackbar(0)
         ;
         cvWaitKey(0)
         ;
         cvReleaseImage(@*edge)
         cvReleaseImage(@*gray)
         ;
      EndIf
      ;
      cvReleaseImage(@*cedge)
      ;
   EndIf
   ;
   cvReleaseImage(@*Image)
   ;
   cvDestroyWindow("Edge")
   ;
EndIf
;
;-
Debug "Ended"
;-
End
;-
HandleError: 
;
MessageRequester("Error!", GetErrorDescription()) 
;
End 
;-
But I keep getting an error when trying to change the trackbar value: "An attempted read or write to/from an address to which that process isn't allowed."

It seems like it's a problem in the cvCreateTrackbar() function but I don't see what. The little program is fine until on_trackbar() is called to change the threshhold (by sliding the trackbar).

The cvCreateTrackbar() function is declared as...

Code: Select all

/* create trackbar and display it on top of given window, set callback */
CVAPI(int) cvCreateTrackbar( const char* trackbar_name, const char* window_name,
                             int* value, int count, CvTrackbarCallback on_change );
And the original c example is...

Code: Select all

#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#endif

char wndname[] = "Edge";
char tbarname[] = "Threshold";
int edge_thresh = 1;

IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;

// define a trackbar callback
void on_trackbar(int h)
{
    cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 );
    cvNot( gray, edge );

    // Run the edge detector on grayscale
    cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh*3, 3);
  
    cvZero( cedge );
    // copy edge points
    cvCopy( image, cedge, edge );

    cvShowImage(wndname, cedge);
}

int main( int argc, char** argv )
{
    char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
    
    if( (image = cvLoadImage( filename, 1)) == 0 )
        return -1;

    // Create the output image
    cedge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 3);

    // Convert to grayscale
    gray = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
    edge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
    cvCvtColor(image, gray, CV_BGR2GRAY);

    // Create a window
    cvNamedWindow(wndname, 1);

    // create a toolbar 
    cvCreateTrackbar(tbarname, wndname, &edge_thresh, 100, on_trackbar);

    // Show the image
    on_trackbar(0);

    // Wait for a key stroke; the same function arranges events processing
    cvWaitKey(0);
    cvReleaseImage(&image);
    cvReleaseImage(&gray);
    cvReleaseImage(&edge);
    cvDestroyWindow(wndname);

    return 0;
}

#ifdef _EiC
main(1,"edge.c");
#endif
Help would be appreciated.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: OpenCV Problems

Post by DoubleDutch »

Did you ever get anywhere with this? - I'm also thinking of using OpenCV for something.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: OpenCV Problems

Post by jesperbrannmark »

Oh... I dont have much to contribute but will *love* to follow this topic. OpenCV is very interesting, and being able to use it from PB would be amazing.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: OpenCV Problems

Post by DarkDragon »

I'd like to have a opencv wrapper in purebasic, but it is highly recommended to use the new C++ classes instead of those old C functions. Maybe it would be better to convert the smaller JJIL project entirely to PureBasic, as the C interface will die.
bye,
Daniel
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: OpenCV Problems

Post by DoubleDutch »

Maybe, but JJIL hasn't been worked on in over a couple of years (I think) - so maybe OpenCV has a better chance of surviving?

Having this available as a Native cross platform 'built-in' library would be Amazing! It would bring things like image filters, object and face recognition (possibly barcode and word recognition) and more to PureBasic.

See: http://www.purebasic.fr/english/viewtop ... =3&t=47401
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: OpenCV Problems

Post by djes »

Replace Import with ImportC, and Procedure with ProcedureC when needed.

Terrific library ! :D
Post Reply