-
Notifications
You must be signed in to change notification settings - Fork 0
Week3: Image Subscriber
The next node will subscribe to the image topic and execute a series of processing steps to identify the pump's orientation relative to the horizontal image axis.
-
As before, create a basic ROS python node (
detect_pump.py) and set its executable permissions:#!/usr/bin/env python import rospy def start_node(): rospy.init_node('image_subcriber') rospy.loginfo('image_subcriber node started') if __name__ == '__main__': try: start_node() except rospy.ROSInterruptException: pass
chmod u+x nodes/detect_pump.py
- Note that we don't have to edit
CMakeListsto create new build rules for each script, since python does not need to be compiled.
- Note that we don't have to edit
-
Add a ROS subscriber to the
imagetopic, to provide the source for images to process.-
Import the
Imagemessage headerfrom sensor_msgs.msg import Image
-
Above the
start_nodefunction, create an empty callback (process_image) that will be called when a new Image message is received:def process_image(msg): try: pass except Exception as err: print err
- The try/except error handling will allow our code to continue running, even if there are errors during the processing pipeline.
-
In the
start_nodefunction, create a ROS Subscriber object:- subscribe to the
imagetopic, monitoring messages of typeImage - register the callback function we defined above
rospy.Subscriber("image", Image, process_image) rospy.spin()
- reference: rospy.Subscriber
- reference: rospy.spin
- subscribe to the
-
Run the new node and verify that it is subscribing to the topic as expected:
rosrun detect_pump detect_pump.py rosnode info /detect_pump rqt_graph
-
-
Convert the incoming
Imagemessage to an OpenCVImageobject and display it As before, we'll use theCvBridgemodule to do the conversion.-
Import the
CvBridgemodules:from cv_bridge import CvBridge
-
In the
process_imagecallback, add a call to the CvBridge imgmsg_to_cv2 method:# convert sensor_msgs/Image to OpenCV Image bridge = CvBridge() orig = bridge.imgmsg_to_cv2(msg, "bgr8")
- This code (and all other image-processing code) should go inside the
tryblock, to ensure that processing errors don't crash the node. - This should replace the placeholder
passcommand placed in thetryblock earlier
- This code (and all other image-processing code) should go inside the
-
Use the OpenCV
imshowmethod to display the images received. We'll create a pattern that can be re-used to show the result of each image-processing step.-
Import the OpenCV
cv2module:import cv2
-
Add a display helper function above the
process_imagecallback:def showImage(img): cv2.imshow('image', img) cv2.waitKey(1)
-
Copy the received image to a new "drawImg" variable:
drawImg = orig
-
Below the
exceptblock (outside its scope; atprocess_imagescope, display thedrawImgvariable:# show results showImage(drawImg)
-
-
Run the node and see the received image displayed.
-