Hướng dẫn image subscriber ros python - người đăng ký hình ảnh ros python

Hướng dẫn image subscriber ros python - người đăng ký hình ảnh ros python

Vui lòng hỏi về các vấn đề và câu hỏi liên quan đến hướng dẫn này về câu trả lời.ros.org. Đừng quên bao gồm trong câu hỏi của bạn liên kết đến trang này, các phiên bản của OS & ROS của bạn và cũng thêm các thẻ thích hợp.

Nội phân chính

  • Nhà xuất bản Người đăng ký nén Python
  • Mã Python
  • Mã giải thích
  • Bao gồm các dòng
  • Định nghĩa lớp
  • Phương pháp __init__
  • Phương thức gọi lại
  • Chuyển đổi hình ảnh nén thành CV2
  • Chọn và tạo một máy dò tính năng
  • Tạo một hình ảnh nén để xuất bản

Nhà xuất bản Người đăng ký nén Python

Mã 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ã Python

   1 
   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 '
  10 __version__=  '0.1'
  11 __license__ = 'BSD'
  12 
  13 import sys, time
  14 
  15 
  16 import numpy as np
  17 from scipy.ndimage import filters
  18 
  19 
  20 import cv2
  21 
  22 
  23 import roslib
  24 import rospy
  25 
  26 
  27 from sensor_msgs.msg import CompressedImage
  28 
  29 
  30 
  31 VERBOSE=False
  32 
  33 class image_feature:
  34 
  35     def __init__(self):
  36         '''Initialize ros publisher, ros subscriber'''
  37         
  38         self.image_pub = rospy.Publisher("/output/image_raw/compressed",
  39             CompressedImage)
  40         
  41 
  42         
  43         self.subscriber = rospy.Subscriber("/camera/image/compressed",
  44             CompressedImage, self.callback,  queue_size = 1)
  45         if VERBOSE :
  46             print "subscribed to /camera/image/compressed"
  47 
  48 
  49     def callback(self, ros_data):
  50         '''Callback function of subscribed topic. 
  51         Here images get converted and features detected'''
  52         if VERBOSE :
  53             print 'received image of type: "%s"' % ros_data.format
  54 
  55         
  56         np_arr = np.fromstring(ros_data.data, np.uint8)
  57         image_np = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
  58         
  59         
  60         
  61         
  62         
  63         method = "GridFAST"
  64         feat_det = cv2.FeatureDetector_create(method)
  65         time1 = time.time()
  66 
  67         
  68         featPoints = feat_det.detect(
  69             cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY))
  70         time2 = time.time()
  71         if VERBOSE :
  72             print '%s detector found: %s points in: %s sec.'%(method,
  73                 len(featPoints),time2-time1)
  74 
  75         for featpoint in featPoints:
  76             x,y = featpoint.pt
  77             cv2.circle(image_np,(int(x),int(y)), 3, (0,0,255), -1)
  78         
  79         cv2.imshow('cv_img', image_np)
  80         cv2.waitKey(2)
  81 
  82         
  83         msg = CompressedImage()
  84         msg.header.stamp = rospy.Time.now()
  85         msg.format = "jpeg"
  86         msg.data = np.array(cv2.imencode('.jpg', image_np)[1]).tostring()
  87         
  88         self.image_pub.publish(msg)
  89         
  90         
  91 
  92 def main(args):
  93     '''Initializes and cleanup ros node'''
  94     ic = image_feature()
  95     rospy.init_node('image_feature', anonymous=True)
  96     try:
  97         rospy.spin()
  98     except KeyboardInterrupt:
  99         print "Shutting down ROS Image feature detector module"
 100     cv2.destroyAllWindows()
 101 
 102 if __name__ == '__main__':
 103     main(sys.argv)

Mã giải thích

Bao gồm các dòng

Định nghĩa lớp

Phương pháp __init__Use the full environment to look for the python interpreter.

Bao gồm các dòng

   1 
   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ớp

   1 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ại

Chuyển đổi hình ảnh nén thành CV2

Chuyển đổi hình ảnh nén thành CV2

   1 
   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ăng

   1 
   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ản

   1 
   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)