OpenCV cvCanny内存异常

时间:2011-06-21 14:10:08

标签: c image-processing opencv edge-detection

我正在尝试在OpenCV书中做例子,我得到了关于cvCanny的部分。我试图使用它,但我不断收到

的内存异常错误

Unhandled exception at 0x75d8b760 in Image_Transform.exe: Microsoft C++ exception: cv::Exception at memory location 0x0011e7a4..

我还看过另一篇与此问题类似的帖子,但它对我没有帮助,因为我每次都得到同样的错误。非常感谢任何帮助,该函数的源代码位于下方。

void example2_4(IplImage* img)
{
// Create windows to show input and ouput images
cvNamedWindow("Example 2-4 IN", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Example 2-4 OUT", CV_WINDOW_AUTOSIZE);

// Display out input image
cvShowImage("Example 2-4 IN", img);

// Create an image to hold our modified input image
IplImage* out = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);

// Do some smoothing
//cvSmooth(img, out, CV_GAUSSIAN, 3, 3);

// Do some Edge detection
cvCanny(img, out, 10, 20, 3);

// Show the results
cvShowImage("Example 2-4 OUT", out);

// Release the memory used by the transformed image
cvReleaseImage(&out);

// Wait for user to hit a key then clean up the windows
cvWaitKey(0);
cvDestroyWindow("Example 2-4 IN");
cvDestroyWindow("Example 2-4 OUT");
}

int main()
{
// Load in an image
IplImage* img = cvLoadImage("images/00000038.jpg");

// Run the transform
example2_4(img);

// clean the image from memory
cvReleaseImage(&img);

return 0;
}

1 个答案:

答案 0 :(得分:1)

您忘了说是否能够看到屏幕上显示原始图像。

我从不厌倦地告诉人们必须检查功能的返回!

考虑IplImage* img = cvLoadImage("images/00000038.jpg");,你怎么知道这个功能是否成功?据我所知,您所遇到的错误可能来自于cvCanny()被调用之前失败的函数。

无论如何,我最近发布了code that uses cvCanny to improve circle detection。您可以检查该代码,看看您在做什么不同。

修改

在这种情况下,您的问题是,当它只需要一个通道图像时,您将传递给cvCanny输入和输出为3通道图像。 Check the docs

  

void cvCanny(const CvArr * image,CvArr * edges,double threshold1,double threshold2,int aperture_size = 3)

Implements the Canny algorithm for edge detection.
Parameters: 

    * image – Single-channel input image
    * edges – Single-channel image to store the edges found by the function
    * threshold1 – The first threshold
    * threshold2 – The second threshold
    * aperture_size – Aperture parameter for the Sobel operator (see Sobel)

因此,请将您的代码更改为:

// Create an image to hold our modified input image
IplImage* out = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); 

// Do some smoothing
//cvSmooth(img, out, CV_GAUSSIAN, 3, 3);

IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); 
cvCvtColor(img, gray, CV_BGR2GRAY);

// Do some Edge detection
cvCanny(gray, out, 10, 20, 3);