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

Beginner question: OpenCV Error: Assertion failed, Ros

asked 2022-05-05 11:39:26 -0600

hnb gravatar image

updated 2022-05-08 11:43:32 -0600

gvdhoorn gravatar image

Ubuntu 18.04 Ros Melodic

in the shell:


the output:

OpenCV Error: Assertion failed (cn <= 4) in scalarToRawData, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/core/src/matrix.cpp, line 1104
[ERROR] [1651748901.105022]: bad callback: <function func at 0x7fedfaa5f1d0>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/", line 750, in _invoke_callback
  File "", line 338, in func
  File "", line 255, in search_around_poly
    leftx, lefty, rightx, righty, out_img = find_lane_pixels(image)
  File "", line 195, in find_lane_pixels
    cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 4)
error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/core/src/matrix.cpp:1104: error: (-215) cn <= 4 in function scalarToRawData

run without ROS in the VsCode: use /usr/bin/python3

I understand that there is a problem with the python version. If so how can I fix this? ı am new here and Ros.

Thanks in advance

edit retag flag offensive close merge delete


In that function, "cn" is the number of channels per pixel. For example, an RGB image has 3 channels per pixel. So assuming you are using python2, it would appear you are not passing in an acceptable image format.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-05-08 12:29:09 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2022-05-06 02:35:45 -0600

aarsh_t gravatar image

There are several possibilities, /usr/bin/python or /usr/bin/python2 or even /usr/bin/env python. Ros melodic works with python2.7 so you should be using python2 not python3.

To confirm you are using right path for python, you can run /usr/bin/python or /usr/bin/python2 in your terminal, use the one which starts the python in the terminal.

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: 2022-05-05 07:00:35 -0600

Seen: 235 times

Last updated: May 08