1#!/usr/bin/env python
2
3'''
4Feature homography
5==================
6
7Example of using features2d framework for interactive video homography matching.
8ORB features and FLANN matcher are used. The actual tracking is implemented by
9PlaneTracker class in plane_tracker.py
10
11Inspired by http://www.youtube.com/watch?v=-ZNYoL8rzPY
12
13video: http://www.youtube.com/watch?v=FirtmYcC0Vc
14
15Usage
16-----
17feature_homography.py [<video source>]
18
19Keys:
20   SPACE  -  pause video
21
22Select a textured planar object to track by drawing a box with a mouse.
23'''
24
25import numpy as np
26import cv2
27
28# local modules
29import video
30import common
31from common import getsize, draw_keypoints
32from plane_tracker import PlaneTracker
33
34
35class 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
86if __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