简体   繁体   English

OpenCV和MinGW的运行时错误?

[英]Runtime error with OpenCV and MinGW?

I'm trying to run an example in OpenCV's documentation as following 我正在尝试在OpenCV的文档中运行以下示例

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

I have if statement to check if anything goes wrong with Camera, it should terminate the program but this is not the case. 我有if statement来检查Camera是否有任何问题,它应该终止程序,但事实并非如此。 This is the error that I got 这是我得到的错误

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.

By looking at the OpenCV code in "color.cpp" it looks like the variable "scn" mentioned in the error is the number of channels of the frame, and the conversion type (BGR -> grayscale) requires there to be 3 or 4 channels: 通过查看“ color.cpp”中的OpenCV代码,看起来错误中提到的变量“ scn”是帧的通道数,并且转换类型(BGR->灰度)要求为3或4渠道:

Assertion failed (scn == 3 || scn == 4) 断言失败(scn == 3 || scn == 4)

Are you sure your camera isn't providing grayscale images by default? 您确定默认情况下相机不提供​​灰度图像吗? Try commenting out the lines that process the frame and show only the retrieved image and see what you get. 尝试注释掉处理框架的行,并仅显示检索到的图像,然后看看您得到了什么。 Or just put a breakpoint right after the frame capture and inspect the "frame" variable - is it empty? 还是仅在捕获帧之后放置一个断点并检查“ frame”变量-它是否为空? does it have the expected size and so on? 它具有预期的尺寸等吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM