CvtColor approval failed (OpenCV with C ++)

I have

cv::Mat image; 

which I downloaded to the file from the file, it reads it correctly and that’s it.

Now I have written a function to convert it to gray.

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

And this error occurs:

 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 (via the imagehandler class, which has a cv :: Mat m_image member)

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

Try using a different dst image:

 cv::Mat grayImage; cv::cvtColor(image, grayImage, CV_RGB2GRAY); 
+6
source

Try image.clone () to copy images

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

check matrix before conversion.

-1
source

Source: https://habr.com/ru/post/1447277/


All Articles