Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. import cv2
  2. from cv_bridge import CvBridge, CvBridgeError
  3. import rospy
  4. from sensor_msgs.msg import Image
  5.  
  6. """
  7. Starter code for an image processing node. Subscribes to the downward camera
  8. and publishes a processed cv image (i.e. colorized, overlayed with a detected line)
  9. This node is not meant to be a full node that you will use in your final line tracking
  10. solution, but it will teach you key concepts that you will need for line following
  11. """
  12.  
  13. class ImageToCV:
  14.     def __init__(self):
  15.         # A subscriber to the topic '/aero_downward_camera/image'. self.image_sub_cb is called when a message is recieved
  16.         self.camera_sub = rospy.Subscriber("/aero_downward_camera/image", Image, self.image_cb)
  17.  
  18.         # A publisher which will publish a parametrization of the detected line to the topic '/image_to_cv/processed'
  19.         self.image_pub = rospy.Publisher("/image_to_cv/processed", Image, queue_size=1)
  20.         self.bridge = CvBridge()
  21.  
  22.     def image_cb(self, msg):
  23.         """
  24.        Callback function which is called when a new message of type Image is recieved by self.camera_sub.
  25.  
  26.            Args:
  27.                - msg = ROS Image message in 8UC1 format
  28.  
  29.            Returns: NA
  30.        """
  31.         try:
  32.             cv_image = self.bridge.imgmsg_to_cv2(msg, "8UC1")
  33.             self.process(cv_image)
  34.         except CvBridgeError as e:
  35.             print(e)
  36.  
  37.     def process(self, img):
  38.         """
  39.        Takes in an img param and finds the line in it, if it exists
  40.  
  41.            Args:
  42.                - img : OpenCV Image to process
  43.  
  44.            Publishes:
  45.                - annotated image to the /image_to_cv/processed topic
  46.        """
  47.         """TODO-START: FILL IN CODE HERE"""
  48.         # Get a colored copy of the image. This will be used solely to provide an annotated version
  49.         # of the image for debuging purposes (for you to see in rqt_image_view). See slides for help here
  50.  
  51.         # Threshold the image to isolate the line and use it for your linear regression algorithm
  52.         # (tip: check size to ensure you are detecting the LED line or a simple blip on the screen)
  53.  
  54.         # "Draw" a line if you have found one on the image
  55.  
  56.         # Publish your newly annotated, color image
  57.         # NOTE: If you do not detect a line, or if you have decided to ignore detected lines of very small
  58.         #       size, be sure to still publish the image. Also be sure to have some way to handle the camera
  59.         #       seeing multiple "lines" at one time
  60.  
  61.  
  62.         """TODO-END"""
  63.  
  64. if __name__ == "__main__":
  65.     rospy.init_node("image_to_cv")
  66.     a = ImageToCV()
  67.  
  68.     rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement