ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Configuring ROS2 for OpenCV and C++

asked 2022-10-08 09:11:14 -0500

Werewolf_86 gravatar image

updated 2022-10-08 09:24:28 -0500

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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2022-10-08 09:38:17 -0500

sgvandijk gravatar image

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:

<depend>cv_bridge</depend>

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:

https://gitlab.com/boldhearts/ros2_v4...

and specifically an example of it operating on images here:

https://gitlab.com/boldhearts/ros2_v4...

Also see the cv_bridge tutorials:

http://wiki.ros.org/cv_bridge/Tutoria...

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

edit flag offensive delete link more

Comments

Thank you very much for such a detailed answer, Sander ! Was very helpful. Regards

Werewolf_86 gravatar image Werewolf_86  ( 2022-10-16 10:01:10 -0500 )edit
0

answered 2023-01-13 12:08:58 -0500

Roberto Z. gravatar image

Apparently ROS2 installs OpenCV, so it is definitely worth verifying if that is the case. To to so run the following in your terminal:

pkg-config --modversion opencv4

If you get a version number you already have it.

Then to integrate OpenCV with ROS2 you have to create a ROS2 package and specify OpenCV as dependency:

ros2 pkg create my_ros2_opencv_pkg --dependencies rclcpp OpenCV

If you plan to subscribe or publish images to the ROS2 network include at least cv_bridge, sensor_msgs and image_transport, for instance:

ros2 pkg create my_ros2_opencv_pkg --dependencies rclcpp std_msgs sensor_msgs cv_bridge image_transport OpenCV

This will automatically populate the package.xml file and most part of the CMakeLists.txt for you.

In your .cpp file don't forget to add (in addition to any ROS2 includes) these #include statements at the top:

#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.hpp>
#include <opencv2/opencv.hpp>

Finally modify your CMakeLists.txt to generate the executable like so:

add_executable(my_ros2_opencv_node src/name_of_your_cpp_file.cpp)

And add the node dependencies to the ament_target_dependencies() function, for instance:

ament_target_dependencies(my_ros2_opencv_node rclcpp std_msgs sensor_msgs cv_bridge image_transport OpenCV)

And install it:

install(TARGETS
   my_ros2_opencv_node
   DESTINATION lib/${PROJECT_NAME}
 )

This is how I integrate OpenCV with ROS2 using C++ when I create a new package from scratch.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-10-08 09:11:14 -0500

Seen: 1,260 times

Last updated: Jan 13 '23