Ask Your Question

Unhandled exception using cvCanny

asked 2013-03-17 15:19:24 -0500

jrpharis gravatar image

updated 2020-10-24 14:04:16 -0500

I am implementing cvCanny for a segmetation/edge detection project for my image processing class. The program runs multiple segmentation algorithms via command line arguments, and passes the input image to the corresponding algorithm via IplImage*. You can see in the code below, I am taking the pointer passed to the function, and creating a local copy to do the work on.

void CannyEdgeSeg (IplImage * image, int)

{ const char* name = "Edge Detection Window"; // Kernel size int N = 7;

// Set up images
IplImage* img;
img = cvCloneImage(image);
IplImage* img_b = cvCreateImage( cvSize(img->width+N-1,img->height+N-1), img->depth, img->nChannels );
IplImage* out = cvCreateImage( cvGetSize(img_b), IPL_DEPTH_8U, img_b->nChannels );

// Add convolution boarders
CvPoint offset = cvPoint((N-1)/2,(N-1)/2);
cvCopyMakeBorder(img, img_b, offset, IPL_BORDER_REPLICATE, cvScalarAll(0));

// Make window
cvNamedWindow( name, 1 );

// Edge Detection Variables
int aperature_size = N;
double lowThresh = 20;
double highThresh = 40;

// Create trackbars
cvCreateTrackbar( "High", name, &high_switch_value, 4, switch_callback_h );
cvCreateTrackbar( "Low", name, &low_switch_value, 4, switch_callback_l );


for(;;) {
    switch( highInt ){
        case 0:
            highThresh = 200;
        case 1:
            highThresh = 400;
        case 2:
            highThresh = 600;
        case 3:
            highThresh = 800;
        case 4:
            highThresh = 1000;
    switch( lowInt ){
        case 0:
            lowThresh = 0;
        case 1:
            lowThresh = 100;
        case 2:
            lowThresh = 200;
        case 3:
            lowThresh = 400;
        case 4:
            lowThresh = 600;

    // Edge Detection
    cvCanny( img_b, out, lowThresh*N*N, highThresh*N*N, aperature_size );
    cvShowImage(name, out);

    if( cvWaitKey( 15 ) == 27 ) 

// Release
cvReleaseImage( &img );
cvReleaseImage( &img_b );
cvReleaseImage( &out );
cvDestroyWindow( name );


However, with this methodology, I get this error once it tries to run cvCanny:

Unhandled exception at 0x000007fefd899e5d in Project2.exe: Microsoft C++ exception: cv::Exception at memory location 0x0012f630..

But, if I explicitly call the image to read in my function, rather than using the passed image,

 IplImage* img = cvLoadImage("fruits.jpg", 0);

the application runs just fine.

I don't understand why it would matter if the image is passed or called explicitly in the function. I was able to usethis exact same method, creating a clone of the passed image, in my other segmentation algorightms without any issues. Is there something specific to cvCanny that I am miss? Any help here would be greatly appreciated. Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-17 15:51:00 -0500

updated 2013-03-18 10:37:47 -0500

sammy gravatar image

Simply said, pointers are difficult to manage, since you have to assign and release memory manually. Skip the C - style api and go to the C++ style api, which handles all your pointer problems that occur with the IplImages. Switch to the Mat type variable, which can be used to do wondering stuff with the C++ api in a far more easier manner.

edit flag offensive delete link more


Yes, I completely agree. I have also used Mat, my teacher sets up the main and for some reason uses IplImage. The first thing I do is convert that to Mat. But the cvCanny function requires the input and output containers be IplImage. I tried with cvPyrSegmentation, which also requires IplImage*, to use casted Mat structures but got errors. I have not tried that with cvCanny yet.

jrpharis gravatar imagejrpharis ( 2013-03-17 16:59:15 -0500 )edit

Please fix your language, Steven. This is a forum for everyone to participate, and to keep it nice and pleasant, politeness is a must.

sammy gravatar imagesammy ( 2013-03-18 02:42:48 -0500 )edit

First of all @sammy, I have no clue what you are talking about. Looking at my post the only think I actually do is telling people that pointers can be hard to deal with correctly.

@jrpharis : there are C++ functions for that cvCanny. Look at the functions in the docs at this link. The new C++ name is Canny, without the cv.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-18 07:57:47 -0500 )edit

And same goes for all other functions. Just take the function name without the cv, put that in the search bar and you will find the possible C++ solutions.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-18 07:59:09 -0500 )edit

Question Tools


Asked: 2013-03-17 15:19:24 -0500

Seen: 903 times

Last updated: Mar 18 '13