1.環境準備
Ubuntu16.04ROS-kineticopencv3.3.1video-stream-opencv一個USB網路攝影機
video-stream-opencv是USB網路攝影機驅動,關於它的介紹,請看github:https://github.com/ros-drivers/video_stream_opencv 安裝video-stream-opencv
cd ~/catkin_ws/src/git clone https://github.com/ros-drivers/video_stream_opencv.gitcd ~/catkin_ws/catkin_make
測試是否安裝成功
在終端輸入:
rosrun video_stream_opencv test_video_resource.py
如果能從網路攝影機中看到影像說明安裝成功。 2.opencv格式的圖片與ROS中Image-message之間的相互轉換
ROS image訊息類型到opencv類型的轉換:
cv_image = bridge.imgmsg_to_cv2(image_message, desired_encoding="passthrough")
Opencv類型到ROS Image類型的轉換:
image_message = cv2_to_imgmsg(cv_image, encoding="passthrough")
3.目標
接下來要完成使用Opencv擷取圖片,通過ROS的publisher(web_cam)發布圖片到image_topic主題下,然後有一個新的listener(image-converter)訂閱這個topic,並在圖片中的制定位置畫出一個實心圓。
4.發行者
在video_stream_opencv這個package下有一個test_video_resource.py,這個檔案的 原始作用只是簡單的將OpenCv擷取到的圖片顯示出來,這裡我們要做的是通過這個node,將擷取到的圖片發布到image_topic下去,以下是我修改過的 test_video_resource.py:
#! /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 Pfeiffertest_video_resource.py containsa testing code to see if opencv can open a video streamuseful to debug if video_stream does not work"""import sysimport signalimport cv2import rospyfrom cv_bridge import CvBridge, CvBridgeErrorfrom sensor_msgs.msg import Imagedef quit(signum, frame): print '' print 'stop fusion' sys.exit()if __name__ == '__main__': signal.signal(signal.SIGINT, quit) signal.signal(signal.SIGTERM, quit) # 參數的長度 if len(sys.argv) < 2: print "You must give an argument to open a video stream." print " It can be 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, starting to show feed." rval, frame = cap.read() while rval: # 將opencv格式的圖片轉換為ROS可接受的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()
以上代碼不做過多解釋,都比較簡單。特別指出以下代碼是為了堅挺鍵盤當使用者輸入Ctrl+c的時候節點終止
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.訂閱者
訂閱者的作用就是上邊說的從image-topic中訂閱訊息,然後將其轉換為opencv格式的圖片,然後在圖片中畫出一個實心圓,在與發行者相同的檔案夾下建立一個web_cam_listener.py:
#!/usr/bin/env pythonimport roslibroslib.load_manifest('video_stream_opencv')import rospyimport cv2from sensor_msgs.msg import Imagefrom cv_bridge import CvBridge, CvBridgeErrorclass 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.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) (rows, cols, channels) = cv_image.shape if cols > 60 and 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_converter() rospy.init_node('image_converter') try: rospy.spin() except KeyboardInterrupt: print("Shutting down") cv2.destroyAllWindows()
6.運行節點 啟動第一個終端輸入roscore; 啟動(publisher)第二個終端輸入rosrun video_stream_opencv test_video_resource.py 0;
得到如下輸出:
xzchuang@Vostro:~/catkin_ws$ rosrun video_stream_opencv test_video_resource.py 0Trying to open resource: /dev/video0Correctly opened resource, starting to show feed.
啟動(Subscriber)啟動第三個終端輸入:
rosrun video_stream_opencv web_cam_listener.py
這時候會在網路攝影機輸入中畫一個實心圓(橙色),然後顯示出來,如圖:
啟動一個新的終端調用rqt-graph外掛程式:rosrun rqt_graph rqt_graph