2014-03-31 47 views
0

我試圖運行OpenCV的文檔中的一個例子如下OpenCV和MinGW的運行時錯誤?

#include <iostream> 
#include <opencv2\core\core.hpp> 
#include <opencv2\highgui\highgui.hpp> 
#include <opencv\cv.h> 


int main(int argc, char **argv) 
{ 
    cv::VideoCapture cap(0); 
    if (!cap.isOpened()) 
    { 
     std::cout << "Error with opening Camera" << std::endl; 
     return -1; 
    } 

    cv::Mat frame, edges; 
    cv::namedWindow("edges", 1); 
    for(;;) 
    { 
     cap >> frame; 
     cvtColor(frame, edges, CV_BGR2GRAY); 
     GaussianBlur(edges, edges, cv::Size(7,7), 1.5, 1.5); 
     Canny(edges, edges, 0, 30, 3); 
     cv::imshow("edges", edges); 
     if (cv::waitKey(30) >= 0) break; 
    } 

    return 0; 
} 

我有if statement檢查,如果有什麼毛病相機,它應該終止程序,但這情況並非如此。這是我得到

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file C:\opencv_2_4_8\opencv\sources\modules\imgproc\src\color.cpp, line 3737 
terminate called after throwing an instance of 'cv::Exception' 
    what(): C:\opencv_2_4_8\opencv\sources\modules\imgproc\src\color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor 


This application has requested the Runtime to terminate it in an unusual way. 
Please contact the application's support team for more information. 

回答

1

通過查看「color.cpp」 OpenCV的代碼中的錯誤,它看起來像在錯誤中提到的變量「SCN」是幀的信道的數量,和轉換式(BGR - >灰度)要求那裏是3個或4個通道:

斷言失敗(SCN == 3 || SCN == 4)

你確定你的相機是不是默認提供灰度圖像?嘗試註釋處理框架的行並僅顯示檢索到的圖像並查看您獲得的內容。或者在幀捕獲後立即放置一個斷點並檢查「幀」變量 - 是否爲空?它是否具有預期的尺寸等?

+0

實際上是因爲凸輪存在問題,所以顯示灰色圖像。我確實禁用了凸輪,而且它的工作原理並不是我的意思是if語句。感謝您的建議。 – CroCo