Saving depth images from ROS messages

1,169 views
Skip to first unread message

Shravan M

unread,
Dec 17, 2014, 2:54:27 PM12/17/14
to ros-by-...@googlegroups.com
I modified a code given by ROS by example to view ROS messages  and I could view it perfectly. But when i try to save them, I get a black image. When  I googled , it told me that opencv cannot write 32FC1 images and it can show 8UC1 and 16UC1 images only. Does anybody know how to convert it? Or is there any other option that can be done?

Patrick Goebel

unread,
Dec 18, 2014, 9:05:05 AM12/18/14
to ros-by-...@googlegroups.com
Hi Shravan,

Can you post the code you are running so we can see where the problem is?

Thanks,
patrick

Shravan M

unread,
Dec 18, 2014, 12:42:40 PM12/18/14
to ros-by-...@googlegroups.com
This is my code . I can convert it into a numpy array and normalize it. but when I write it as a 16 bit image , it gets distorted. But the 8 bit depth image is fine. But for further operations I would prefer a 16 bit image .So is there any other possibility to do this?




import roslib
from matplotlib import pyplot as plt
roslib.load_manifest('beginner_tutorials')
import sys
import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import numpy as np
i=0
np.set_printoptions(threshold='nan')
def callback(data):
    try:
            cv_image = CvBridge().imgmsg_to_cv2(data, "32FC1")
    except CvBridgeError, e:
            print e   
    global i
    cv_image1 = np.array(cv_image, dtype=np.float)
    cv2.normalize(cv_image1, cv_image1, 0, 1, cv2.NORM_MINMAX)
    cv_image1=cv_image1*2**16
    cv2.imwrite("/home/admin123/bagfiles/im"+str(i)+".jpg",cv_image1)
    i+=1
    cv2.waitKey(3)
def video():

    rospy.init_node('video', anonymous=True)
    im=rospy.Subscriber('/camera/depth_registered/image_rect', Image, callback)
   
    rospy.spin()
if __name__ == '__main__':
    video()
Reply all
Reply to author
Forward
0 new messages