Welcome to RDK X5 Board repository
1.ROS+opencv application
1. ROS+opencv application
ROS transmits images in its own sensor-msgs/Image message format and cannot directly process images, but the provided CvBridge can perfectly convert and convert image data formats. CvBridge is a ROS library that serves as a bridge between ROS and Opencv.
The conversion of Opencv and ROS image data is shown in the following figure:

This lesson will use two cases to demonstrate how to use CvBridge for data conversion.
1.1 Camera Topic Data
We have already set up the camera driver environment and visible color images, depth images, and infrared IR images in the previous section. We can first check which topics are published and what the content of the image data is after driving the camera. Enter the following command on the terminal to start the camera,
x
#AI VIEW camera startros2 launch astra_camera gemini.launch.xml
Then, use the following command to view the list of topic data.
xxxxxxxxxxros2 topic list

Among them,/camera/color/image_raw and/camera/depth/image_raw are the data of color and depth maps. You can use the following command to view the data content of a certain frame,
x
#View RGB image topic data contentros2 topic echo /camera/color/image_raw#View Depth image topic data contentros2 topic echo /camera/depth/image_rawColor image:

Depth map:

Here is the main value needed: encoding, which represents the encoding format of the image and needs to be referred to when converting image data later.
1.2、Read color image topic data and display color images
1.2.1、Run command
x
#astraproplus cameraros2 launch astra_camera gemini.launch.xmlros2 run yahboomcar_visual astra_rgb_image
View topic communication between nodes.
Input following command in the terminal
xxxxxxxxxxros2 run rqt_graph rqt_graph

1.2.2、Core code
Code path
xxxxxxxxxx~/orbbec_ws/src/yahboomcar_visual/yahboomcar_visual/astra_rgb_image.py/The get_ astra_rgb_node node subscribed to the topic of/camera/color/image_raw, and then converted the topic data into car image data for publication through data conversion.
The code is as follows.
x
#Import the opecv library and cv-bridge libraryimport cv2 as cvfrom cv_bridge import CvBridge#Create CvBridge objectself.bridge = CvBridge()#Define a subscriber to subscribe to RGB color image topic data published by a deep camera nodeself.sub_img=self.create_subscription(Image,'/camera/color/image_raw',self.handleTopic,100)#Convert msg to image data, where bgr8 is the image encoding formatframe = self.bridge.imgmsg_to_cv2(msg, "bgr8")1.3、Subscribe to deep image topic information and display deep images
1.3.1、Run command
x
#astraproplus cameraros2 launch astra_camera gemini.launch.xmlros2 run yahboomcar_visual astra_depth_image
View topic communication between nodes.
Input following command in the terminal.
xxxxxxxxxxros2 run rqt_graph rqt_graph

1.3.2、Core code
Code path
xxxxxxxxxx~/orbbec_ws/src/yahboomcar_visual/yahboomcar_visual/astra_depth_image.py
The basic implementation process is the same as displaying RGB color images. We subscribed to the topic data/camera/depth/image_raw published by the depth camera node, and then converted it into image data through data conversion.
The code is as follows.
x
#Import the opecv library and cv-bridge libraryimport cv2 as cvfrom cv_bridge import CvBridge#Create CvBridge objectself.bridge = CvBridge()#Define a subscriber to subscribe to Depth depth image topic data published by a Depth Camera nodeself.sub_img=self.create_subscription(Image,'/camera/depth/image_raw',self.handleTopic,10)#Convert msg to image data, where 32FC1 is the image encoding formatframe = self.bridge.imgmsg_to_cv2(msg, "32FC1")