Unable to publish an image using cvbridge [closed]
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.
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 -
What does
print cv_img
output?