2012-07-14 04:27:54 +08:00
|
|
|
'''
|
|
|
|
Feature-based image matching sample.
|
2011-07-04 17:05:11 +08:00
|
|
|
|
2012-07-14 04:27:54 +08:00
|
|
|
USAGE
|
|
|
|
find_obj.py [ <image1> <image2> ]
|
2011-07-04 17:05:11 +08:00
|
|
|
'''
|
|
|
|
|
2012-07-14 04:27:54 +08:00
|
|
|
import numpy as np
|
|
|
|
import cv2
|
2011-07-04 17:05:11 +08:00
|
|
|
|
2012-07-14 04:27:54 +08:00
|
|
|
FLANN_INDEX_KDTREE = 1 # bug: flann enums are missing
|
2011-08-17 10:00:52 +08:00
|
|
|
|
2012-07-14 04:27:54 +08:00
|
|
|
flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
|
2011-07-04 17:05:11 +08:00
|
|
|
|
2011-08-17 10:00:52 +08:00
|
|
|
|
2011-07-04 17:05:11 +08:00
|
|
|
def draw_match(img1, img2, p1, p2, status = None, H = None):
|
|
|
|
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 21:05:34 +08:00
|
|
|
vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
|
2011-07-04 17:05:11 +08: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))
|
|
|
|
|
|
|
|
if status is None:
|
|
|
|
status = np.ones(len(p1), np.bool_)
|
|
|
|
green = (0, 255, 0)
|
|
|
|
red = (0, 0, 255)
|
|
|
|
for (x1, y1), (x2, y2), inlier in zip(np.int32(p1), np.int32(p2), status):
|
|
|
|
col = [red, green][inlier]
|
2011-07-13 14:19:21 +08:00
|
|
|
if inlier:
|
2011-07-04 17:05:11 +08:00
|
|
|
cv2.line(vis, (x1, y1), (x2+w1, y2), col)
|
2011-07-13 14:19:21 +08:00
|
|
|
cv2.circle(vis, (x1, y1), 2, col, -1)
|
|
|
|
cv2.circle(vis, (x2+w1, y2), 2, col, -1)
|
|
|
|
else:
|
|
|
|
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)
|
|
|
|
cv2.line(vis, (x2+w1-r, y2-r), (x2+w1+r, y2+r), col, thickness)
|
|
|
|
cv2.line(vis, (x2+w1-r, y2+r), (x2+w1+r, y2-r), col, thickness)
|
2011-07-04 17:05:11 +08:00
|
|
|
return vis
|
|
|
|
|
2011-08-17 10:00:52 +08:00
|
|
|
|
2011-07-04 17:05:11 +08:00
|
|
|
if __name__ == '__main__':
|
2012-07-14 04:27:54 +08:00
|
|
|
print __doc__
|
|
|
|
|
2011-07-04 17:05:11 +08:00
|
|
|
import sys
|
|
|
|
try: fn1, fn2 = sys.argv[1:3]
|
|
|
|
except:
|
|
|
|
fn1 = '../c/box.png'
|
|
|
|
fn2 = '../c/box_in_scene.png'
|
|
|
|
|
|
|
|
img1 = cv2.imread(fn1, 0)
|
|
|
|
img2 = cv2.imread(fn2, 0)
|
|
|
|
|
2012-07-14 04:27:54 +08:00
|
|
|
detector = cv2.SIFT()
|
|
|
|
kp1, desc1 = detector.detectAndCompute(img1, None)
|
|
|
|
kp2, desc2 = detector.detectAndCompute(img2, None)
|
2011-07-04 17:05:11 +08:00
|
|
|
print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))
|
|
|
|
|
2012-07-14 04:27:54 +08:00
|
|
|
bf_matcher = cv2.BFMatcher(cv2.NORM_L2)
|
|
|
|
flann_matcher = cv2.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329)
|
|
|
|
|
|
|
|
def match_and_draw(matcher, r_threshold = 0.75):
|
|
|
|
raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2)
|
|
|
|
p1, p2 = [], []
|
|
|
|
for m in raw_matches:
|
|
|
|
if len(m) == 2 and m[0].distance < m[1].distance * r_threshold:
|
|
|
|
m = m[0]
|
|
|
|
p1.append( kp1[m.queryIdx].pt )
|
|
|
|
p2.append( kp2[m.trainIdx].pt )
|
|
|
|
p1, p2 = np.float32((p1, p2))
|
|
|
|
if len(p1) >= 4:
|
|
|
|
H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 2.0)
|
|
|
|
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 10:00:52 +08:00
|
|
|
|
2012-07-14 04:27:54 +08:00
|
|
|
vis = draw_match(img1, img2, p1, p2, status, H)
|
2011-08-17 10:00:52 +08:00
|
|
|
return vis
|
2011-07-04 17:05:11 +08:00
|
|
|
|
2011-08-17 10:00:52 +08:00
|
|
|
print 'bruteforce match:',
|
2012-07-14 04:27:54 +08:00
|
|
|
vis_brute = match_and_draw( bf_matcher )
|
2011-08-17 10:00:52 +08:00
|
|
|
print 'flann match:',
|
2012-07-14 04:27:54 +08:00
|
|
|
vis_flann = match_and_draw( flann_matcher )
|
|
|
|
cv2.imshow('find_obj', vis_brute)
|
|
|
|
cv2.imshow('find_obj flann', vis_flann)
|
2012-03-15 05:46:34 +08:00
|
|
|
0xFF & cv2.waitKey()
|
2012-03-15 11:45:15 +08:00
|
|
|
cv2.destroyAllWindows()
|