我正在尝试做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;
}
发布于 2011-06-21 22:20:04
您忘了说您是否能够看到屏幕上显示的原始图像。
我不厌其烦地告诉人们,检查函数的返回是必须的!
以IplImage* img = cvLoadImage("images/00000038.jpg");
为例,如何判断此函数是否成功?据我所知,您遇到的错误可能是在调用cvCanny()
之前某个函数失败所致。
不管怎样,我最近发布了一个code that uses cvCanny to improve circle detection。你可以检查代码,看看你做了什么不同的事情。
编辑
在这种情况下,您的问题是传递给cvCanny输入和输出作为一个3通道图像,而它只需要一个通道图像。Check the docs
空图像(常量CvArr* cvCanny,CvArr* threshold1,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);
https://stackoverflow.com/questions/6426677
复制相似问题