Hướng dẫn image subscriber ros python - người đăng ký hình ảnh ros python
Show Nội phân chính
Nhà xuất bản Người đăng ký nén PythonMã Python This example subscribes to a ros topic containing sensor_msgs::CompressedImage. It converts the CompressedImage into a numpy.ndarray, then detects and marks features in that image. It finally displays and publishes the new image - again as CompressedImage topic. Mã giải thích CompressedImage, OpenCV, Publisher, Subscriber Bao gồm các dòng INTERMEDIATE Mã Python1 2 """OpenCV feature detectors with ros CompressedImage Topics in python. 3 4 This example subscribes to a ros topic containing sensor_msgs 5 CompressedImage. It converts the CompressedImage into a numpy.ndarray, 6 then detects and marks features in that image. It finally displays 7 and publishes the new image - again as CompressedImage topic. 8 """ 9 __author__ = 'Simon Haller Mã giải thíchBao gồm các dòng Định nghĩa lớpPhương pháp __init__Use the full environment to look for the python interpreter. Bao gồm các dòng1 2 import sys, time 3 4 import numpy as np 5 from scipy.ndimage import filters 6 7 8 import cv2 9 10 11 import roslib 12 import rospy 13 14 15 from sensor_msgs.msg import CompressedImage Định nghĩa lớpNumpy, scipy and cv2 are included to handle the conversions, the display and feature detection. Phương pháp __init__ Phương thức gọi lạiTrue you will get some additional information printed to the commandline (feature detection method, number of points, time for detection) Định nghĩa lớp1 class image_feature: 2 3 def __init__(self): 4 ... 5 6 def callback(self, ros_data): Phương pháp __init__The _init_ method defines the instantiation operation. It uses the "self" variable, which represents the instance of the object itself. Phương thức gọi lại Phương pháp __init__1 def __init__(self): 2 '''Initialize ros publisher, ros subscriber''' 3 4 self.image_pub = rospy.Publisher("/output/image_raw/compressed", 5 CompressedImage) 6 7 8 9 self.subscriber = rospy.Subscriber("/camera/image/compressed", 10 CompressedImage, self.callback, queue_size = 1) 11 if VERBOSE : 12 print "subscribed to /camera/image/compressed" Phương thức gọi lại Chuyển đổi hình ảnh nén thành CV2 Phương thức gọi lạiChuyển đổi hình ảnh nén thành CV2 Chuyển đổi hình ảnh nén thành CV21 2 np_arr = np.fromstring(ros_data.data, np.uint8) 3 image_np = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR) 4 Chọn và tạo một máy dò tính năngThe second line decodes the image into a raw cv2 image (numpy.ndarray). Chọn và tạo một máy dò tính năng1 2 3 4 method = "GridFAST" 5 feat_det = cv2.FeatureDetector_create(method) 6 time1 = time.time() Tạo một hình ảnh nén để xuất bảnBefore the feature detection gets started remember the time. 1 2 featPoints = feat_det.detect(cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)) 3 time2 = time.time() 4 if VERBOSE : 5 print '%s detector found: %s featpoints in: %s sec.' %(method, 6 len(featPoints),time2-time1) Mô tả: Ví dụ này đăng ký vào một chủ đề ROS chứa Sensor_MSGS :: nén. Nó chuyển đổi tính năng nén thành numpy.ndarray, sau đó phát hiện và đánh dấu các tính năng trong hình ảnh đó. Cuối cùng nó hiển thị và xuất bản hình ảnh mới - một lần nữa dưới dạng chủ đề nén.cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY) - converts the image into a grayscale image - the feature detection algorithms do not take color images. The second part starts the detection with the fresh converted grayscale image. Từ khóa: nén, opencv, nhà xuất bản, thuê bao 1 for featpoint in featPoints: 2 x,y = featpoint.pt 3 cv2.circle(image_np,(int(x),int(y)), 3, (0,0,255), -1) 4 5 cv2.imshow('cv_img', image_np) 6 cv2.waitKey(2) Cấp độ hướng dẫn: Trung cấp Tạo một hình ảnh nén để xuất bản1 2 msg = CompressedImage() 3 msg.header.stamp = rospy.Time.now() 4 msg.format = "jpeg" 5 msg.data = np.array(cv2.imencode('.jpg', image_np)[1]).tostring() Mô tả: Ví dụ này đăng ký vào một chủ đề ROS chứa Sensor_MSGS :: nén. Nó chuyển đổi tính năng nén thành numpy.ndarray, sau đó phát hiện và đánh dấu các tính năng trong hình ảnh đó. Cuối cùng nó hiển thị và xuất bản hình ảnh mới - một lần nữa dưới dạng chủ đề nén.For data field encode the cv2 image to a jpg, generate an numpy array and convert it to a string. 1 2 self.image_pub.publish(msg) Từ khóa: nén, opencv, nhà xuất bản, thuê bao Cấp độ hướng dẫn: Trung cấpLucasWalter) |