ros-perception / vision_opencv

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error with compressed image

anto1713 opened this issue · comments

i try to show image from my raspicam using compressed image, this my whole code

#!/usr/bin/env python
# Import ROS libraries and messages
import rospy
import numpy as np
from sensor_msgs.msg import Image, CompressedImage, CameraInfo
import cv2
from cv_bridge import CvBridge, CvBridgeError

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, "passthrough")
        
    except CvBridgeError, e:
        print 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()

but when i try this code, all i get is warning

[ERROR] [1635397160.930008]: bad callback: <function image_callback at 0x7f12ff7dab18>
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 23, in image_callback
    cv_image = bridge.imgmsg_to_cv2(img_msg, "passthrough")
  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

any idea why this is happening, is this because i use wrong version of opencv ?
i check my open cv version in python terminal

$ python
Python 2.7.15 |Anaconda, Inc.| (default, May  1 2018, 23:32:55) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.1-dev'

but when i use

$ pkg-config --modversion opencv
2.4.9.1

which the right open cv i have ?

curently i work in

  • ubuntu 16.04
  • ROS Kinetic
  • Python 2.7

The error is because you're trying to call bridge.imgmsg_to_cv2 which expects a sensor_msgs.msg.Image as the first argument, but you're passing a sensor_msgs.msg.CompressedImage as the first argument.

Unfortunately, as explained on this line in the tutorials, cv_bridge does not support CompressedImage in python.

I'm going to close this as I belive this explains the problem, but if you think the functionality to handle CompressedImages should be added, feel free to start a new issue.