Rosanswers logo

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(&ros_image.data()[0], image.data, size);

then mp_publisher->write(&st);

what I'm doing wrong?

the full project is here https://github.com/HemaZ/FastRTPS-ROS2

thank you in advance

Originally posted by Ibrahim Essam on ROS Answers with karma: 16 on 2020-01-24

Post score: 0


1 Answer 1


Rosanswers logo

here is an example for anyone wondering how to do it. https://github.com/HemaZ/FastRTPS-ROS2

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

Originally posted by Ibrahim Essam with karma: 16 on 2020-01-27

This answer was ACCEPTED on the original site

Post score: 0

Original comments

Comment by gvdhoorn on 2020-01-27:
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.

Comment by Ibrahim Essam on 2020-02-09:
@gvdhoom updated.


Your Answer

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