C++ OpenCV 错误:断言失败 (scn == 3 || scn == 4) in cv:: cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3737

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31297841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:55:01  来源:igfitidea点击:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv:: cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3737

c++visual-studioopencvvisual-studio-2013

提问by Joissa.R

Hi I am trying to run this sample code from OpenCV:

嗨,我正在尝试从 OpenCV 运行此示例代码:

#include "opencv2\opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if (!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges", 1);
    for (;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if (waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

I am currently using a Windows 7 x64 BootCamp on a Macbook Pro. I'm running this code with Visual Studios 2013 and OpenCV 2.4.9.

我目前在 Macbook Pro 上使用 Windows 7 x64 BootCamp。我正在使用 Visual Studios 2013 和 OpenCV 2.4.9 运行此代码。

This is how I've set up my Config Properties:

这是我设置配置属性的方式:

VC++ Directories: Include Directories: H:\opencv\build\include;$(IncludePath)

Linker:General:Additional Library Directories: H:\opencv\build\x64\vc12\lib;%(AdditionalLibraryDirectories)
Linker:Input:Additional Dependencies: opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_nonfree249.lib;opencv_objdetect249.lib;opencv_ocl249.lib;opencv_photo249.lib;opencv_stitching249.lib;opencv_superres249.lib;opencv_ts249.lib;opencv_video249.lib;opencv_videostab249.lib;%(AdditionalDependencies)

When I click on Local Windows Debugger in Release x64 mode I get the following error from Visual Studios:

当我在 Release x64 模式下单击本地 Windows 调试器时,我从 Visual Studios 收到以下错误:

First-chance exception at 0x000007FEFD21B3DD in Project3.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000019A8A0.

If there is a handler for this exception, the program may be safely continued.

Project3.exe 中 0x000007FEFD21B3DD 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x000000000019A8A0 处的 cv::Exception。

如果有这个异常的处理程序,程序可以安全地继续。

When I click Break instead (scared to press Continue), a window named Edges does pop up and the camera does turn on since the green light turns on. But I also get the following error in the command window:

当我改为单击 Break(害怕按 Continue)时,会弹出一个名为 Edges 的窗口,并且由于绿灯亮起,相机确实打开了。但我也在命令窗口中收到以下错误:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv:: cvtColor, file ........\opencv\modules\imgproc\src\color.cpp, line 3737

OpenCV 错误:断言失败 (scn == 3 || scn == 4) in cv:: cvtColor, file ........\opencv\modules\imgproc\src\color.cpp, line 3737

I'm pretty new to C++ and Visual Studios, any help would be appreciated. Thanks in advance!

我对 C++ 和 Visual Studios 很陌生,任何帮助将不胜感激。提前致谢!

采纳答案by Miki

From the conversation in the comments to the question, we saw that VideoCapturegives frame in grayscale. So the call to cvtColorcaused the crash.

从评论中的对话到问题,我们看到VideoCapture灰度给出了框架。所以调用cvtColor导致崩溃。

...
Mat frame;
cap >> frame; // frame is already CV_8UC1
//cvtColor(frame, edges, CV_BGR2GRAY); // so don't to convert here, or crash!
edges = frame.clone(); 
...