我正在尝试做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;
}
最佳答案
您忘记说了是否能够看到屏幕上显示的原始图像。
我从不厌倦地告诉人们检查功能的返回是必须的!
考虑IplImage* img = cvLoadImage("images/00000038.jpg");
,如何判断该函数是否成功?据我所知,您遇到的错误可能是由于函数在调用cvCanny()
之前失败。
无论如何,我最近发布了code that uses cvCanny to improve circle detection。您可以检查该代码,然后查看自己在做什么。
编辑:
在这种情况下,您的问题是,当它仅接受单个通道图像时,您将传递给cvCanny输入和输出为3通道图像。 Check the docs:
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);