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

Unable to store the depth map in 32FC1 format?

asked 2017-02-23 07:14:21 -0500

Joy16 gravatar image

Hello!

I want to get the depth data from Creative 3d Senz camera. I am not able to store the depth map in its correct format. Please see the snippet below.

  1 #!/usr/bin/env python
  2 
  3 import sys
  4 import cv2
  5 import rospy
  6 from std_msgs.msg import String
  7 from sensor_msgs.msg import Image
  8 from sensor_msgs.msg import PointCloud2
  9 from cv_bridge import CvBridge, CvBridgeError

 10 
 11 class ImgCapture:
 12     def __init__(self):
 13         self.CvImg = CvBridge()
 14         skImg = rospy.Subscriber("/softkinetic_camera/rgb/image_color",Image, self.callbackImg)  
 15         skDep = rospy.Subscriber("/softkinetic_camera/depth/image_raw",Image, self.callbackDepth)
 16 
 17     def callbackImg(self, data):
 18         #print "Processing RGB"
 19         try:
 20               NewImg = self.CvImg.imgmsg_to_cv2(data,"bgr8")
 21               print data.encoding
 22               print
 23               cv2.imwrite("rgb.png", NewImg)
 24         except CvBridgeError as e:
 25               print(e)
 26 
 27     def callbackDepth(self, data):
 28         #print "Depth"
 29         try:
               # THIS LINE GIVES ME THE FOLLOWING ERROR
 30           NewImg = self.CvImg.imgmsg_to_cv2(data,"32FC1")                           
 31           print data.encoding
 32           print
 33           cv2.imwrite("depth.png", NewImg)
 34         except CvBridgeError as e:
 35           print(e)
 36

In the above code snippet, callbackDepth() gives me the following error:

[ERROR] [WallTime: 1487855299.273887] bad callback: <bound method ImgCapture.callbackDepth of <__main__.ImgCapture instance at 0x7fc64712e320>>
Traceback (most recent call last):
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback
    cb(msg)
  File "/home/lerrel/ros_ws/src/baxter_examples/scripts/image_capture.py", line 33, in callbackDepth
    cv2.imwrite("depth.png", NewImg)
TypeError: img data type = 14 is not supported

If I change line 30. to 30 NewImg = self.CvImg.imgmsg_to_cv2(data,"passthrough") , here is no error but the image saved is just black with no depth information.

Can you please help me understand what is going wrong here?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2017-02-23 07:26:06 -0500

Joy16 gravatar image

updated 2018-12-16 20:14:02 -0500

jayess gravatar image

Okay so here it goes! I followed the solution by @E1000ii given here and it worked

I am copy-pasting my changed code, based on the solution provided above:

def callbackDepth(self, data):
    print "all is well"
    try:
        NewImg = self.CvImg.imgmsg_to_cv2(data, "passthrough")
        depth_array = np.array(NewImg, dtype=np.float32)
        cv2.normalize(depth_array, depth_array, 0, 1, cv2.NORM_MINMAX)
        print data.encoding
        print
        cv2.imwrite("depth.png", depth_array*255)
    except CvBridgeError as e:
        print(e)
edit flag offensive delete link more
0

answered 2021-09-09 11:58:16 -0500

lucasw gravatar image

Saving as tif will preserve the 32FC1 floating point values:

cv2.imwrite("depth.tif", NewImg)
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2017-02-23 07:14:21 -0500

Seen: 2,484 times

Last updated: Sep 09 '21