gpt4 book ai didi

image - 使用openCV从ROS处理深度图像消息

转载 作者:行者123 更新时间:2023-12-02 16:30:56 24 4
gpt4 key购买 nike

所以我目前正在编写一个应该接收ros图像消息的python脚本,然后将其转换为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处理深度图像的经验,并且可以帮助我使它也可以处理深度图像吗?
我真的很感谢您的帮助:)
最好的祝福

最佳答案

您可以按照以下方式尝试获取深度图像,

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

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()

关于image - 使用openCV从ROS处理深度图像消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64590101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com