Go to the documentation of this file.00001 import rospy
00002 from sensor_msgs.msg import Image as msg_Image
00003 from cv_bridge import CvBridge, CvBridgeError
00004 import sys
00005 import os
00006
00007 class ImageListener:
00008 def __init__(self, topic):
00009 self.topic = topic
00010 self.bridge = CvBridge()
00011 self.sub = rospy.Subscriber(topic, msg_Image, self.imageDepthCallback)
00012
00013 def imageDepthCallback(self, data):
00014 try:
00015 cv_image = self.bridge.imgmsg_to_cv2(data, data.encoding)
00016 pix = (data.width/2, data.height/2)
00017 sys.stdout.write('%s: Depth at center(%d, %d): %f(mm)\r' % (self.topic, pix[0], pix[1], cv_image[pix[1], pix[0]]))
00018 sys.stdout.flush()
00019 except CvBridgeError as e:
00020 print(e)
00021 return
00022
00023 def main():
00024 topic = '/camera/depth/image_rect_raw'
00025 listener = ImageListener(topic)
00026 rospy.spin()
00027
00028 if __name__ == '__main__':
00029 node_name = os.path.basename(sys.argv[0]).split('.')[0]
00030 rospy.init_node(node_name)
00031 main()