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



所以我目前正在编写一个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向我显示

这里是我使用image_view 时获得的深度图像

这里是我使用脚本和cv2.imshow 时收到的深度图像

有人有使用cv2处理深度图像的经验吗?可以帮助我处理深度图像吗?我真的很感激任何帮助:(

向致以最良好的问候

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

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

最新更新