Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/find_obj.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
Feature-based image matching sample.
5
6
Note, that you will need the https://github.com/opencv/opencv_contrib repo for SIFT and SURF
7
8
USAGE
9
find_obj.py [--feature=<sift|surf|orb|akaze|brisk>[-flann]] [ <image1> <image2> ]
10
11
--feature - Feature to use. Can be sift, surf, orb or brisk. Append '-flann'
12
to feature name to use Flann-based matcher instead bruteforce.
13
14
Press left mouse button on a feature point to see its matching point.
15
'''
16
17
# Python 2/3 compatibility
18
from __future__ import print_function
19
20
import numpy as np
21
import cv2 as cv
22
from common import anorm, getsize
23
24
FLANN_INDEX_KDTREE = 1 # bug: flann enums are missing
25
FLANN_INDEX_LSH = 6
26
27
28
def init_feature(name):
29
chunks = name.split('-')
30
if chunks[0] == 'sift':
31
detector = cv.xfeatures2d.SIFT_create()
32
norm = cv.NORM_L2
33
elif chunks[0] == 'surf':
34
detector = cv.xfeatures2d.SURF_create(800)
35
norm = cv.NORM_L2
36
elif chunks[0] == 'orb':
37
detector = cv.ORB_create(400)
38
norm = cv.NORM_HAMMING
39
elif chunks[0] == 'akaze':
40
detector = cv.AKAZE_create()
41
norm = cv.NORM_HAMMING
42
elif chunks[0] == 'brisk':
43
detector = cv.BRISK_create()
44
norm = cv.NORM_HAMMING
45
else:
46
return None, None
47
if 'flann' in chunks:
48
if norm == cv.NORM_L2:
49
flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
50
else:
51
flann_params= dict(algorithm = FLANN_INDEX_LSH,
52
table_number = 6, # 12
53
key_size = 12, # 20
54
multi_probe_level = 1) #2
55
matcher = cv.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329)
56
else:
57
matcher = cv.BFMatcher(norm)
58
return detector, matcher
59
60
61
def filter_matches(kp1, kp2, matches, ratio = 0.75):
62
mkp1, mkp2 = [], []
63
for m in matches:
64
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
65
m = m[0]
66
mkp1.append( kp1[m.queryIdx] )
67
mkp2.append( kp2[m.trainIdx] )
68
p1 = np.float32([kp.pt for kp in mkp1])
69
p2 = np.float32([kp.pt for kp in mkp2])
70
kp_pairs = zip(mkp1, mkp2)
71
return p1, p2, list(kp_pairs)
72
73
def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
74
h1, w1 = img1.shape[:2]
75
h2, w2 = img2.shape[:2]
76
vis = np.zeros((max(h1, h2), w1+w2), np.uint8)
77
vis[:h1, :w1] = img1
78
vis[:h2, w1:w1+w2] = img2
79
vis = cv.cvtColor(vis, cv.COLOR_GRAY2BGR)
80
81
if H is not None:
82
corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]])
83
corners = np.int32( cv.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2) + (w1, 0) )
84
cv.polylines(vis, [corners], True, (255, 255, 255))
85
86
if status is None:
87
status = np.ones(len(kp_pairs), np.bool_)
88
p1, p2 = [], [] # python 2 / python 3 change of zip unpacking
89
for kpp in kp_pairs:
90
p1.append(np.int32(kpp[0].pt))
91
p2.append(np.int32(np.array(kpp[1].pt) + [w1, 0]))
92
93
green = (0, 255, 0)
94
red = (0, 0, 255)
95
kp_color = (51, 103, 236)
96
for (x1, y1), (x2, y2), inlier in zip(p1, p2, status):
97
if inlier:
98
col = green
99
cv.circle(vis, (x1, y1), 2, col, -1)
100
cv.circle(vis, (x2, y2), 2, col, -1)
101
else:
102
col = red
103
r = 2
104
thickness = 3
105
cv.line(vis, (x1-r, y1-r), (x1+r, y1+r), col, thickness)
106
cv.line(vis, (x1-r, y1+r), (x1+r, y1-r), col, thickness)
107
cv.line(vis, (x2-r, y2-r), (x2+r, y2+r), col, thickness)
108
cv.line(vis, (x2-r, y2+r), (x2+r, y2-r), col, thickness)
109
vis0 = vis.copy()
110
for (x1, y1), (x2, y2), inlier in zip(p1, p2, status):
111
if inlier:
112
cv.line(vis, (x1, y1), (x2, y2), green)
113
114
cv.imshow(win, vis)
115
116
def onmouse(event, x, y, flags, param):
117
cur_vis = vis
118
if flags & cv.EVENT_FLAG_LBUTTON:
119
cur_vis = vis0.copy()
120
r = 8
121
m = (anorm(np.array(p1) - (x, y)) < r) | (anorm(np.array(p2) - (x, y)) < r)
122
idxs = np.where(m)[0]
123
124
kp1s, kp2s = [], []
125
for i in idxs:
126
(x1, y1), (x2, y2) = p1[i], p2[i]
127
col = (red, green)[status[i][0]]
128
cv.line(cur_vis, (x1, y1), (x2, y2), col)
129
kp1, kp2 = kp_pairs[i]
130
kp1s.append(kp1)
131
kp2s.append(kp2)
132
cur_vis = cv.drawKeypoints(cur_vis, kp1s, None, flags=4, color=kp_color)
133
cur_vis[:,w1:] = cv.drawKeypoints(cur_vis[:,w1:], kp2s, None, flags=4, color=kp_color)
134
135
cv.imshow(win, cur_vis)
136
cv.setMouseCallback(win, onmouse)
137
return vis
138
139
140
if __name__ == '__main__':
141
print(__doc__)
142
143
import sys, getopt
144
opts, args = getopt.getopt(sys.argv[1:], '', ['feature='])
145
opts = dict(opts)
146
feature_name = opts.get('--feature', 'brisk')
147
try:
148
fn1, fn2 = args
149
except:
150
fn1 = '../data/box.png'
151
fn2 = '../data/box_in_scene.png'
152
153
img1 = cv.imread(fn1, 0)
154
img2 = cv.imread(fn2, 0)
155
detector, matcher = init_feature(feature_name)
156
157
if img1 is None:
158
print('Failed to load fn1:', fn1)
159
sys.exit(1)
160
161
if img2 is None:
162
print('Failed to load fn2:', fn2)
163
sys.exit(1)
164
165
if detector is None:
166
print('unknown feature:', feature_name)
167
sys.exit(1)
168
169
print('using', feature_name)
170
171
kp1, desc1 = detector.detectAndCompute(img1, None)
172
kp2, desc2 = detector.detectAndCompute(img2, None)
173
print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2)))
174
175
def match_and_draw(win):
176
print('matching...')
177
raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2
178
p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches)
179
if len(p1) >= 4:
180
H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0)
181
print('%d / %d inliers/matched' % (np.sum(status), len(status)))
182
else:
183
H, status = None, None
184
print('%d matches found, not enough for homography estimation' % len(p1))
185
186
_vis = explore_match(win, img1, img2, kp_pairs, status, H)
187
188
match_and_draw('find_obj')
189
cv.waitKey()
190
cv.destroyAllWindows()
191
192