Created
February 13, 2013 21:16
-
-
Save kapadia/4948362 to your computer and use it in GitHub Desktop.
Use OpenCV to match features between two images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import cv2 | |
original = "original.tif" | |
cropped = "cropped.jpeg" | |
def matcher(): | |
# Open images as grayscale | |
im1 = cv2.imread(original, cv2.CV_LOAD_IMAGE_GRAYSCALE) | |
im2 = cv2.imread(cropped, cv2.CV_LOAD_IMAGE_GRAYSCALE) | |
# Create feature detector | |
fd = cv2.FeatureDetector_create('SURF') | |
detector = cv2.GridAdaptedFeatureDetector(fd, 500) | |
# Detect features on input images | |
keypoints1 = detector.detect(im1) | |
keypoints2 = detector.detect(im2) | |
# Create a descriptor extractor | |
extractor = cv2.DescriptorExtractor_create('SURF') | |
# Calculate descriptors | |
descriptor1 = extractor.compute(im1, keypoints1) | |
descriptor2 = extractor.compute(im2, keypoints2) | |
# Match descriptor vectors using FLANN match | |
FLANN_INDEX_KDTREE = 1 | |
flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) | |
matcher = cv2.FlannBasedMatcher(flann_params, {}) | |
matches = matcher.match(descriptor1[1], descriptor2[1]) | |
for match in matches: | |
distance = match.distance | |
if distance < 0.2: | |
print distance | |
if __name__ == '__main__': | |
matcher() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment