ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Can we get Image type(16UC1) byte array from the compressed byte array(jpeg compressed)?

asked 2019-02-23 12:11:51 -0500

akcast gravatar image

updated 2019-02-23 12:13:03 -0500


I am using ROS Sharp to send compressedImages from a ROS Kinetic Ubuntu Xenial workstation with aligned depth to color information. Then, I am using 'OpenCV for Unity' to decode the compressedImage byte array and was hoping to receive 16UC1 data but instead I am receiving 8UC1 data.

So my question is, is it possible to decompress a ROS compressedImage to retrieve back the original Image type byte array? Or will there always be a loss like in my case, i.e. 16UC1 is the original image but the decoded compressedImage is 8UC1 ?

edit retag flag offensive close merge delete


This is certainly possible. Compressed image transport supports 16 bit images. How are you producing the compressed image topic initially and how are you converting it to a cv::Mat in the subscriber?

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-02-23 17:46:50 -0500 )edit

Firstly, I subscribe to "/camera/aligned_depth_to_color/image_raw/image raw/compressed" topic (originally 16UC1) which is published from realsense ros node.

Then, using OpenCV for Unity I use: imgcodec.imdecode(new MatOfByte(, CV_LOAD_IMAGE_ANYDEPTH)

akcast gravatar image akcast  ( 2019-02-24 14:49:59 -0500 )edit

...resulting in a 307200 byte array i.e. 8UC1. The imdecode function I think gives 16-bit values if there is a corresponding depth in the image, else it returns a 8-bit value, when using the CV_LOAD_ANYDEPTH flag. So, if the compressedImage does contain 16-bit values, im not sure why I get 8-bit?

akcast gravatar image akcast  ( 2019-02-24 14:56:52 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-02-25 05:26:00 -0500

Firstly you should avoid subscribing directly to the compressed topic, and use the image_transport package to deal with any decompression transparently.

The example shown in the image_transport tutorials shows how to set this up. You can request that it uses the compressed topic if possible using a TransportHints object as shown below:

image_transport::Subscriber itSub;
image_transport::ImageTransport it(n);
itSub = it.subscribe("base_img_topic",1,&imgCallback,image_transport::TransportHints("compressed"));

This way you never have to deal with the compression or decompression directly. If you're still getting 8 bit images using this method then there may be a problem with the publishing node, that means it's publishing 8 bit images. But hopefully this will get this working.

edit flag offensive delete link more


Hi, @PeteBlackerThe3rd Thanks for your response!

I would be decompressing the compressedImage topic in Unity and I am using ROS Sharp for this, which I believe at the moment does not have support for the image_transport' package and can only subscribe to regular ROS topics.

Which is why I..

akcast gravatar image akcast  ( 2019-02-25 06:37:01 -0500 )edit

...had this question, and wanted to know if it can be done just by subscribing to the compressedImage topic and decoding using OpenCV ? Also, is jpeg compression suitable for retrieving the 16UC1 byte representation or is PNG required for this ? At the moment I am using jpeg.

akcast gravatar image akcast  ( 2019-02-25 06:43:25 -0500 )edit

I'm about 80% certain that compressed image transport uses PNG for 16 bit images, I don't believe the JPEG compressor in openCV supports 16 bit. This may be where your problem lies.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-02-25 08:34:04 -0500 )edit

Question Tools

1 follower


Asked: 2019-02-23 12:11:51 -0500

Seen: 1,242 times

Last updated: Feb 25 '19