ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

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

asked 2020-02-20 03:02:45 -0500

jahim44229 gravatar image

updated 2020-02-20 04:08:39 -0500

gvdhoorn gravatar image

Images are bgr, where is the problem?

This is my code block.(callback function)

void callback_function(const ImageConstPtr& image_l,const ImageConstPtr& image_r){

cv_ptr_l = cv_bridge::toCvCopy(image_l, sensor_msgs::image_encodings::BGR8);

cv_ptr_r = cv_bridge::toCvCopy(image_r, sensor_msgs::image_encodings::BGR8);

Mat right = cv_ptr_r->image;

Mat left = cv_ptr_l->image;

    if(filter=="wls_conf") {  
       Ptr<StereoBM> left_matcher = StereoBM::create(max_disp,wsize);
       wls_filter = createDisparityWLSFilter(left_matcher);
       Ptr<StereoMatcher> right_matcher = createRightMatcher(left_matcher);

       left_for_matcher  = left.clone();
       right_for_matcher = right.clone();

       cvtColor(left_for_matcher,  left_for_matcher,  COLOR_BGR2GRAY);
       cvtColor(right_for_matcher, right_for_matcher, COLOR_BGR2GRAY);
       imshow("lefti", left_for_matcher);
       imshow("righti", right_for_matcher);

That is my error ;

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

Aborted (core dumped)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-06-10 21:55:41 -0500

harumo11 gravatar image

Hello. I got same error and solve it for now.

A cause is that there are 2 opencv library (version 4.4 and 3.2) in one probram. So you can avoid this problem in cvtColor() by specifying version of opencv 3.2. Please change this line in your CMakeLists.txt.

FROM find_package(OpenCV REQUIRED)

TO find_package(OpenCV 3.2 REQUIRED)

I suppose that cv_bridge package uses opencv 3.2 and your program use another version opencv. I think It is a error cause.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-02-20 02:49:57 -0500

Seen: 1,913 times

Last updated: Jun 10 '20