How to solve Type Error while converting images using cv_bridge?

asked 2019-08-15 17:35:25 -0500

ambareesh gravatar image

Implementing a variant of the example code from : http://wiki.ros.org/cv_bridge/Tutoria..., I'm facing trouble at line 22 of the snippet:

 cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")

in the subscriber call back.

The Error stack is as follows:

[ERROR] [1565895663.419871]: bad callback: <bound method image_converter.callback of <__main__.image_converter instance at 0x7f0ff14c5ef0>>
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/home/florian/catkin_dlc_ws/src/project/src/dlc_node.py", line 22, in callback
 cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/core.py", line 171, in imgmsg_to_cv2
dtype=dtype, buffer=img_msg.data)
TypeError: buffer is too small for requested array

I checked the corresponding file from the core.py file imgmsg_to_cv2 function,

    def imgmsg_to_cv2(self, img_msg, desired_encoding = "passthrough"):
     import cv2
     import numpy as np
     dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
     dtype = np.dtype(dtype)
     dtype = dtype.newbyteorder('>' if img_msg.is_bigendian else '<')
     if n_channels == 1:
        im = np.ndarray(shape=(img_msg.height, img_msg.width),
                       dtype=dtype, buffer=img_msg.data)
     else:
        im = np.ndarray(shape=(img_msg.height, img_msg.width, n_channels),
                       dtype=dtype, buffer=img_msg.data)
     #If the byt order is different between the message and the system.
     if img_msg.is_bigendian == (sys.byteorder == 'little'):
        im = im.byteswap().newbyteorder()

     if desired_encoding == "passthrough":
        return im

     from cv_bridge.boost.cv_bridge_boost import cvtColor2

     try:
        res = cvtColor2(im, img_msg.encoding, desired_encoding)
     except RuntimeError as e:
        raise CvBridgeError(e)

     return res

The error is in line im = np.ndarray(shape=(img_msg.height, img_msg.width, n_channels), dtype=dtype, buffer=img_msg.data)

where apparently img_ms.data is too small for the shape prescribed. But this seems unlikely bcause img_msg.height, img_msg.width are from the same message.

I'm not sure how to debug this, could someone shed any light on this?

Using Melodic+ Ubuntu18.04+ Python2, Image stream is from playing a rosbag

Thank you!

edit retag flag offensive close merge delete

Comments

Hi, I have the same exact issue, and am not sure what is happening in my code. Have you resolved this issue?

Yingchen Ma gravatar image Yingchen Ma  ( 2020-09-05 22:05:15 -0500 )edit