Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rinogo
Created April 14, 2021 19:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rinogo/0e7124d5da3c6abd9b9cd6e133ff00fb to your computer and use it in GitHub Desktop.
Save rinogo/0e7124d5da3c6abd9b9cd6e133ff00fb to your computer and use it in GitHub Desktop.
BRISK keypoint detection and brute force matching using OpenCV (Source: https://stackoverflow.com/a/65863713/114558)
# Imports
import cv2 as cv
import matplotlib.pyplot as plt
import sys
# Open and convert the input and training-set image from BGR to GRAYSCALE
image1 = cv.imread(filename = sys.argv[1],
flags = cv.IMREAD_GRAYSCALE)
image2 = cv.imread(filename = sys.argv[2],
flags = cv.IMREAD_GRAYSCALE)
# Initiate BRISK descriptor
BRISK = cv.BRISK_create()
# Find the keypoints and compute the descriptors for input and training-set image
keypoints1, descriptors1 = BRISK.detectAndCompute(image1, None)
keypoints2, descriptors2 = BRISK.detectAndCompute(image2, None)
# create BFMatcher object
BFMatcher = cv.BFMatcher(normType = cv.NORM_HAMMING,
crossCheck = True)
# Matching descriptor vectors using Brute Force Matcher
matches = BFMatcher.match(queryDescriptors = descriptors1,
trainDescriptors = descriptors2)
# Sort them in the order of their distance
matches = sorted(matches, key = lambda x: x.distance)
# Draw first 15 matches
output = cv.drawMatches(img1 = image1,
keypoints1 = keypoints1,
img2 = image2,
keypoints2 = keypoints2,
matches1to2 = matches[:15],
outImg = None,
flags = cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
plt.imshow(output)
plt.show()
@lornatchana
Copy link

really interesting thanks. Had you continued the process untill the stitching of two images?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment