【发布时间】: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