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

Create a bag out of 16 bit images

asked 2019-01-18 18:16:26 -0500

Avner gravatar image

updated 2019-01-21 16:22:17 -0500


I want to store a mono16 depth image to a bag The instructions here work for bit depth of 8 bits per pixel (either for mono or rgb8) but not for mono16 or 32FC1.

I'm getting an error from

field data[] exceeds specified width [uint8]

Where the problem occurs, the field_val is 1574 and the maxval is 256

How can I serialize a 16 bit image into bytes, and write it into the bag?

Thanks, Avner


Thanks for your answer @PeteBlackerThe3rd. With your answer here and the answer here I was able to use the example code as a base with the following changes:

> from PIL import Image as Image1
<         cv2.imwrite("./dframe"+str(i)+".jpg", image_normal)
>         # ok - workaround for bug in Pillow ( for uint16 (via uint32)
>         ar32 = numpy_image.astype(np.uint32)
>         im1 = Image1.fromarray(ar32) # or more verbose as Image.fromarray(ar32, 'I')
<         rospy.Subscriber("/camera/depth_registered/image_raw",Image,checkdepth)
>         rospy.Subscriber("/camera/camera/depth/image",Image,checkdepth)

With this, I successfully extracted 16 bit depth images from the bag into 16 bits-per-pixel image files.

edit retag flag offensive close merge delete



Instead of putting your solution in the question, can you please put it as an answer? That'll make it easier for others to find

jayess gravatar image jayess  ( 2019-08-15 20:20:53 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-01-19 05:54:17 -0500

The answer you linked to is from a very long time ago and attempts to manually build up the ROS image message manually, although this can be done it isn't anything I'd recommend.

You can use the cv_bridge package to produce ROS image messages from CV images and vice versa. This package will take care of various encodings for you including mono16 and 32FC1 which you mentioned and includes a python example very similar to what you're trying to do. You will have to construct Open CV images using cv::Mat objects if you're not doing that already.

Hope this helps.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-01-18 18:16:26 -0500

Seen: 613 times

Last updated: Jan 21 '19