OpenCV cvCanny内存异常

发布于 2024-11-16 20:19:15 字数 1282 浏览 7 评论 0原文

我正在尝试执行 OpenCV 书中的示例,并且到达了有关 cvCanny 的部分。我正在尝试使用它,但我不断收到内存异常错误

Unhandled exception at 0x75d8b760 in Image_Transform.exe: Microsoft C++ error: 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;
}

I am trying to do the examples in the OpenCV book and I got to the part regarding cvCanny. I am trying to use it, but I keep getting a memory exception error of

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

I have also looked at another post that was similar to this question, but it did not help for me as I got the same error each time. Any help is greatly appreciated and the source code for the function is located below.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2024-11-23 20:19:15

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

我总是不厌其烦地告诉人们检查函数的返回是必须的!

考虑 IplImage* img = cvLoadImage("images/00000038.jpg"); ,如何判断此函数是否成功?据我所知,您遇到的错误可能是由于调用 cvCanny() 之前函数失败所致。

无论如何,我最近发布了一个 使用 cvCanny 来改进的代码圆检测。您可以检查该代码,看看您做了什么不同的事情。

编辑

在这种情况下,您的问题是您将作为 3 通道图像传递给 cvCanny 输入和输出,而它只需要一个单通道图像。 查看文档

void cvCanny(const CvArr* 图像, CvArr* 边缘, 双阈值 1, 双阈值 2, int 孔径大小=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);

You forgot to say if you are able to see the original image being displayed on the screen.

I never get tired of telling people that checking the return of functions is a must!

Consider IplImage* img = cvLoadImage("images/00000038.jpg"); , how can you tell if this function succeeded or not? As far as I can tell, the error you are having might be from a function failing prior to cvCanny() being called.

Anyway, I recently posted a code that uses cvCanny to improve circle detection. You can check that code and see what you are doing differently.

EDIT:

Your problem in this case is that you are passing to cvCanny input and output as a 3 channel image, when it takes only a single channel image. 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)

So, change your code to:

// 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文