cvtColor 断言失败(带有 C++ 的 OpenCV )

cvtColor assertion failed ( OpenCV with C++ )

本文关键字:OpenCV C++ 带有 断言 失败 cvtColor      更新时间:2023-10-16

我有一个

cv::Mat image;

对象,我从文件中加载了图像,它可以正确读取它。

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

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

出现此错误:

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

可能有什么问题?

这就是我读取图像的方式(通过具有成员 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;
}
}

尝试使用不同的 dst 图像:

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

尝试 image.clone() 来复制图像

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

转换前检查矩阵。