CV_bridge cannot use together with python object oriented programming?

asked 2020-01-09 03:28:06 -0500

mingcheng gravatar image

updated 2020-01-09 03:52:32 -0500

gvdhoorn gravatar image


May I ask why in the portion of my program below, I have error of "

Traceback (most recent call last):
  File "", line 81, in <module>
    depthCentre = init.showDepth_analysis()
  File "", line 39, in showDepth_analysis
    depth_image = bridge.imgmsg_to_cv2(self.showDepth, "passthrough")
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/", line 163, in imgmsg_to_cv2
    dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
AttributeError: 'NoneType' object has no attribute 'encoding"

The portion of program:

class MyDistance:
    def __init__(self):
        self.showDepth = None
        self.depthImage_sub = rospy.Subscriber("/camera/aligned_depth_to_color/image_raw",Image, self.showDepth_callback, queue_size=1, buff_size=2**24)

    def showDepth_callback(self,data):
        self.showDepth = data

    def showDepth_analysis(self):
        depth_image = bridge.imgmsg_to_cv2(self.showDepth, "passthrough")

I cannot really figure out which place of above have gone wrong. I have tried to use bridge.imgmsg_to_cv2 in another code which did not use OOP(object oriented programming), which is fine. So may I know that is it because cv_bridge cannot use together with OOP or is the above portion of code have some mistake. Thank you for reading this post.

edit retag flag offensive close merge delete


cv_bridge can definitely be used with python objects, the problem you're having is just a bug with your code.

It's hard to say for sure without seeing all your code. However you initialise self.showDepth to None and your error is being caused because the showDepth_analysis function is being called while it is still set to None. It's very likely that you're calling this function before the callback has been executed.

Also why have you set a buffer size of 16.7 million for the subscriber? I would expect a value of between 1 - 10 for an image topic such as this.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2020-01-09 17:03:10 -0500 )edit