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

Export bag of CompressedImage to disk without decoding

asked 2018-09-19 09:25:43 -0500

autonomy gravatar image

I have a bag of compressed images (/camera/image/compressed). I wrote a Python script which reads the bagfile using the Python API and writes JPEGs to disk in this way:

cv_img = bridge.compressed_imgmsg_to_cv2(msg, desired_encoding="passthrough")
cv2.imwrite(file_name, cv_img)

However, the image quality is not that great. I looked at the source and the compressed_imgmsg_to_cv2 function calls cv2.imdecode. So, essentially, I'm compressing the image twice. Is there a way around this using cvbridge? Should I just use PNG instead?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-10-16 19:11:37 -0500

Hello @autonomy,

Don't worry, the cv_bridge method isn't compressing your images twice, otherwise, we would open a PR now. So, you are right regarding the cv2.imdecode use, however, the cv_bridge just uses it to read an image from the specified buffer in the memory as you can check here.

Just for completing the answer we can analyze the snippet of the code available here:

str_msg = cmprs_img_msg.data
buf = np.ndarray(shape=(1, len(str_msg)) dtype=np.uint8, buffer=cmprs_img_msg.data)
im = cv2.imdecode(buf, cv2.IMREAD_ANYCOLOR)

At the first line, it just initializing the compressed data provided to a variable. At the second, it assigns the compressed data to a buffer method (buffer=cmprs_img_msg.data). At the third, it uses the cv2.imdecode to read the buffer using the cv2.IMREAD_ANYCOLOR, which just guarantees the use of any possible color format.

PS: In the second line, It could have used the variable defined before (str_msg), I don't know why it didn't.

So, don't worry about that keep pushing you ROS knowledge!

Waiting for your reply.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-09-19 09:25:43 -0500

Seen: 566 times

Last updated: Oct 16 '18