Ask Your Question
0

Unable to publish an image using cvbridge [closed]

asked 2018-02-06 23:07:52 -0500

dpakshimpo gravatar image

updated 2018-02-06 23:22:00 -0500

Hello,

I am trying to create a simple image publisher from OpenCV to ROS, I am following the given tutorial. http://wiki.ros.org/cv_bridge/Tutoria...

However, when I run the program I get the following error:

ROS distribution: Kinetic Kame OpenCV: 3.4.0

Traceback (most recent call last):

File "/home/deepak/catkin_ws/src/python_learning/src/scripts/image_pub.py", line 40, in <module> main() File "/home/deepak/catkin_ws/src/python_learning/src/scripts/image_pub.py", line 36, in main image_publisher_obj.img_pub_node() File "/home/deepak/catkin_ws/src/python_learning/src/scripts image_pub.py", line 24, in img_pub_node ros_img = self.cvbridge_obj.cv2_to_imgmsg(cv_img, encoding="bgr8")    File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 246, in cv2_to_imgmsg raise TypeError('Your input type is not a numpy array') TypeError: Your input type is not a numpy array

My code is:

class ImagePublisher:
""" Class to define member functions for image publisher """

def __init__(self):
    """ Constructor """
    self.image_pub = rospy.Publisher("image_pub_topic", img,queue_size=200)
    self.cvbridge_obj = CvBridge()

def img_pub_node(self):
    """ Publisher helper function """
    cv_img = cv2.imread('sizeDetect.jpg')

    # Convert this to ROS image format
    ros_img = self.cvbridge_obj.cv2_to_imgmsg(cv_img, encoding="bgr8")
    try:
        self.image_pub.publish(ros_img)
        rate = rospy.Rate(10)
        rate.sleep()
    except CvBridgeError as bridgerr:
        print(bridgerr)

def main():
    "Main function"
    image_publisher_obj = ImagePublisher()
    rospy.init_node('img_pub_node', anonymous=True)
    image_publisher_obj.img_pub_node()
    rospy.spin()

if __name__ == '__main__':
    main()

My understanding is that OpenCV in Python will return numpy array and C++ will return Mat class. Then why is he complaining that it is not a numpy array.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by dpakshimpo
close date 2018-02-12 02:09:29.865735

Comments

P.S: I also tried converting the cv_img object as a numpy array before passing to cv2_to_imgmsg, however, then it fails with the error -

~/cv_bridge/core.py", line 248, in cv2_to_imgmsg 
img_msg.height = cvim.shape[0]  IndexError: tuple index out of range
dpakshimpo gravatar image dpakshimpo  ( 2018-02-06 23:17:12 -0500 )edit

What does print cv_img output?

lucasw gravatar image lucasw  ( 2018-02-07 08:23:53 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-02-07 08:56:43 -0500

lucasw gravatar image

updated 2018-02-07 08:58:39 -0500

The most likely explanation is that cv_img = cv2.imread('sizeDetect.jpg') failed to load sizeDetect.jpg because it isn't in the same path as where the script is being run from, and cv_img is None. imread doesn't throw when it fails, just returns None.

If sizeDetect.jpg is in the same package as your python script you could use rospack like this:

import rospkg

...
    rospack = rospkg.RosPack()
    image_path = rospack.get_path('my_package_name') + '/images/'
    cv_img = cv2.imread(image_path + 'sizeDetect.jpg')

This is a complete example:

https://github.com/lucasw/rviz_textur...

edit flag offensive delete link more

Comments

Thank you so much lucasw. It solved the problem. It looks stupid in hindsight, that I did not think of this :) I still gave the full path in my code instead of using rospack. I am sure, rospack is the right way to go about in real world.

dpakshimpo gravatar image dpakshimpo  ( 2018-02-07 23:57:18 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-06 23:07:52 -0500

Seen: 1,885 times

Last updated: Feb 07 '18