如何在没有 cvbridge 的情况下使用 opencv 在 ros 上发布图像主题

How can I publish an image topic using opencv on ros without cvbridge

我正在尝试使用 python 脚本发布实时视频。

#!/usr/bin/env python3

import rospy
from sensor_msgs import msg
import cv2
from sensor_msgs.msg import Image



def takes_data_from_camera():
     
    rate = rospy.Rate(10)  
    vid = cv2.VideoCapture(0)
    
    while not rospy.is_shutdown():
       
        ret, frame = vid.read()
        #video_bridge = bridge.cv2_to_imgmsg(frame, "passthrough")
        pub.publish(frame)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            vid.release()
            cv2.destroyAllWindows()
            break
    
  


if __name__ == '__main__':
    
    rospy.init_node('Server',anonymous = True)
    pub = rospy.Publisher('TestOps/Camera', Image, queue_size=10)
    takes_data_from_camera()
    

但是当我尝试 运行 时,出现以下错误

  File "/home/akash-j/catkin_ws/src/test_package/src/py-server.py", line 19, in takes_data_from_camera
    pub.publish(frame)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 879, in publish
    data = args_kwds_to_message(self.data_class, args, kwds)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/msg.py", line 122, in args_kwds_to_message
    return data_class(*args)
  File "/opt/ros/noetic/lib/python3/dist-packages/sensor_msgs/msg/_Image.py", line 77, in __init__
    super(Image, self).__init__(*args, **kwds)
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 354, in __init__
    raise TypeError('Invalid number of arguments, args should be %s' % str(self.__slots__) + ' args are' + str(args))
TypeError: Invalid number of arguments, args should be ['header', 'height', 'width', 'encoding', 'is_bigendian', 'step', 'data'] args are(array([[[209, 200, 193],

我不知道这里出了什么问题,我不想使用 cv bridge。代码在 cv bridge.i am using ubuntu 20 和 ros noetic 和 python 3

下运行良好

图像消息类型需要下面的数据结构。

>>> from sensor_msgs.msg import Image
>>> Image.__slots__
['header', 'height', 'width', 'encoding', 'is_bigendian', 'step', 'data']
>>> Image._slot_types
['std_msgs/Header', 'uint32', 'uint32', 'string', 'uint8', 'uint32', 'uint8[]']

而且好像只发布了视频 'data' 帧。

pub.publish(frame)

希望这足以让您进一步排除故障