【问题标题】:Process depth image message from ROS with openCV使用 openCV 处理来自 ROS 的深度图像消息
【发布时间】:2021-02-11 20:05:23
【问题描述】:

所以我目前正在编写一个 python 脚本,它应该接收 ros 图像消息,然后将其转换为 cv2,以便我可以做进一步的处理。现在程序只接收一个图像,然后将其输出到一个小窗口中,并将其保存为 png。 这是我的代码:

#! /usr/bin/python
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,它告诉我当前 ros 消息的编码类型。 cv2.imshow 的工作方式与前置摄像头图片的工作方式完全相同,它显示的效果与我使用 ros image_view 时的效果相同,但是一旦我尝试使用深度图像,我就会得到一张全黑或全白的图片,不像image_view 向我展示了什么

Here the depth image i get when i use image_view

Here the depth image i receive when i use the script and cv2.imshow

有没有人有使用 cv2 处理深度图像的经验,并且可以帮助我让它也处理深度图像? 我真的很感激任何帮助:)

最好的问候

【问题讨论】:

  • 我对 ROS 真的一无所知,但会注意到 PNG 格式无法存储 32 位浮点数。我认为您需要编写可以存储 32 位浮点数的 TIFF,或者转换为 PNG 可以存储的 16 位无符号数。

标签: image opencv image-processing ros


【解决方案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()

【讨论】:

  • 你好,我尝试了这两种方法,第一个带有颜色图的结果是蓝色图像,第二个结果是黑色图像
  • 我也尝试过实现它。结果相同,只是一张蓝色图片
猜你喜欢
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多