使用 openCV 处理来自 ROS 的深度图像消息

问题描述 投票:0回答:1

我想接收一个ros图像消息,然后将其转换为cv2。 现在,程序只是接收图像,然后将其输出到一个小窗口中,并将其另存为

png

import rospy 
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2

bridge = CvBridge()

def image_callback(msg):
        print("Received an image!")
        print(msg.encoding)

try:
    # Convert your ROS Image message to OpenCV2
    # Converting the rgb8 image of the front camera, works fine
    cv2_img = bridge.imgmsg_to_cv2(msg, 'rgb8')
    # Converting the depth images, does not work 
    #cv2_img = bridge.imgmsg_to_cv2(msg, '32FC1')        
except CvBridgeError, e:
    print(e)

else:
    # Save your OpenCV2 image as a png
    cv2.imwrite('camera_image.png', cv2_img)
    cv2.imshow('pic', cv2_img)
    cv2.waitKey(0)

def main():
    rospy.init_node('image_listener')
    #does not work:
    #image_topic = "/pepper/camera/depth/image_raw"
    #works fine:
    image_topic = "/pepper/camera/front/image_raw"
    rospy.Subscriber(image_topic, Image, image_callback)
rospy.spin()


if __name__ == '__main__':
        main()

问题是我的代码不适用于深度图像。 为了获得正确的编码,我使用

msg.encoding
cv2.imshow
带有深度图像的是全黑或全白的图片。

这是我使用 image_view 时获得的深度图像 Here the depth image i get when i use image_view

这是我使用脚本和 cv2.imshow 时收到的深度图像 Here the depth image i receive when i use the script and cv2.imshow

image opencv image-processing ros
1个回答
1
投票

您可以尝试通过以下方式获取深度图像,

    import rospy
    from cv_bridge import CvBridge, CvBridgeError
    from sensor_msgs.msg import Image
    import numpy as np
    import cv2

    def convert_depth_image(ros_image):
        cv_bridge = CvBridge()
        try:
            depth_image = cv_bridge.imgmsg_to_cv2(ros_image, desired_encoding='passthrough')
        except CvBridgeError, e:
            print e
        depth_array = np.array(depth_image, dtype=np.float32)
        np.save("depth_img.npy", depth_array)
        rospy.loginfo(depth_array)
        #To save image as png
        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
        cv2.imwrite("depth_img.png", depth_colormap)
        #Or you use 
        # depth_array = depth_array.astype(np.uint16)
        # cv2.imwrite("depth_img.png", depth_array)


    def pixel2depth():
        rospy.init_node('pixel2depth',anonymous=True)
        rospy.Subscriber("/pepper/camera/depth/image_raw", Image,callback=convert_depth_image, queue_size=1)
        rospy.spin()

    if __name__ == '__main__':
        pixel2depth()
© www.soinside.com 2019 - 2024. All rights reserved.