订阅Image Topic但不输出图片

Subscribing to Image Topic but doesn't output images

我有以下代码,我一直在努力从中获取图像。有一个主题“CompressedImage”,其中包含来自我的机器人相机的 jpeg 图像。以下是我的尝试。终端只给我打印的版本并退出。

#!/usr/bin/env python
import cv2
import numpy as np
from timeit import default_timer as timer
from std_msgs.msg import Float64
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import rospy

import sys
print(sys.version)
print(cv2.__version__)

height = 480
width = 640
global_frame = np.zeros((height,width,3), np.uint8)

def camera_callback(data):

    bridge = CvBridge() 

    try:
        global_frame = bridge.compressed_imgmsg_to_cv2(data)
    except CvBridgeError as e:
        print(e)

    height, width, channels = global_frame.shape
    # print(height)
    cv2.imshow("Original", global_frame)

def lane_pose_publisher():
    # Set the node name
    rospy.init_node('lane_pose_publisher', anonymous=True)

    rospy.Subscriber('/camera/image_raw/compressed', CompressedImage, camera_callback, queue_size = 1)
    # set rate
    rate = rospy.Rate(1000) # 1000hz


if __name__ == '__main__':
    try:
        lane_pose_publisher()
    except rospy.ROSInterruptException:
        pass

这是因为您没有旋转 ROS。查看 corresponding ROS tutorial to create a subscriber 其中

# spin() simply keeps python from exiting until this node is stopped
rospy.spin()

被调用以确保 ROS 可以完成他的工作(调用回调,...)。

所以你应该更换你的台词

# set rate
rate = rospy.Rate(1000) # 1000hz

与旋转的。

你还应该添加

cv2.waitKey(1)

调用后

cv2.imshow("Original", global_frame)

以确保显示图像。