yahboom
HomeTutorialsProduct
Solution
Blog
Online Store
About Us
EN/中文
Store
Realsense Depth Camera📖
1.Installation video
  • Installation video
2.Tutorials
  • 2.0 How to use camera
  • 2.1 Opencv application
  • 2.2 Color recognition-object tracking
  • 2.3 ARTag
  • 2.4 ROS+Opencv basic course
  • 2.5 ROS+OpenCV application
  • 2.6 Data conversion and point cloud
Download
  • Code
  • Datasheet
  • Software
  • VM file
Download ZIP

Welcome to Realsense Depth Camera repository

2.4 ROS+Opencv basic course

4、 ROS+Opencv basic course

4、 ROS+Opencv basic course4.1、Overview4.2、Astra4.2.1、Start up Astra camera4.2.2、Start the color map subscription node4.2.3、Start the depth map subscription node4.2.4、Start color image inversion4.2.5、Publish image

This lesson takes the Astra camera as an example, the ordinary camera is similar.

4.1、Overview

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

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

Source code:https://github.com/ros-perception/vision_opencv.git

Function package path:~/transbot_ws/src/transbot_visual

【CvBridge】is a ROS library, equivalent to a bridge between ROS and Opencv. It can perfectly convert and be converted image data format.

Opencv and ROS image data conversion is shown below:

image-20210903091159912

This function package not only needs to use [CvBridge], but also needs [Opencv] and [PCL], so we need to perform the following configuration.

  • package.xml

Add following content.

​x
    <build_depend>sensor_msgs</build_depend>
    <build_export_depend>sensor_msgs</build_export_depend>
    <exec_depend>sensor_msgs</exec_depend>

    <build_depend>std_msgs</build_depend>
    <build_export_depend>std_msgs</build_export_depend>
    <exec_depend>std_msgs</exec_depend>

    <build_depend>cv_bridge</build_depend>
    <build_export_depend>cv_bridge</build_export_depend>
    <exec_depend>cv_bridge</exec_depend>

    <build_depend>transbot_msgs</build_depend>
    <build_export_depend>transbot_msgs</build_export_depend>
    <exec_depend>transbot_msgs</exec_depend>

    <exec_depend>image_transport</exec_depend>

【cv_bridge】:Image conversion dependent package.

【transbot_msgs】:Custom message dependency package.

  • CMakeLists.txt

This file has a lot of configuration content, please check the source file for specific content.

4.2、Astra

4.2.1、Start up Astra camera

xxxxxxxxxx
roslaunch astra_camera astrapro.launch

View topic

xxxxxxxxxx
rostopic list

Common topics are as follows

Topic nameData type
/camera/depth/image_rawsensor_msgs/Image
/camera/depth/imagesensor_msgs/Image
/camera/rgb/image_rawsensor_msgs/Image
/camera/depth/image_raw/compressedDepthsensor_msgs/CompressedImage
/camera/rgb/image_raw/compressedsensor_msgs/CompressedImage

View the encoding format of the topic: rostopic echo +【topic】+encoding, for example

xxxxxxxxxx
rostopic echo /camera/rgb/image_raw/encoding
rostopic echo /camera/depth/image_raw/encoding

image-20210903120844654

image-20210903120710683

The topic with 【compressed】or 【compressedDepth】 at the end of the topic is a compressed topic. During image transmission, ROS may cause data packet loss due to factors such as the network, main control running speed, main control running memory, and huge video stream data. Unable to get topics, so we recommend users to use compressed topics.

image-20210903140543123

4.2.2、Start the color map subscription node

xxxxxxxxxx
roslaunch transbot_visual astra_get_rgb.launch    # launch
rosrun transbot_visual astra_rgb_image.py         # py 
rosrun transbot_visual astra_rgb_image            # C++

View node graph

xxxxxxxxxx
rqt_graph

image-20210903112012709

【/astra_rgb_Image_cpp】is the node we wrote.

  • py code analysis

Create subscribers: The subscribed topic is 【"/camera/rgb/image_raw"】, the data type is 【Image】, and the callback function is 【topic()】

xxxxxxxxxx
sub = rospy.Subscriber("/camera/rgb/image_raw", Image, topic)

Use 【CvBridge】 for data conversion to ensure that the encoding format is correct.

xxxxxxxxxx
frame = bridge.imgmsg_to_cv2(msg, "bgr8")
  • c++ code analysis
xxxxxxxxxx
//Create a receiver.
ros::Subscriber subscriber = n.subscribe<sensor_msgs::Image>("/camera/rgb/image_raw", 10, RGB_Callback);
//Create cv_bridge example
cv_bridge::CvImagePtr cv_ptr;
//Data conversion
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);

4.2.3、Start the depth map subscription node

xxxxxxxxxx
roslaunch transbot_visual astra_get_depth.launch    # launch
rosrun transbot_visual astra_depth_image.py         # py
rosrun transbot_visual astra_depth_image            # C++

View node graph

xxxxxxxxxx
rqt_graph

image-20210903141033399

  • py code analysis

Create subscribers: The subscribed topic is ["/camera/depth/image_raw"], the data type is [Image], and the callback function is [topic()]

xxxxxxxxxx
sub = rospy.Subscriber("/camera/depth/image_raw", Image, topic)

Use 【CvBridge】 for data conversion to ensure that the encoding format is correct.

xxxxxxxxxx
# Encoding format
encoding = ['16UC1', '32FC1']
# Can switch different encoding formats to test the effect
frame = bridge.imgmsg_to_cv2(msg, encoding[1])
  • c++ code analysis
xxxxxxxxxx
//Create a receiver.
ros::Subscriber subscriber = n.subscribe<sensor_msgs::Image>("/camera/depth/image_raw", 10, depth_Callback);
//Create cv_bridge example
cv_bridge::CvImagePtr cv_ptr;
//Data conversion
cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_16UC1);

4.2.4、Start color image inversion

xxxxxxxxxx
roslaunch transbot_visual astra_image_flip.launch    # launch
rosrun transbot_visual astra_image_flip.py           # py

View node graph

xxxxxxxxxx
rqt_image_view

image-20210903105043487

  • py code analysis

Two subscribers and two publishers are created here, one for processing general image data and one for processing compressed image data.

1)Create subscriber

The subscribed topic is【"/camera/rgb/image_raw"】,the data type is【Image】,and the callback function is【topic()】。

The subscribed topic is【"/camera/rgb/image_raw/compressed"】,the data type is【CompressedImage】,and the callback function is【compressed_topic()】。

2)Create publisher

The published topic is【"/camera/rgb/image_flip"】,the data type is【Image】,Queue size【10】。

The published topic is【"/camera/rgb/image_flip/compressed"】,the data type is【CompressedImage】,Queue size【10】。

xxxxxxxxxx
sub_img = rospy.Subscriber("/camera/rgb/image_raw", Image, topic)
pub_img = rospy.Publisher("/camera/rgb/image_flip", Image, queue_size=10)
sub_comimg = rospy.Subscriber("/camera/rgb/image_raw/compressed", CompressedImage, compressed_topic)
pub_comimg = rospy.Publisher("/camera/rgb/image_flip/compressed", CompressedImage, queue_size=10)

3)Callback function

xxxxxxxxxx
# Normally image transmission processing
def topic(msg):
    if not isinstance(msg, Image):
        return
    bridge = CvBridge()
    frame = bridge.imgmsg_to_cv2(msg, "bgr8")
    # Opencv processing image
    frame = cv.resize(frame, (640, 480))
    frame = cv.flip(frame, 1)
    # opencv mat ->  ros msg
    msg = bridge.cv2_to_imgmsg(frame, "bgr8")
    # The image is processed and released directly
    pub_img.publish(msg)
    
# Compressed image transmission processing
def compressed_topic(msg):
    if not isinstance(msg, CompressedImage): return
    bridge = CvBridge()
    frame = bridge.compressed_imgmsg_to_cv2(msg, "bgr8")
    # Opencv processing image
    frame = cv.resize(frame, (640, 480))
    frame = cv.flip(frame, 1)
    # Create CompressedIamge 
    msg = CompressedImage()
    msg.header.stamp = rospy.Time.now()
    # Image data conversion
    msg.data = np.array(cv.imencode('.jpg', frame)[1]).tostring()
    # The image is processed and released directly
    pub_comimg.publish(msg)

4.2.5、Publish image

Start up command as following content:

xxxxxxxxxx
roslaunch transbot_visual astra_to_image.launch    # launch
rosrun transbot_visual astra_to_image.py           # py

 

ARTag
ROS+OpenCV application

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文