turtlebot_openCV.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
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         # What we do during shutdown
00018         rospy.on_shutdown(self.cleanup)
00019         
00020         # Create the OpenCV display window for the RGB image
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         # And one for the depth image
00026         cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
00027         cv.MoveWindow("Depth Image", 25, 350)
00028         
00029         # Create the cv_bridge object
00030         self.bridge = CvBridge()
00031         
00032         # Subscribe to the camera image and depth topics and set
00033         # the appropriate callbacks
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         # Use cv_bridge() to convert the ROS image to OpenCV format
00043         # Convert the ROS image to OpenCV format using a cv_bridge helper function
00044         frame = self.convert_image(data)
00045                 
00046         # Process the image to detect and track objects or features
00047         processed_image = self.process_image(frame)
00048         
00049         # If the result is a greyscale image, convert to 3-channel for display purposes """
00050         #if processed_image.channels == 1:
00051             #cv.CvtColor(processed_image, self.processed_image, cv.CV_GRAY2BGR)
00052                
00053         # Display the image.
00054         cv2.imshow(self.node_name, processed_image)
00055         
00056         # Process any keyboard commands
00057         self.keystroke = cv2.waitKey(5)
00058         if self.keystroke != -1:
00059             cc = chr(self.keystroke & 255).lower()
00060             if cc == 'q':
00061                 # The user has press the q key, so exit
00062                 rospy.signal_shutdown("User hit q key to quit.")
00063                 
00064     def depth_callback(self, ros_image):
00065         # Use cv_bridge() to convert the ROS image to OpenCV format
00066         try:
00067             # Convert the depth image using the default passthrough encoding
00068             depth_image = self.bridge.imgmsg_to_cv2(ros_image, "passthrough")
00069         except CvBridgeError, e:
00070             print e
00071 
00072         # Convert the depth image to a Numpy array since most cv2 functions require Numpy arrays.
00073         depth_array = np.array(depth_image, dtype=np.float32)
00074                 
00075         # Normalize the depth image to fall between 0 (black) and 1 (white)
00076         cv2.normalize(depth_array, depth_array, 0, 1, cv2.NORM_MINMAX)
00077         
00078         # Process the depth image
00079         depth_display_image = self.process_depth_image(depth_array)
00080     
00081         # Display the result
00082         cv2.imshow("Depth Image", depth_display_image)
00083 
00084     def convert_image(self, ros_image):
00085         
00086         # Use cv_bridge() to convert the ROS image to OpenCV format
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         # Use cv_bridge() to convert the ROS image to OpenCV format
00095         try:
00096             depth_image = self.bridge.imgmsg_to_cv2(ros_image, "passthrough")
00097             
00098             # Convert to a numpy array since this is what OpenCV uses
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         # Just return the raw image for this demo
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     


gapter
Author(s):
autogenerated on Thu Jun 6 2019 22:05:13