c - OpenCV cvCanny 内存异常

标签 c image-processing opencv edge-detection

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

Image_Transform.exe 中 0x75d8b760 处的未处理异常:Microsoft C++ 异常:内存位置 0x0011e7a4.. 处的 cv::Exception

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

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 .您可以检查该代码并查看您所做的不同之处。

编辑:

在这种情况下,您的问题是,当它仅采用单 channel 图像时,您将作为 3 channel 图像传递给 cvCanny 输入和输出。 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);

关于c - OpenCV cvCanny 内存异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6426677/

相关文章:

c - 您如何定位和修复段错误?

c++ - 为什么 cmake 在 64 位系统上查找 32 位库而不是 64 位库?

c - 编译 *.cc 文件时出现 Gcc 编译错误

python - OpenCV 的 calcOpticalFlowFarneback 的未知输出

c - "formatted"C语言

c++ - OpenCV cv::cvtColor 删除 alpha channel ,如何保留 alpha 数据?

image-processing - 为 cv::imshow 配置窗口位置

asp.net - 如何使用 C# 在 ASP.NET 中识别 CMYK 图像

python - 用另一张图片替换一张图片的白色部分

c++ - 是否可以在 OpenCV 中将卡方内核用于 SVM?