Hướng dẫn ros publish image python - ros xuất bản hình ảnh python

Nội dung chính ShowShow

  • Nhà xuất bản Người đăng ký nén Python
  • Mã giải thích
  • Định nghĩa lớp
  • Bao gồm các dòng
  • Định nghĩa lớp
  • Bao gồm các dòng
  • 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

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.

  • Nhà xuất bản Người đăng ký nén Python
  • Nội phân chính
  • Mã giải thích
  • Bao gồm các dòng
  • Định nghĩa lớp
  • Bao gồm các dòng
  • 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

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

Tạo một hình ảnh nén để xuất bản 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.

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. CompressedImage, OpenCV, Publisher, Subscriber

Nội phân chính INTERMEDIATE

Nội phân chính

   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 <simon.haller at uibk.ac.at>'
  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

Bao gồm các dòngUse 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

Phương pháp __init__Numpy, scipy and cv2 are included to handle the conversions, the display and feature detection.

Bao gồm các dòng

Phương pháp __init__True 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 thức gọi lạiThe _init_ method defines the instantiation operation. It uses the "self" variable, which represents the instance of the object itself.

Phương pháp __init__

Bao gồm các dòng

   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 pháp __init__

Phương thức gọi lại

Phương pháp __init__

Phương thức gọi lại

Phương thức gọi lại

   1 
   2 np_arr = np.fromstring(ros_data.data, np.uint8)
   3 image_np = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
   4 

Chuyển đổi hình ảnh nén thành CV2The second line decodes the image into a raw cv2 image (numpy.ndarray).

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

   1 
   2 
   3 
   4 method = "GridFAST"
   5 feat_det = cv2.FeatureDetector_create(method)
   6 time1 = time.time()

Chọn và tạo một máy dò tính năngBefore 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)

Tạo một hình ảnh nén để xuất bảncv2.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.

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.

   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)

Nội phân chính

Chọn và tạo một máy dò tính năng

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

Tạo một hình ảnh nén để xuất bảnFor 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)

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ínhLucasWalter)