ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I now is an old thread. But just for the others that need to store a depth image (from camera or from rosbag) to a png file. This is what you should do on the depth image subscription callback or in any place you want to perform this operation:

def callback(self, data):
    try:
        # The depth image is a single-channel float32 image
        # the values is the distance in mm in z axis
        depth_image = self.bridge.imgmsg_to_cv(data, '32FC1')
        # Convert the depth image to a Numpy array since most cv2 functions
        # require Numpy arrays.
        depth_array = np.array(depth_image, dtype=np.float32)
        # Normalize the depth image to fall between 0 (black) and 1 (white)
        cv2.normalize(depth_array, depth_array, 0, 1, cv2.NORM_MINMAX)
        # At this point you can display the result properly:
        # cv2.imshow('Depth Image', depth_display_image)
        # If you write it as it si, the result will be a image with only 0 to 1 values.
        # To actually store in a this a image like the one we are showing its needed
        # to reescale the otuput to 255 gray scale.
        cv2.imwrite('capture_depth.png',frame*255)
    except CvBridgeError, e:
        print e

I'm using at the moment ubuntu 12.04 and Ros Hydro

I now is an old thread. But just for the others that need to store a depth image (from camera or from rosbag) to a png file. This is what you should do on the depth image subscription callback or in any place you want to perform this operation:operation with python:

def callback(self, data):
    try:
        # The depth image is a single-channel float32 image
        # the values is the distance in mm in z axis
        depth_image = self.bridge.imgmsg_to_cv(data, '32FC1')
        # Convert the depth image to a Numpy array since most cv2 functions
        # require Numpy arrays.
        depth_array = np.array(depth_image, dtype=np.float32)
        # Normalize the depth image to fall between 0 (black) and 1 (white)
        cv2.normalize(depth_array, depth_array, 0, 1, cv2.NORM_MINMAX)
        # At this point you can display the result properly:
        # cv2.imshow('Depth Image', depth_display_image)
        # If you write it as it si, the result will be a image with only 0 to 1 values.
        # To actually store in a this a image like the one we are showing its needed
        # to reescale the otuput to 255 gray scale.
        cv2.imwrite('capture_depth.png',frame*255)
    except CvBridgeError, e:
        print e

I'm using at the moment ubuntu 12.04 and Ros Hydro