Problem of saving depth image
I try to use the code below to save one frame of depth iamge from topic /camera/depth/image_raw, but the problem is I could only get a image looks black. When I read it in Matlab, the matrix elements are basically from number 1 to 7, I guess that's why it looks black. But when I rosrun the image_view (camera/depth/image_raw), it looks just fine. I guess probably it's the problem of encoding, can someone guide me what to do to save image just like the image_view showed to me. plz, thank you!
from __future__ import print_function
import sys
import rospy
import cv2
import cv
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
class TakePhoto:
def __init__(self):
self.bridge = CvBridge()
self.image_received = False
# Connect image topic
img_topic = "/camera/depth/image_raw"
self.image_sub = rospy.Subscriber(img_topic, Image, self.callback)
# Allow up to one second to connection
rospy.sleep(1)
def callback(self, data):
# Convert image to OpenCV format
try:
cv_image = self.bridge.imgmsg_to_cv2(data,'16UC1')
except CvBridgeError as e:
print(e)
self.image_received = True
self.image = cv_image
def take_picture(self, img_title):
if self.image_received:
# Save an image
cv2.imwrite(img_title, self.image)
return True
else:
return False
if __name__ == '__main__':
# Initialize
rospy.init_node('take_photo', anonymous=False)
camera = TakePhoto()
# Take a photo
# Use '_image_title' parameter from command line
# Default value is 'photo.jpg'
img_title = rospy.get_param('~image_title', 'photo16U.jpg')
if camera.take_picture(img_title):
rospy.loginfo("Saved image " + img_title)
else:
rospy.loginfo("No images received")
# Sleep to give the last log messages time to be sent
rospy.sleep(1)
when you divide the image by 7.0 and multiply by 255, can you see something in Matlab?
yes, but because those numbers from 1 to 7 are all int. so when I see it on Matlab it lacks detail.
is this the full code? If not could you give me please, I want to work with images in depth but I find somewhat complicated to start since 0. thank you :)
I run into the same problem. The difference is that I cannot even visualize the depth image using image_view. Have you run into this before?