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

Create a bag out of 16 bit images

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

Avner gravatar image

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

Hi,

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 bag.py

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


EDIT1

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:

diff grabdepth.orig.py grabdepth.new.py 
> 
> from PIL import Image as Image1
---
> 
47,48c50,57
<         np.save("./dframe"+str(i)+".npy",numpy_image)
<         cv2.imwrite("./dframe"+str(i)+".jpg", image_normal)
---
>         np.save("/tmp/tmp1/dframe"+str(i)+".npy",numpy_image)
> 
>         # ok - workaround for bug in Pillow (https://github.com/python-pillow/Pillow/issues/2970) for uint16 (via uint32)
>         ar32 = numpy_image.astype(np.uint32)
>         im1 = Image1.fromarray(ar32) # or more verbose as Image.fromarray(ar32, 'I')
> 
>         im1.save("/tmp/tmp1/dframe"+str(i)+".png")
---
> 
58,59c67,68
<         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

Comments

1

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 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

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

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

Stats

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

Seen: 731 times

Last updated: Jan 21 '19