Go to the documentation of this file.00001
00002
00003 import rospy
00004 import sys
00005 import cv2
00006 import cv2.cv as cv
00007 from sensor_msgs.msg import Image, CameraInfo
00008 from cv_bridge import CvBridge, CvBridgeError
00009 import numpy as np
00010
00011 class turtlebot_openCV():
00012 def __init__(self):
00013 self.node_name = "turtlebot_openCV"
00014
00015 rospy.init_node(self.node_name)
00016
00017
00018 rospy.on_shutdown(self.cleanup)
00019
00020
00021 self.cv_window_name = self.node_name
00022 cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
00023 cv.MoveWindow(self.cv_window_name, 25, 75)
00024
00025
00026 cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
00027 cv.MoveWindow("Depth Image", 25, 350)
00028
00029
00030 self.bridge = CvBridge()
00031
00032
00033
00034 self.image_sub = rospy.Subscriber("input_rgb_image", Image, self.image_callback, queue_size=1)
00035 self.depth_sub = rospy.Subscriber("input_depth_image", Image, self.depth_callback, queue_size=1)
00036
00037 rospy.loginfo("Waiting for image topics...")
00038 rospy.wait_for_message("input_rgb_image", Image)
00039 rospy.loginfo("Ready.")
00040
00041 def image_callback(self, data):
00042
00043
00044 frame = self.convert_image(data)
00045
00046
00047 processed_image = self.process_image(frame)
00048
00049
00050
00051
00052
00053
00054 cv2.imshow(self.node_name, processed_image)
00055
00056
00057 self.keystroke = cv2.waitKey(5)
00058 if self.keystroke != -1:
00059 cc = chr(self.keystroke & 255).lower()
00060 if cc == 'q':
00061
00062 rospy.signal_shutdown("User hit q key to quit.")
00063
00064 def depth_callback(self, ros_image):
00065
00066 try:
00067
00068 depth_image = self.bridge.imgmsg_to_cv2(ros_image, "passthrough")
00069 except CvBridgeError, e:
00070 print e
00071
00072
00073 depth_array = np.array(depth_image, dtype=np.float32)
00074
00075
00076 cv2.normalize(depth_array, depth_array, 0, 1, cv2.NORM_MINMAX)
00077
00078
00079 depth_display_image = self.process_depth_image(depth_array)
00080
00081
00082 cv2.imshow("Depth Image", depth_display_image)
00083
00084 def convert_image(self, ros_image):
00085
00086
00087 try:
00088 cv_image = self.bridge.imgmsg_to_cv2(ros_image, "bgr8")
00089 return np.array(cv_image, dtype=np.uint8)
00090 except CvBridgeError, e:
00091 print e
00092
00093 def convert_depth_image(self, ros_image):
00094
00095 try:
00096 depth_image = self.bridge.imgmsg_to_cv2(ros_image, "passthrough")
00097
00098
00099 depth_image = np.array(depth_image, dtype=np.float32)
00100
00101 return depth_image
00102
00103 except CvBridgeError, e:
00104 print e
00105
00106 def process_depth_image(self, frame):
00107
00108 return frame
00109
00110 def process_image(self, frame):
00111 return frame
00112
00113 def cleanup(self):
00114 print "Shutting down vision node."
00115 cv2.destroyAllWindows()
00116
00117 def main(args):
00118 try:
00119 turtlebot_openCV()
00120 rospy.spin()
00121 except KeyboardInterrupt:
00122 print "Shutting down vision node."
00123 cv.DestroyAllWindows()
00124
00125 if __name__ == '__main__':
00126 main(sys.argv)
00127