c++ - OpenCV 和 MinGW 的运行时错误?

标签 c++ windows opencv mingw qt5

我正在尝试运行 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;
}

我有 if 语句 来检查 Camera 是否有任何问题,它应该终止程序,但事实并非如此。这是我得到的错误

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.

最佳答案

通过查看“color.cpp”中的OpenCV代码,错误中提到的变量“scn”似乎是帧的 channel 数,转换类型(BGR -> 灰度)要求有3 或 4 个 channel :

Assertion failed (scn == 3 || scn == 4)

您确定您的相机默认不提供灰度图像吗?尝试注释掉处理帧的行并仅显示检索到的图像,看看你得到了什么。或者只是在捕获帧后立即放置一个断点并检查“帧”变量——它是空的吗?它是否具有预期的大小等?

关于c++ - OpenCV 和 MinGW 的运行时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759961/

相关文章:

opencv - 如何在 ubuntu 11.10 上使用 cuda 和 opencv

c++ - 如何访问图像的片段

c++ - 如何将 map 作为不可变 map 传递?

.net - 此异常消息 : Not enough quota is available to process this command 中引用的是什么 'quota'

c++ - Dll 只能通过 LoadLibrary 正确加载吗?

c++ - 如何使用 Opencv 在任何计算机上显示图像

c# - 不同机器执行相同API的时间差

python - 使用Python直接向USB发送信号

c++ - Qt/C++ - 对调用者/被调用者、对象所有权感到困惑

使用 cx_Freeze 构建的 Python 可执行文件崩溃