Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/python/test/test_lk_homography.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
Lucas-Kanade homography tracker test
5
===============================
6
Uses goodFeaturesToTrack for track initialization and back-tracking for match verification
7
between frames. Finds homography between reference and current views.
8
'''
9
10
# Python 2/3 compatibility
11
from __future__ import print_function
12
13
import numpy as np
14
import cv2 as cv
15
16
#local modules
17
from tst_scene_render import TestSceneRender
18
from tests_common import NewOpenCVTests, isPointInRect
19
20
lk_params = dict( winSize = (19, 19),
21
maxLevel = 2,
22
criteria = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))
23
24
feature_params = dict( maxCorners = 1000,
25
qualityLevel = 0.01,
26
minDistance = 8,
27
blockSize = 19 )
28
29
def checkedTrace(img0, img1, p0, back_threshold = 1.0):
30
p1, _st, _err = cv.calcOpticalFlowPyrLK(img0, img1, p0, None, **lk_params)
31
p0r, _st, _err = cv.calcOpticalFlowPyrLK(img1, img0, p1, None, **lk_params)
32
d = abs(p0-p0r).reshape(-1, 2).max(-1)
33
status = d < back_threshold
34
return p1, status
35
36
class lk_homography_test(NewOpenCVTests):
37
38
render = None
39
framesCounter = 0
40
frame = frame0 = None
41
p0 = None
42
p1 = None
43
gray0 = gray1 = None
44
numFeaturesInRectOnStart = 0
45
46
def test_lk_homography(self):
47
self.render = TestSceneRender(self.get_sample('samples/data/graf1.png'),
48
self.get_sample('samples/data/box.png'), noise = 0.1, speed = 1.0)
49
50
frame = self.render.getNextFrame()
51
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
52
self.frame0 = frame.copy()
53
self.p0 = cv.goodFeaturesToTrack(frame_gray, **feature_params)
54
55
isForegroundHomographyFound = False
56
57
if self.p0 is not None:
58
self.p1 = self.p0
59
self.gray0 = frame_gray
60
self.gray1 = frame_gray
61
currRect = self.render.getCurrentRect()
62
for (x,y) in self.p0[:,0]:
63
if isPointInRect((x,y), currRect):
64
self.numFeaturesInRectOnStart += 1
65
66
while self.framesCounter < 200:
67
self.framesCounter += 1
68
frame = self.render.getNextFrame()
69
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
70
if self.p0 is not None:
71
p2, trace_status = checkedTrace(self.gray1, frame_gray, self.p1)
72
73
self.p1 = p2[trace_status].copy()
74
self.p0 = self.p0[trace_status].copy()
75
self.gray1 = frame_gray
76
77
if len(self.p0) < 4:
78
self.p0 = None
79
continue
80
_H, status = cv.findHomography(self.p0, self.p1, cv.RANSAC, 5.0)
81
82
goodPointsInRect = 0
83
goodPointsOutsideRect = 0
84
for (_x0, _y0), (x1, y1), good in zip(self.p0[:,0], self.p1[:,0], status[:,0]):
85
if good:
86
if isPointInRect((x1,y1), self.render.getCurrentRect()):
87
goodPointsInRect += 1
88
else: goodPointsOutsideRect += 1
89
90
if goodPointsOutsideRect < goodPointsInRect:
91
isForegroundHomographyFound = True
92
self.assertGreater(float(goodPointsInRect) / (self.numFeaturesInRectOnStart + 1), 0.6)
93
else:
94
self.p0 = cv.goodFeaturesToTrack(frame_gray, **feature_params)
95
96
self.assertEqual(isForegroundHomographyFound, True)
97
98
99
if __name__ == '__main__':
100
NewOpenCVTests.bootstrap()
101
102