2011-06-21 45 views
1

我想在OpenCV書中做這些例子,並且涉及到cvCanny的部分。我想使用它,但我不斷收到的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; 
} 
+0

順便說一句,您使用的是哪個版本的OpenCV? – karlphillip

+0

我正在使用OpenCV 2.20 – Seb

回答

1

如果您能夠看到原始圖像顯示在屏幕上,您忘了說。

我從來沒有厭倦告訴人們檢查函數的返回是必須的!

考慮IplImage* img = cvLoadImage("images/00000038.jpg");,你怎麼知道這個函數是否成功?據我所知,你所遇到的錯誤可能來自在調用cvCanny()之前失敗的函數。

無論如何,我最近發佈了一個code that uses cvCanny to improve circle detection。你可以檢查代碼,看看你在做什麼不同。

EDIT

你在這種情況下的問題是,要傳遞到cvCanny輸入和輸出作爲一個三通道圖像中,當只需要一個單一的通道的圖像。 Check the docs

無效cvCanny(常量CvArr *圖像,CvArr *邊緣,雙閾值1,雙閾值2,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); 
+0

我的歉意。我有兩個窗口正在顯示,如果我註釋掉cvCanny,然後在屏幕上顯示原始圖像。 – Seb

+0

已更新的答案。問題已修復。 – karlphillip

+0

這個技巧。謝謝您的幫助。 – Seb