Welcome to ROSMASTER M3 repository
03.Depth Pseudo-Color Image
Depth Pseudo-Color Image
1. Course Content
Run example programs, convert black and white depth images to pseudo-color images by subscribing to depth color image topics and using OpenCV image processing.
2. Program Startup
For Raspberry Pi 5 users, if the ROS container is not started, you need to start the container first,
bringup_m3Open a terminal inside the container,
xxxxxxxxxxexec_m3- Enter command in terminal:
xxxxxxxxxxros2 launch m3_bringup depthcam_demo.launch.pyAfter startup, as shown in the figure below:

Different colors represent differences in depth information.
3. Source Code Analysis
- Source code path:
xxxxxxxxxx~/yahboomM3_ws/code_ws/src/m3_common_demo/m3_common_demo/depthcamera/GetDepthColor.py- Import required library files
xxxxxxxxxxfrom cv_bridge import CvBridgeimport cv2from rclpy.node import Nodeimport rclpyfrom sensor_msgs.msg import Image- Define depth image decoding format
xxxxxxxxxxencoding = ['16UC1', '32FC1']- Define subscriber for depth image topic
xxxxxxxxxxself.sub_depth = self.create_subscription(Image,"/camera/depth/image_raw",self.get_DepthImgCallBack,100)- Define self.depth_bridge for converting message format to image format that OpenCV can process
xxxxxxxxxxself.depth_bridge = CvBridge()- Convert image data message to image
xxxxxxxxxxdepth_image = self.depth_bridge.imgmsg_to_cv2(depth_frame, encoding[1])- Call OpenCV image processing function cv2.applyColorMap to convert depth map
xxxxxxxxxxdepth_to_color_image = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=1.0), cv2.COLORMAP_JET)
cv2.convertScaleAbs: Performs linear transformation on image data and converts it to 8-bit unsigned integer (uint8) format, with scaling factor as the scaling factor.
cv2.applyColorMap: A function that converts single-channel grayscale images to pseudo-color images to enhance visualization effects through color mapping. This parameter has two variables: the first variable is a single-channel 8-bit or floating-point image (grayscale image), and the second variable is the color mapping table, with the following optional values:
xxxxxxxxxxcv2.COLORMAP_AUTUMN # Red-Orange-Yellowcv2.COLORMAP_BONE # Black-White (grayscale with blue tint)cv2.COLORMAP_HSV # HSV color spacecv2.COLORMAP_JET # Blue-Cyan-Yellow-Red (classic heat map)cv2.COLORMAP_WINTER # Blue-Greencv2.COLORMAP_RAINBOW # Rainbow colors (Red-Purple)cv2.COLORMAP_OCEAN # Dark Blue-Light Bluecv2.COLORMAP_SUMMER # Green-Yellowcv2.COLORMAP_SPRING # Magenta-Yellowcv2.COLORMAP_COOL # Cyan-Magentacv2.COLORMAP_HSV # HSV color space cyclecv2.COLORMAP_PINK # Pink tonescv2.COLORMAP_PARULA # MATLAB Parula stylecv2.COLORMAP_VIRIDIS # Modern scientific color scale (Green-Purple)cv2.COLORMAP_INFERNO # Black-Red-Yellow-White (high contrast)cv2.COLORMAP_PLASMA # Purple-Red-Yellowcv2.COLORMAP_MAGMA # Black-Red-Whitecv2.COLORMAP_TWILIGHT # Blue-Pink-White gradient