2012-07-13 22:27:54 +02:00
|
|
|
'''
|
|
|
|
Feature-based image matching sample.
|
2011-07-04 11:05:11 +02:00
|
|
|
|
2012-07-13 22:27:54 +02:00
|
|
|
USAGE
|
2012-07-16 13:31:43 +02:00
|
|
|
find_obj.py [--feature=<sift|surf|orb>[-flann]] [ <image1> <image2> ]
|
|
|
|
|
|
|
|
--feature - Feature to use. Can be sift, surf of orb. Append '-flann' to feature name
|
|
|
|
to use Flann-based matcher instead bruteforce.
|
|
|
|
|
|
|
|
Press left mouse button on a feature point to see its mathcing point.
|
2011-07-04 11:05:11 +02:00
|
|
|
'''
|
|
|
|
|
2012-07-13 22:27:54 +02:00
|
|
|
import numpy as np
|
|
|
|
import cv2
|
2012-07-16 13:31:43 +02:00
|
|
|
from common import anorm, getsize
|
2011-07-04 11:05:11 +02:00
|
|
|
|
2012-07-13 22:27:54 +02:00
|
|
|
FLANN_INDEX_KDTREE = 1 # bug: flann enums are missing
|
2012-07-16 13:31:43 +02:00
|
|
|
FLANN_INDEX_LSH = 6
|
2011-08-17 04:00:52 +02:00
|
|
|
|
2011-07-04 11:05:11 +02:00
|
|
|
|
2012-07-16 13:31:43 +02:00
|
|
|
def init_feature(name):
|
|
|
|
chunks = name.split('-')
|
|
|
|
if chunks[0] == 'sift':
|
|
|
|
detector = cv2.SIFT()
|
|
|
|
norm = cv2.NORM_L2
|
|
|
|
elif chunks[0] == 'surf':
|
2012-07-16 14:29:49 +02:00
|
|
|
detector = cv2.SURF(800)
|
2012-07-16 13:31:43 +02:00
|
|
|
norm = cv2.NORM_L2
|
|
|
|
elif chunks[0] == 'orb':
|
2012-07-16 14:29:49 +02:00
|
|
|
detector = cv2.ORB(400)
|
2012-07-16 13:31:43 +02:00
|
|
|
norm = cv2.NORM_HAMMING
|
2012-07-16 14:31:56 +02:00
|
|
|
else:
|
|
|
|
return None, None
|
2012-07-16 13:31:43 +02:00
|
|
|
if 'flann' in chunks:
|
|
|
|
if norm == cv2.NORM_L2:
|
|
|
|
flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
|
|
|
|
else:
|
|
|
|
flann_params= dict(algorithm = FLANN_INDEX_LSH,
|
|
|
|
table_number = 6, # 12
|
|
|
|
key_size = 12, # 20
|
|
|
|
multi_probe_level = 1) #2
|
|
|
|
matcher = cv2.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329)
|
|
|
|
else:
|
|
|
|
matcher = cv2.BFMatcher(norm)
|
|
|
|
return detector, matcher
|
2011-08-17 04:00:52 +02:00
|
|
|
|
2012-07-16 13:31:43 +02:00
|
|
|
|
|
|
|
def filter_matches(kp1, kp2, matches, ratio = 0.75):
|
|
|
|
mkp1, mkp2 = [], []
|
|
|
|
for m in matches:
|
|
|
|
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
|
|
|
|
m = m[0]
|
|
|
|
mkp1.append( kp1[m.queryIdx] )
|
|
|
|
mkp2.append( kp2[m.trainIdx] )
|
|
|
|
p1 = np.float32([kp.pt for kp in mkp1])
|
|
|
|
p2 = np.float32([kp.pt for kp in mkp2])
|
|
|
|
kp_pairs = zip(mkp1, mkp2)
|
|
|
|
return p1, p2, kp_pairs
|
|
|
|
|
|
|
|
def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
|
2011-07-04 11:05:11 +02:00
|
|
|
h1, w1 = img1.shape[:2]
|
|
|
|
h2, w2 = img2.shape[:2]
|
|
|
|
vis = np.zeros((max(h1, h2), w1+w2), np.uint8)
|
|
|
|
vis[:h1, :w1] = img1
|
|
|
|
vis[:h2, w1:w1+w2] = img2
|
2011-07-18 15:05:34 +02:00
|
|
|
vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
|
2011-07-04 11:05:11 +02:00
|
|
|
|
|
|
|
if H is not None:
|
|
|
|
corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]])
|
|
|
|
corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2) + (w1, 0) )
|
|
|
|
cv2.polylines(vis, [corners], True, (255, 255, 255))
|
2012-07-16 13:31:43 +02:00
|
|
|
|
2011-07-04 11:05:11 +02:00
|
|
|
if status is None:
|
2012-07-16 13:46:05 +02:00
|
|
|
status = np.ones(len(kp_pairs), np.bool_)
|
2012-07-16 13:31:43 +02:00
|
|
|
p1 = np.int32([kpp[0].pt for kpp in kp_pairs])
|
|
|
|
p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) + (w1, 0)
|
|
|
|
|
2011-07-04 11:05:11 +02:00
|
|
|
green = (0, 255, 0)
|
|
|
|
red = (0, 0, 255)
|
2012-07-16 13:31:43 +02:00
|
|
|
white = (255, 255, 255)
|
|
|
|
kp_color = (51, 103, 236)
|
|
|
|
for (x1, y1), (x2, y2), inlier in zip(p1, p2, status):
|
2011-07-13 08:19:21 +02:00
|
|
|
if inlier:
|
2012-07-16 13:31:43 +02:00
|
|
|
col = green
|
2011-07-13 08:19:21 +02:00
|
|
|
cv2.circle(vis, (x1, y1), 2, col, -1)
|
2012-07-16 13:31:43 +02:00
|
|
|
cv2.circle(vis, (x2, y2), 2, col, -1)
|
2011-07-13 08:19:21 +02:00
|
|
|
else:
|
2012-07-16 13:31:43 +02:00
|
|
|
col = red
|
2011-07-13 08:19:21 +02:00
|
|
|
r = 2
|
|
|
|
thickness = 3
|
|
|
|
cv2.line(vis, (x1-r, y1-r), (x1+r, y1+r), col, thickness)
|
|
|
|
cv2.line(vis, (x1-r, y1+r), (x1+r, y1-r), col, thickness)
|
2012-07-16 13:31:43 +02:00
|
|
|
cv2.line(vis, (x2-r, y2-r), (x2+r, y2+r), col, thickness)
|
|
|
|
cv2.line(vis, (x2-r, y2+r), (x2+r, y2-r), col, thickness)
|
|
|
|
vis0 = vis.copy()
|
|
|
|
for (x1, y1), (x2, y2), inlier in zip(p1, p2, status):
|
|
|
|
if inlier:
|
|
|
|
cv2.line(vis, (x1, y1), (x2, y2), green)
|
|
|
|
|
|
|
|
cv2.imshow(win, vis)
|
|
|
|
def onmouse(event, x, y, flags, param):
|
|
|
|
cur_vis = vis
|
|
|
|
if flags & cv2.EVENT_FLAG_LBUTTON:
|
|
|
|
cur_vis = vis0.copy()
|
|
|
|
r = 8
|
|
|
|
m = (anorm(p1 - (x, y)) < r) | (anorm(p2 - (x, y)) < r)
|
|
|
|
idxs = np.where(m)[0]
|
|
|
|
kp1s, kp2s = [], []
|
|
|
|
for i in idxs:
|
|
|
|
(x1, y1), (x2, y2) = p1[i], p2[i]
|
|
|
|
col = (red, green)[status[i]]
|
|
|
|
cv2.line(cur_vis, (x1, y1), (x2, y2), col)
|
|
|
|
kp1, kp2 = kp_pairs[i]
|
|
|
|
kp1s.append(kp1)
|
|
|
|
kp2s.append(kp2)
|
|
|
|
cur_vis = cv2.drawKeypoints(cur_vis, kp1s, flags=4, color=kp_color)
|
|
|
|
cur_vis[:,w1:] = cv2.drawKeypoints(cur_vis[:,w1:], kp2s, flags=4, color=kp_color)
|
|
|
|
|
|
|
|
cv2.imshow(win, cur_vis)
|
|
|
|
cv2.setMouseCallback(win, onmouse)
|
2012-07-29 15:26:22 +02:00
|
|
|
return vis
|
2011-07-04 11:05:11 +02:00
|
|
|
|
2011-08-17 04:00:52 +02:00
|
|
|
|
2011-07-04 11:05:11 +02:00
|
|
|
if __name__ == '__main__':
|
2012-07-13 22:27:54 +02:00
|
|
|
print __doc__
|
|
|
|
|
2012-07-16 13:31:43 +02:00
|
|
|
import sys, getopt
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], '', ['feature='])
|
|
|
|
opts = dict(opts)
|
|
|
|
feature_name = opts.get('--feature', 'sift')
|
|
|
|
try: fn1, fn2 = args
|
2011-07-04 11:05:11 +02:00
|
|
|
except:
|
|
|
|
fn1 = '../c/box.png'
|
|
|
|
fn2 = '../c/box_in_scene.png'
|
2012-07-16 13:31:43 +02:00
|
|
|
|
2011-07-04 11:05:11 +02:00
|
|
|
img1 = cv2.imread(fn1, 0)
|
|
|
|
img2 = cv2.imread(fn2, 0)
|
2012-07-16 13:31:43 +02:00
|
|
|
detector, matcher = init_feature(feature_name)
|
|
|
|
if detector != None:
|
|
|
|
print 'using', feature_name
|
|
|
|
else:
|
|
|
|
print 'unknown feature:', feature_name
|
|
|
|
sys.exit(1)
|
|
|
|
|
2011-07-04 11:05:11 +02:00
|
|
|
|
2012-07-13 22:27:54 +02:00
|
|
|
kp1, desc1 = detector.detectAndCompute(img1, None)
|
|
|
|
kp2, desc2 = detector.detectAndCompute(img2, None)
|
2011-07-04 11:05:11 +02:00
|
|
|
print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))
|
|
|
|
|
2012-07-16 13:31:43 +02:00
|
|
|
def match_and_draw(win):
|
|
|
|
print 'matching...'
|
|
|
|
raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2
|
|
|
|
p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches)
|
2012-07-13 22:27:54 +02:00
|
|
|
if len(p1) >= 4:
|
2012-07-16 13:31:43 +02:00
|
|
|
H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0)
|
2012-07-13 22:27:54 +02:00
|
|
|
print '%d / %d inliers/matched' % (np.sum(status), len(status))
|
|
|
|
else:
|
|
|
|
H, status = None, None
|
|
|
|
print '%d matches found, not enough for homography estimation' % len(p1)
|
2011-08-17 04:00:52 +02:00
|
|
|
|
2012-07-16 13:31:43 +02:00
|
|
|
vis = explore_match(win, img1, img2, kp_pairs, status, H)
|
2011-07-04 11:05:11 +02:00
|
|
|
|
2012-07-16 13:31:43 +02:00
|
|
|
match_and_draw('find_obj')
|
|
|
|
cv2.waitKey()
|
2012-03-15 04:45:15 +01:00
|
|
|
cv2.destroyAllWindows()
|