Skip to content

Instantly share code, notes, and snippets.

@bhive01
Created August 28, 2016 01:14
Show Gist options
  • Select an option

  • Save bhive01/80fd19f5b5d3a6dd68f7e31acf634b23 to your computer and use it in GitHub Desktop.

Select an option

Save bhive01/80fd19f5b5d3a6dd68f7e31acf634b23 to your computer and use it in GitHub Desktop.
import argparse
import cv2
import numpy as np
MIN_MATCH_COUNT = 10
ap = argparse.ArgumentParser()
ap.add_argument("-i1", "--trainimage", required = True, help = "Path to the image")
ap.add_argument("-i2", "--queryimage", required = True, help = "Path to the image")
args = vars(ap.parse_args())
# load the image
im1 = cv2.imread(args["queryimage"])
im2 = cv2.imread(args["trainimage"])
# convert it to grayscale
gray1 = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
# initialize the AKAZE descriptor, then detect keypoints and extract
# local invariant descriptors from the image
detector = cv2.AKAZE_create()
(kps1, descs1) = detector.detectAndCompute(gray1, None)
(kps2, descs2) = detector.detectAndCompute(gray2, None)
print("keypoints: {}, descriptors: {}".format(len(kps1), descs1.shape))
print("keypoints: {}, descriptors: {}".format(len(kps2), descs2.shape))
# Match the features
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
matches = bf.knnMatch(descs1,descs1, k=2)
# Apply ratio test
good = []
for m,n in matches:
if m.distance < 0.9*n.distance:
good.append([m])
# cv2.drawMatchesKnn expects list of lists as matches.
im3 = cv2.drawMatchesKnn(im1, kps1, im2, kps2, good[1:50], None, flags=2)
cv2.imshow("AKAZE matching", im3)
cv2.waitKey(0)
cv2.imwrite("AKAZEresults.png", im3)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment