How to store the depth data from kinect(/camera/depth_registrered/image_raw) as gray scale image?
I want to get the depth data from kinect by receiving sensor_msgs/Image from topic /camera/depth_registrered/image_raw(reference), I use cv_bridge to store the data to a gray scale image.
I use this snippet of code to receive sensor_msgs/Image from topic /camera/depth_registrered/image_raw, change it to gray scale image by cvBridge, and then store the image to a jpeg file.
#!/usr/bin/env python
import roslib
roslib.load_manifest('store_stereo_image')
import sys
import rospy
import cv
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
class image_converter:
def __init__(self):
self.bridge = CvBridge()
self.image_sub = rospy.Subscriber("/camera/depth_registered/image_raw",Image,self.callback)
def callback(self,data):
try:
cv_image = self.bridge.imgmsg_to_cv(data, "mono8")
cv.SaveImage("depth_camera_msg.jpg", cv_image)
print "image saved!"
except CvBridgeError, e:
print e
def main(args):
ic = image_converter()
rospy.init_node('image_converter', anonymous=True)
try:
rospy.spin()
except KeyboardInterrupt:
print "Shutting down"
if __name__ == '__main__':
main(sys.argv)
This is the jpeg file I stored, I suppose the value in this picture should vary from 0 to 255. However, I found the values in this picture are either 0 or 255(only black and white).
I use rostopic echo /camera/depth_registered/image_raw
to check, I found that the data it published vary from 0~255, but I don't know why the image is only black and white~
Did I miss something? Thanks in advance.
---EDIT---
Thanks to DamienJadeDuff's comment, I learned that the raw data from msg sensor_msgs/Image is uint8, and the data contained in /camera/depth_registered/image_raw is uint16 depths in mm. Does this means depth data is retrieved from combining two uint8 data?
From memory, the depth image is a 32 bit float. Perhaps the rostopic echo is only showing the byte level data?
To respond to your edit, Ricky; sorry I got the encoding wrong for your message (depth_registered/image_raw), but why don't you use depth_registered/image instead? See http://www.ros.org/wiki/depth_image_proc --- then you can use jelfring's code for image type 32FC1.
Also, yes, check the encoding field in the message; that gives a cue as to how the data should be interpreted. But you should be able to use the cv_bridge library to interpret the message. See http://www.ros.org/wiki/cv_bridge