Ask Your Question

problem publishing OpenCV Image from FastRTPS to ROS2

asked 2020-01-24 09:19:23 -0500

Ibrahim Essam gravatar image

Hello ROS2 community, I'm trying to publish video camera images from opencv using fastRTPS standalone to ROS2 Node. I've copied Image.idl from ROS2, built it like the HelloWorld example from fastRTPS. changed topicname to rt/topicname and added the DS_ namespace as the answer in this post suggests link. when I try to publish the image it shows on ROS2 network using ros2 topic echo but after filling the data field of Image message with OpenCV MAT data. nothing shows. I use the following function to convert OpenCV MAT to Image Message

 void toImageMsg(sensor_msgs::msg::Image & ros_image, cv::Mat &image)



ros_image.step(image.cols * image.elemSize());

size_t size = ros_image.step() * image.rows;;

memcpy(&[0],, size);

then mp_publisher->write(&st);

what I'm doing wrong?

the full project is here

thank you in advance

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-01-27 06:45:00 -0500

Ibrahim Essam gravatar image

updated 2020-02-09 15:34:31 -0500

here is an example for anyone wondering how to do it.

First we need to get all the messages dependencies from ROS2 installation.

Example if we need to build sensors_msg/Image we should build all of the message dependencies. Image -> Header -> Time

we can get these messages from


and copy them to current directory.

edit this line

#include "builtin_interfaces/msg/Time.idl"

in Header.idl to #include "Time.idl"

edit this line in Image.idl #include "std_msgs/msg/Header.idl" to #include "Header.idl"

generate the messages using fastrtpsgen like this

fastrtpsgen -example CMake Image.idl

edit line #37 in ImagePubSubTypes.cxx to setName("sensor_msgs::msg::dds_::Image_");

Edit topic name in ImagePublisher.cxx and ImageSubscriber.cxx and add rt/ before the topic name. ie Wparam.topic.topicName = "rt/ImagePubSubTopic";

Add these two lines under the above line.

Wparam.qos.m_publishMode.kind = ASYNCHRONOUS_PUBLISH_MODE; // To handle big data
Wparam.historyMemoryPolicy = rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;

build the project

mkdir build
cd build
cmake ..
./Image publisher
edit flag offensive delete link more


That repository contains at least 30 files.

It would be good to summarise what you found out about your problem/task and add that to your answer.

When you remove the repository from your Github account (and that is likely to happen), your current answer will not contain any information.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-27 06:59:40 -0500 )edit

@gvdhoom updated.

Ibrahim Essam gravatar image Ibrahim Essam  ( 2020-02-09 15:36:09 -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

1 follower


Asked: 2020-01-24 09:19:23 -0500

Seen: 540 times

Last updated: Feb 09 '20