2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2012-08-05 15:20:42 +08:00
|
|
|
'''
|
|
|
|
Multitarget planar tracking
|
|
|
|
==================
|
|
|
|
|
|
|
|
Example of using features2d framework for interactive video homography matching.
|
|
|
|
ORB features and FLANN matcher are used. This sample provides PlaneTracker class
|
|
|
|
and an example of its usage.
|
|
|
|
|
|
|
|
video: http://www.youtube.com/watch?v=pzVbhxx6aog
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
plane_tracker.py [<video source>]
|
|
|
|
|
|
|
|
Keys:
|
|
|
|
SPACE - pause video
|
|
|
|
c - clear targets
|
|
|
|
|
|
|
|
Select a textured planar object to track by drawing a box with a mouse.
|
|
|
|
'''
|
|
|
|
|
2015-12-13 09:43:58 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
PY3 = sys.version_info[0] == 3
|
|
|
|
|
|
|
|
if PY3:
|
|
|
|
xrange = range
|
|
|
|
|
2012-08-04 03:17:11 +08:00
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2013-03-06 14:41:02 +08:00
|
|
|
|
|
|
|
# built-in modules
|
2012-08-04 03:17:11 +08:00
|
|
|
from collections import namedtuple
|
2013-03-06 14:41:02 +08:00
|
|
|
|
|
|
|
# local modules
|
2012-08-04 03:17:11 +08:00
|
|
|
import video
|
|
|
|
import common
|
2016-03-25 01:05:26 +08:00
|
|
|
from video import presets
|
2012-08-04 03:17:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
FLANN_INDEX_KDTREE = 1
|
|
|
|
FLANN_INDEX_LSH = 6
|
|
|
|
flann_params= dict(algorithm = FLANN_INDEX_LSH,
|
|
|
|
table_number = 6, # 12
|
|
|
|
key_size = 12, # 20
|
|
|
|
multi_probe_level = 1) #2
|
|
|
|
|
|
|
|
MIN_MATCH_COUNT = 10
|
|
|
|
|
2012-08-05 15:20:42 +08:00
|
|
|
'''
|
|
|
|
image - image to track
|
|
|
|
rect - tracked rectangle (x1, y1, x2, y2)
|
|
|
|
keypoints - keypoints detected inside rect
|
|
|
|
descrs - their descriptors
|
|
|
|
data - some user-provided data
|
|
|
|
'''
|
2012-08-04 03:17:11 +08:00
|
|
|
PlanarTarget = namedtuple('PlaneTarget', 'image, rect, keypoints, descrs, data')
|
2012-08-05 15:20:42 +08:00
|
|
|
|
|
|
|
'''
|
|
|
|
target - reference to PlanarTarget
|
|
|
|
p0 - matched points coords in target image
|
|
|
|
p1 - matched points coords in input frame
|
|
|
|
H - homography matrix from p0 to p1
|
2017-09-27 11:42:48 +08:00
|
|
|
quad - target boundary quad in input frame
|
2012-08-05 15:20:42 +08:00
|
|
|
'''
|
2012-08-04 03:17:11 +08:00
|
|
|
TrackedTarget = namedtuple('TrackedTarget', 'target, p0, p1, H, quad')
|
|
|
|
|
|
|
|
class PlaneTracker:
|
|
|
|
def __init__(self):
|
2017-12-11 17:55:03 +08:00
|
|
|
self.detector = cv.ORB_create( nfeatures = 1000 )
|
|
|
|
self.matcher = cv.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329)
|
2012-08-04 03:17:11 +08:00
|
|
|
self.targets = []
|
2015-12-13 09:43:58 +08:00
|
|
|
self.frame_points = []
|
2012-08-04 03:17:11 +08:00
|
|
|
|
|
|
|
def add_target(self, image, rect, data=None):
|
|
|
|
'''Add a new tracking target.'''
|
|
|
|
x0, y0, x1, y1 = rect
|
|
|
|
raw_points, raw_descrs = self.detect_features(image)
|
|
|
|
points, descs = [], []
|
|
|
|
for kp, desc in zip(raw_points, raw_descrs):
|
|
|
|
x, y = kp.pt
|
|
|
|
if x0 <= x <= x1 and y0 <= y <= y1:
|
|
|
|
points.append(kp)
|
|
|
|
descs.append(desc)
|
|
|
|
descs = np.uint8(descs)
|
|
|
|
self.matcher.add([descs])
|
2015-01-20 04:51:19 +08:00
|
|
|
target = PlanarTarget(image = image, rect=rect, keypoints = points, descrs=descs, data=data)
|
2012-08-04 03:17:11 +08:00
|
|
|
self.targets.append(target)
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
'''Remove all targets'''
|
|
|
|
self.targets = []
|
|
|
|
self.matcher.clear()
|
|
|
|
|
|
|
|
def track(self, frame):
|
|
|
|
'''Returns a list of detected TrackedTarget objects'''
|
2015-12-13 09:43:58 +08:00
|
|
|
self.frame_points, frame_descrs = self.detect_features(frame)
|
|
|
|
if len(self.frame_points) < MIN_MATCH_COUNT:
|
2012-08-04 03:17:11 +08:00
|
|
|
return []
|
2015-01-20 04:57:23 +08:00
|
|
|
matches = self.matcher.knnMatch(frame_descrs, k = 2)
|
2012-08-04 03:17:11 +08:00
|
|
|
matches = [m[0] for m in matches if len(m) == 2 and m[0].distance < m[1].distance * 0.75]
|
|
|
|
if len(matches) < MIN_MATCH_COUNT:
|
|
|
|
return []
|
|
|
|
matches_by_id = [[] for _ in xrange(len(self.targets))]
|
|
|
|
for m in matches:
|
|
|
|
matches_by_id[m.imgIdx].append(m)
|
|
|
|
tracked = []
|
|
|
|
for imgIdx, matches in enumerate(matches_by_id):
|
|
|
|
if len(matches) < MIN_MATCH_COUNT:
|
|
|
|
continue
|
|
|
|
target = self.targets[imgIdx]
|
|
|
|
p0 = [target.keypoints[m.trainIdx].pt for m in matches]
|
2015-12-13 09:43:58 +08:00
|
|
|
p1 = [self.frame_points[m.queryIdx].pt for m in matches]
|
2012-08-04 03:17:11 +08:00
|
|
|
p0, p1 = np.float32((p0, p1))
|
2017-12-11 17:55:03 +08:00
|
|
|
H, status = cv.findHomography(p0, p1, cv.RANSAC, 3.0)
|
2012-08-04 03:17:11 +08:00
|
|
|
status = status.ravel() != 0
|
|
|
|
if status.sum() < MIN_MATCH_COUNT:
|
|
|
|
continue
|
|
|
|
p0, p1 = p0[status], p1[status]
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-08-04 03:17:11 +08:00
|
|
|
x0, y0, x1, y1 = target.rect
|
|
|
|
quad = np.float32([[x0, y0], [x1, y0], [x1, y1], [x0, y1]])
|
2017-12-11 17:55:03 +08:00
|
|
|
quad = cv.perspectiveTransform(quad.reshape(1, -1, 2), H).reshape(-1, 2)
|
2012-08-04 03:17:11 +08:00
|
|
|
|
|
|
|
track = TrackedTarget(target=target, p0=p0, p1=p1, H=H, quad=quad)
|
|
|
|
tracked.append(track)
|
|
|
|
tracked.sort(key = lambda t: len(t.p0), reverse=True)
|
|
|
|
return tracked
|
|
|
|
|
|
|
|
def detect_features(self, frame):
|
|
|
|
'''detect_features(self, frame) -> keypoints, descrs'''
|
|
|
|
keypoints, descrs = self.detector.detectAndCompute(frame, None)
|
|
|
|
if descrs is None: # detectAndCompute returns descs=None if not keypoints found
|
|
|
|
descrs = []
|
|
|
|
return keypoints, descrs
|
|
|
|
|
|
|
|
|
|
|
|
class App:
|
|
|
|
def __init__(self, src):
|
2016-03-25 01:05:26 +08:00
|
|
|
self.cap = video.create_capture(src, presets['book'])
|
2012-08-04 03:17:11 +08:00
|
|
|
self.frame = None
|
|
|
|
self.paused = False
|
|
|
|
self.tracker = PlaneTracker()
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.namedWindow('plane')
|
2012-08-04 03:17:11 +08:00
|
|
|
self.rect_sel = common.RectSelector('plane', self.on_rect)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-08-04 03:17:11 +08:00
|
|
|
def on_rect(self, rect):
|
|
|
|
self.tracker.add_target(self.frame, rect)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
playing = not self.paused and not self.rect_sel.dragging
|
|
|
|
if playing or self.frame is None:
|
|
|
|
ret, frame = self.cap.read()
|
|
|
|
if not ret:
|
|
|
|
break
|
|
|
|
self.frame = frame.copy()
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-08-04 03:17:11 +08:00
|
|
|
vis = self.frame.copy()
|
|
|
|
if playing:
|
|
|
|
tracked = self.tracker.track(self.frame)
|
|
|
|
for tr in tracked:
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.polylines(vis, [np.int32(tr.quad)], True, (255, 255, 255), 2)
|
2012-08-04 03:17:11 +08:00
|
|
|
for (x, y) in np.int32(tr.p1):
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.circle(vis, (x, y), 2, (255, 255, 255))
|
2012-08-04 03:17:11 +08:00
|
|
|
|
|
|
|
self.rect_sel.draw(vis)
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imshow('plane', vis)
|
|
|
|
ch = cv.waitKey(1)
|
2012-08-04 03:17:11 +08:00
|
|
|
if ch == ord(' '):
|
|
|
|
self.paused = not self.paused
|
|
|
|
if ch == ord('c'):
|
|
|
|
self.tracker.clear()
|
|
|
|
if ch == 27:
|
|
|
|
break
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-12-13 09:43:58 +08:00
|
|
|
print(__doc__)
|
2012-08-04 03:17:11 +08:00
|
|
|
|
|
|
|
import sys
|
2013-03-06 14:41:02 +08:00
|
|
|
try:
|
|
|
|
video_src = sys.argv[1]
|
|
|
|
except:
|
|
|
|
video_src = 0
|
2012-08-04 03:17:11 +08:00
|
|
|
App(video_src).run()
|