OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor

hi

trying to use imshow and getting this error

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor

my code is

cv::Mat cvImage = cv_bridge::toCvCopy(*msg, sensor_msgs::image_encodings::BGR8)->image;
cv::Mat im_gray;
cv::cvtColor(cvImage,im_gray,cv::COLOR_BGR2GRAY);
cv::namedWindow( "Display window", WINDOW_AUTOSIZE );
if (!im_gray.empty())
            {
                cv::imshow("Display window",im_gray);
            }

check cvImage , like:

cout << cvImage.type() << " " << cvImage.channels() << endl;

where’s that cv_bridge::toCvCopy from ? ROS ?

and besides, there should be a cv::waitKey() following the imshow() else you won’t see anything

cv_bridge::toCvCopy is from ROS

I have cv::waitKey(0); a line after

for cvImage I got 16 3
for im_gray I got 0 1

1 Like