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

how to show image via opencv using rospy ROS Kinetic

asked 2021-10-27 00:41:49 -0500

anto gravatar image

updated 2021-10-27 22:17:22 -0500

i want to stream my camera using opencv, this is my whole code

   #!/usr/bin/env python2.7
    import rospy
    from sensor_msgs.msg import Image, CompressedImage
    import cv2
    from cv_bridge import CvBridge, CvBridgeError
    import sys

    rospy.init_node('opencv_example', anonymous=True)
    bridge = CvBridge()

    def show_image(img):
        cv2.imshow("Image Window", img)
        cv2.waitKey(3)

   def image_callback(img_msg):

    try:
        np_arr = np.fromstring(img_msg.data, np.uint8)
        image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
        cv_image = bridge.imgmsg_to_cv2(img_msg, "bgr8")
    except CvBridgeError, e:
        rospy.logerr("CvBridge Error: {0}".format(e))

    show_image(cv_image)

    sub_image = rospy.Subscriber("/raspicam_node/image/compressed", CompressedImage, image_callback)
    cv2.namedWindow("Image Window", 1)

    while not rospy.is_shutdown():
          rospy.spin()

after i try to stream the compressed image from my raspicam node, all i get is this code:

  [ERROR] [1635390346.816659]: bad callback: <function image_callback at 0x7f9c224ccb18>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/anto/catkin_ws/src/opencv_example/src/opencv_ex.py", line 22, in image_callback
    cv_image = bridge.imgmsg_to_cv2(img_msg, "bgr8")
  File "/home/anto/catkin_ws/src/vision_opencv-kinetic/cv_bridge/python/cv_bridge/core.py", line 163, in imgmsg_to_cv2
    dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
AttributeError: 'CompressedImage' object has no attribute 'encoding'

currently, I am working with

  • Ubuntu 16.04
  • Ros Kinetic
  • Open CV 3.3.1
  • python 2.7
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-10-27 03:30:34 -0500

Ranjit Kathiriya gravatar image

Hello there,

You need to convert compress Image to Opencv by adding below code:

np_arr = np.fromstring(ros_data.data, np.uint8)
image_np = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
# image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) # OpenCV >= 3.0:

You can have a look at the following tutorial for it. http://wiki.ros.org/rospy_tutorials/T...

edit flag offensive delete link more

Comments

i updated my question and add that code, but all i get is the same answer from the terminal

anto gravatar image anto  ( 2021-10-27 22:15:28 -0500 )edit

take a look at this answer similar error: https://answers.ros.org/question/3131...

osilva gravatar image osilva  ( 2021-10-28 10:58:56 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-10-27 00:41:49 -0500

Seen: 547 times

Last updated: Oct 27 '21