Rosanswers logo

How to properly setup the ROS2 workspace (i.e., cmakelists and package.xml, etc.) for using OpenCV and C++ ?

I could not find proper examples for that.

Thanks in advance.

Originally posted by Werewolf_86 on ROS Answers with karma: 125 on 2022-10-08

Post score: 0


1 Answer 1


Rosanswers logo

One of the easiest ways is to install the cv_bridge package. That brings in OpenCV and all requirements, plus functionality to use it more easily in the context of ROS 2.

To do that, add the following to the package.xml of your package:


and the following near the top of your CMakeLists.txt file:

find_package(cv_bridge REQUIRED)

and don't forget to add it to ament_target_dependencies as well.

Then if you run rosdep to install all dependencies, cv_bridge, and with that OpenCV, will be installed and available in your workspace:

rosdep install --from-paths src -r -y

Or on Ubuntu you could also install it by hand with:

sudo apt install ros-${ROS_DISTRO}-cv-bridge

You would then normally use cv_bridge to convert between ROS image messages and OpenCV images, and to perform some operations such as colour conversions.

For an example of a package that depends on cv_bridge, see:


and specifically an example of it operating on images here:


Also see the cv_bridge tutorials:


They are for ROS 1 but still mostly relevant for ROS 2 as well.

Originally posted by sgvandijk with karma: 649 on 2022-10-08

This answer was ACCEPTED on the original site

Post score: 3

Original comments

Comment by Werewolf_86 on 2022-10-16:
Thank you very much for such a detailed answer, Sander ! Was very helpful. Regards


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.