Module my_ros_utils.image_conversion

ROS to OpenCV image converter

This script can be imported as a module and contains the following public methods:

Functions

def check_for_type(topic)

Check wheter a topic is publishing Image or CompressedImage messages.

Args

topic : string
The name of the topic to check.

Returns

A tuple containing the message type and the image encoding.

def decode_CompressedImage_RGB(msg)

Convert a ROS Image message into an OpenCV image. From: https://stackoverflow.com/questions/69718148/access-image-via-opencv-in-python-ros-kinetic

Args

msg : sensor_msgs.CompressedImage
The ROS message to convert.

Returns

RGB data in a np.ndarray in np.uint8 format.

def decode_CompressedImage_depth(msg, header_size=12)

Convert a ROS CompressedImage message with depth information into a numpy storing the depth information in the specified measurement unit. From: https://answers.ros.org/question/249775/display-compresseddepth-image-python-cv2/

Args

msg : sensor_msgs.CompressedImage
The ROS CompressedImage message to be converted.
header_size : int, optional
The size of the message header in bytes. Defaults to 12.

Raises

Exception
Raised if the compression type (extracted from the format field of the message) does not have the expected compressedDepth value.
Exception
Raised if the compressed image cannot be decoded. This problem maybe due to a wrong header_size value. The user is prompted to try to adjust that value.
Exception
Raised if the decoding from the specified datatype is not implemented.

Returns

Depth data in a np.ndarray in np.float32 format.

def decode_Image_RGB(msg)

Convert a ROS Image message into an OpenCV image.

Args

msg : sensor_msgs.Image
The ROS message to convert.

Returns

RGB data in a np.ndarray in np.uint8 format.

def decode_Image_depth(msg)

Convert a ROS Image message with depth information into a numpy array.

Args

msg : sensor_msgs.Image
The ROS Image message to be converted.

Returns

Depth data in a np.ndarray in np.float32 format.