cvtColor 断言失败(OpenCV with C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13477694/
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-27 17:22:06  来源:igfitidea点击:

cvtColor assertion failed ( OpenCV with C++ )

c++opencv

提问by TheNotMe

I have a

我有一个

cv::Mat image;

object, which I loaded an image to from a file, it reads it correctly and all.

对象,我从一个文件中加载了一个图像,它正确地读取了它。

Now I have written a function to transform it to grey color.

现在我编写了一个函数将其转换为灰色。

cv::cvtColor(image, image, CV_RGB2GRAY);

And this error comes up:

这个错误出现了:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/buildd/opencv-2.3.1/modules/imgproc/src/color.cpp, line 2834
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/imgproc/src/color.cpp:2834: error: (-215) scn == 3 || scn == 4 in function cvtColor

what could be the problem?

可能是什么问题呢?

This is how I read the image (through an imagehandler class which has a member cv::Mat m_image)

这就是我读取图像的方式(通过具有成员 cv::Mat m_image 的图像处理程序类)

imagehandler::imagehandler(const std::string& fileName)
: m_image(imread(fileName, CV_LOAD_IMAGE_COLOR))
{
        if(!m_image.data)
{
    cout << "Failed loading " << fileName << endl;
}

}

回答by Sassa

Try using different dst image:

尝试使用不同的 dst 图像:

cv::Mat grayImage;
cv::cvtColor(image, grayImage, CV_RGB2GRAY);

回答by gedefet

Try image.clone() for coping images

尝试 image.clone() 来处理图像

回答by mosleim

if(!image.empty()) {
    //your_code
} else
    std::cout<<"Emty "

check the matrix before you convert.

在转换之前检查矩阵。