Home | History | Annotate | Download | only in python2
      1 #!/usr/bin/env python
      2 
      3 '''
      4 Feature homography
      5 ==================
      6 
      7 Example of using features2d framework for interactive video homography matching.
      8 ORB features and FLANN matcher are used. The actual tracking is implemented by
      9 PlaneTracker class in plane_tracker.py
     10 
     11 Inspired by http://www.youtube.com/watch?v=-ZNYoL8rzPY
     12 
     13 video: http://www.youtube.com/watch?v=FirtmYcC0Vc
     14 
     15 Usage
     16 -----
     17 feature_homography.py [<video source>]
     18 
     19 Keys:
     20    SPACE  -  pause video
     21 
     22 Select a textured planar object to track by drawing a box with a mouse.
     23 '''
     24 
     25 import numpy as np
     26 import cv2
     27 
     28 # local modules
     29 import video
     30 import common
     31 from common import getsize, draw_keypoints
     32 from plane_tracker import PlaneTracker
     33 
     34 
     35 class App:
     36     def __init__(self, src):
     37         self.cap = video.create_capture(src)
     38         self.frame = None
     39         self.paused = False
     40         self.tracker = PlaneTracker()
     41 
     42         cv2.namedWindow('plane')
     43         self.rect_sel = common.RectSelector('plane', self.on_rect)
     44 
     45     def on_rect(self, rect):
     46         self.tracker.clear()
     47         self.tracker.add_target(self.frame, rect)
     48 
     49     def run(self):
     50         while True:
     51             playing = not self.paused and not self.rect_sel.dragging
     52             if playing or self.frame is None:
     53                 ret, frame = self.cap.read()
     54                 if not ret:
     55                     break
     56                 self.frame = frame.copy()
     57 
     58             w, h = getsize(self.frame)
     59             vis = np.zeros((h, w*2, 3), np.uint8)
     60             vis[:h,:w] = self.frame
     61             if len(self.tracker.targets) > 0:
     62                 target = self.tracker.targets[0]
     63                 vis[:,w:] = target.image
     64                 draw_keypoints(vis[:,w:], target.keypoints)
     65                 x0, y0, x1, y1 = target.rect
     66                 cv2.rectangle(vis, (x0+w, y0), (x1+w, y1), (0, 255, 0), 2)
     67 
     68             if playing:
     69                 tracked = self.tracker.track(self.frame)
     70                 if len(tracked) > 0:
     71                     tracked = tracked[0]
     72                     cv2.polylines(vis, [np.int32(tracked.quad)], True, (255, 255, 255), 2)
     73                     for (x0, y0), (x1, y1) in zip(np.int32(tracked.p0), np.int32(tracked.p1)):
     74                         cv2.line(vis, (x0+w, y0), (x1, y1), (0, 255, 0))
     75             draw_keypoints(vis, self.tracker.frame_points)
     76 
     77             self.rect_sel.draw(vis)
     78             cv2.imshow('plane', vis)
     79             ch = cv2.waitKey(1)
     80             if ch == ord(' '):
     81                 self.paused = not self.paused
     82             if ch == 27:
     83                 break
     84 
     85 
     86 if __name__ == '__main__':
     87     print __doc__
     88 
     89     import sys
     90     try:
     91         video_src = sys.argv[1]
     92     except:
     93         video_src = 0
     94     App(video_src).run()
     95