1. Environmental Preparedness
Ubuntu16.04
ros-kinetic
opencv3.3.1
VIDEO-STREAM-OPENCV
a USB camera
VIDEO-STREAM-OPENCV is a USB camera driver, for the introduction of it, please see GITHUB:HTTPS://GITHUB.COM/ROS-DRIVERS/VIDEO_STREAM_OPENCV Install VIDEO-STREAM-OPENCV
CD ~/catkin_ws/src/
git clone https://github.com/ros-drivers/video_stream_opencv.git
CD ~/catkin_ws/
Catkin_make
test whether the installation was successful
In the terminal input:
Rosrun VIDEO_STREAM_OPENCV test_video_resource.py
If you can see the image from the camera, the installation is successful. conversion between pictures in 2.OPENCV format and Image-message in Ros
ROS image message type to OPENCV type conversions:
Cv_image = Bridge.imgmsg_to_cv2 (Image_message, desired_encoding= "Passthrough")
Conversion of OPENCV type to Ros image type:
Image_message = Cv2_to_imgmsg (Cv_image, encoding= "Passthrough")
3. Target
The next step is to finish using OpenCV to get the picture, publish the picture through Ros Publisher (Web_cam) to the Image_topic theme, and then have a new listener (Image-converter) Subscribe to this topic, and draw a solid circle in the location of the picture.
4. Publishers '
Under VIDEO_STREAM_OPENCV This package there is a test_video_resource.py, the original function of this file is simply to show the images that OpenCV get, and what we're going to do here is through this node, To publish the captured picture to Image_topic, here are the test_video_resource.py I have modified:
#!
/usr/bin/env Python2 #-*-coding:utf-8-*-"" "Copyright (c) 2015 PAL Robotics SL. Released under the BSD License. Created on 7/14/15 @author: Sammy Pfeiffer test_video_resource.py contains a testing code to = If OpenCV can open a VI Deo Stream useful to debug if Video_stream does not work "" "Import sys import signal import CV2 import Rospy from Cv_bri Dge import Cvbridge, cvbridgeerror from sensor_msgs.msg import Image def quit (Signum, frame): print ' print ' st OP Fusion ' Sys.exit () if __name__ = = ' __main__ ': signal.signal (signal. SIGINT, quit) signal.signal (signal.
Sigterm, quit) # The length of the parameter If Len (SYS.ARGV) < 2:print "You must give a argument to open a video stream." Print "It can be is a number as video device, e.g.: 0 would be/dev/video0" print "It can be a URL of a Stream, e.g.: Rtsp://wowzaec2demo.streamlock.net/vod/mp4:bigbuckbunny_115k.mov "Print" It can be a video file, e.g.: MyviDeo.mkv "Exit (0) resource = sys.argv[1] # If We are given just a number, interpret it as a video device
If Len (Resource) < 3:resource_name = "/dev/video" + Resource resource = Int (Resource) Else: resource_name = resource print "Trying to open resource:" + resource_name cap = Cv2. Videocapture (Resource) if not cap.isopened (): print "Error opening resource:" + str (Resource) Print Maybe OpenCV videocapture can ' t open it ' exit (0) bridge = Cvbridge () publisher = Rospy. Publisher (' image_topic ', image, queue_size=10) rospy.init_node (' Web_cam ') print "correctly opened, resource
Ng to show feed. " Rval, frame = Cap.read () while Rval: # Converts a picture in OPENCV format to an Ros-acceptable msg image_message = bridge.cv2_to_imgmsg
(Frame, encoding= "bgr8") try:publisher.publish (image_message) except Cvbridgeerror as E: Print (e) rval, frame = cap.reAD ()
The above code does not do too much explanation, is relatively simple. Specifically, the following code is for a strong keyboard. When the user enters Ctrl + C, the node terminates.
def quit (Signum, frame):
print '
print ' Stop fusion '
sys.exit ()
if __name__ = = ' __main__ ':
Signal.signal (signal. SIGINT, quit)
signal.signal (signal. Sigterm, quit)
...
While Rval:
...
5. Subscribers '
The subscriber's role is to subscribe to a message from Image-topic, then convert it to a OPENCV format, and then draw a solid circle in the picture, creating a web_cam_listener.py in the same folder as the Publisher:
#!/usr/bin/env python import roslib roslib.load_manifest (' Video_stream_opencv ') import
Rospy Import cv2 from sensor_msgs.msg import Image from Cv_bridge import Cvbridge, Cvbridgeerror class Image_converter: def __init__ (self): Self.bridge = Cvbridge () self.image_sub = Rospy. Subscriber ("Image_topic", Image, Self.callback) def callback (self, data): Try:cv_image = self.br IDGE.IMGMSG_TO_CV2 (data, "bgr8") except Cvbridgeerror as E:print (e) (rows, cols, channels) = Cv_image.shape if cols > Rows > 60:cv2.circle (cv_image, (150, 150), 20, (30, 144, 255), -1) cv2.imshow ("Image window", Cv_image) Cv2.waitkey (3) if __name__ = = ' __main__ ': IC = image_conve Rter () rospy.init_node (' Image_converter ') Try:rospy.spin () except Keyboardinterrupt:print ("S Hutting down ") cv2.destroyallwindows ()
6. Run the nodeStart the first terminal input roscore; Launch (publisher) a second terminal input Rosrun video_stream_opencv test_video_resource.py 0;
Get the following output:
xzchuang@vostro:~/catkin_ws$ rosrun VIDEO_STREAM_OPENCV test_video_resource.py 0Trying to open resource:/dev/video0
correctly opened resource, starting to show feed.
Start (subscriber) Start a third terminal input:
Rosrun VIDEO_STREAM_OPENCV web_cam_listener.py
This will draw a solid circle (orange) in the camera input and then display it, as shown in figure:
Start a new terminal call Rqt-graph plugin: Rosrun rqt_graph rqt_graph