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

OpenCV and ROS2

asked 2021-07-22 13:45:42 -0500

mintybim gravatar image

I've checked all the related questions here and I still wasn't able to get this example to run. I apologize in advance if the solution was right in front of me and if that offended anyone.

Problem description: I run Ubuntu 20.04 and ROS2 foxy, installed OpenCV, via pip, everything is in the system python (no venv/conda), I wanted to get this minimal example of an OpenCV publisher and subscriber to run to get my webcam stream from one node to another. For installation the debian package method was chosen.

The example is found here, I did everything as it is described:

But the step with

rosdep install -i --from-path src --rosdistro foxy -y


ERROR: the following packages/stacks could not have their rosdep keys resolved to system dependencies: cv_basics: Cannot locate rosdep definition for [opencv2]

Question: How do I make this run? Are there any kind of examples for ROS2 where any somewhat modern version of OpenCV and Python/C++ works?

My greatest fear is that I have to use OpenCV 3.2 with Python 2.7 or something like that. Please tell me this is not the case anymore for ROS2.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-07-23 17:55:04 -0500

shonigmann gravatar image

ROS2 is comfortably using Python3, so let that alleviate your concerns.

The error you see wnen using rosdep install is just rosdep telling you that it doesn't know what "opencv2" is for your given OS and so it can't install something that satisfies the requirement listed in the package.xml of your cv_basics pacakge. As for resolutions, I'll refer you to question #232795 which does a good job of explaining how rosdep works and what you can do next.

If you've installed OpenCV2 yourself and have confirmed that the version you've installed matches the version expected by whichever package has the dependency, then you should be okay to ignore the error. You can ignore errors when by adding -r to your rosdep call. This allows rosdep to continue installing dependencies, despite errors.

If you'd rather fix the error, then you probably want to change "opencv2" in your package.xml to "opencv-python" so that rosdep can find it. opencv-python points to OpenCV so it should be what you're looking for.

edit flag offensive delete link more


That worked, thank you!

mintybim gravatar image mintybim  ( 2021-07-26 07:16:36 -0500 )edit

answered 2022-03-14 10:37:46 -0500

jimmycanuck gravatar image

using UBUNTU 20.04 I am following the same tutorial. But, the above solution did not work. I have opencv installed to local python via sudo apt install libopencv-dev python3-opencv Instead, I edited the package.xml file of my project to depend on python3-opencv rather than the suggestions above, and the project built successfully.

As I am still rather new to package management I would appreciate it if anyone could explain why this was what I needed to do rather than the users above. Since I have the same UBUNTU and ROS2 running.

edit flag offensive delete link more


I had the same issue and this solution worked for me.

benedikt gravatar image benedikt  ( 2022-05-24 11:25:38 -0500 )edit

This worked for me. python3-opencv installs version OpenCV 4.2 which works with Foxy. It was not working when I did pip install opencv-python, I think because that is version 4.5 which probably does not work with Foxy.

Morris gravatar image Morris  ( 2022-10-05 14:49:50 -0500 )edit

I worked me too, thanks. (Ubuntu 22.04, ROS 2 Humble)

kametor gravatar image kametor  ( 2023-01-13 05:12:50 -0500 )edit

Your Answer

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

Add Answer

Question Tools



Asked: 2021-07-22 13:45:42 -0500

Seen: 4,792 times

Last updated: Jul 23 '21