2014-03-31 64 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