10 """ Convert a image to a ROS compatible message 13 global publisher, imagePath
15 img = cv2.imread(imagePath, cv2.IMREAD_UNCHANGED)
21 rosimage = sensor_msgs.msg.Image()
22 if img.dtype.itemsize == 2:
23 if len(img.shape) == 3:
25 rosimage.encoding =
'bgr16' 27 rosimage.encoding =
'bgra16' 29 rosimage.encoding =
'mono16' 30 if img.dtype.itemsize == 1:
31 if len(img.shape) == 3:
33 rosimage.encoding =
'bgr8' 35 rosimage.encoding =
'bgra8' 37 rosimage.encoding =
'mono8' 40 rosimage.width = img.shape[1]
41 rosimage.height = img.shape[0]
42 rosimage.step = img.strides[0]
43 rosimage.data = img.tostring()
44 rosimage.header.stamp = rospy.Time.now()
45 rosimage.header.frame_id =
'map' 47 publisher.publish(rosimage)
52 global publisher, imagePath
53 rospack = rospkg.RosPack()
54 rospy.init_node(
'image_publisher')
55 imagePath = rospy.get_param(
'~image_path')
56 topicName = rospy.get_param(
'~topic')
57 publisher = rospy.Publisher(topicName, sensor_msgs.msg.Image, queue_size=10)
58 rospy.Timer(rospy.Duration(2), callback)
61 if __name__ ==
'__main__':
64 except rospy.ROSInterruptException:
pass