Skip to content

Instantly share code, notes, and snippets.

@vinooniv
Created January 9, 2020 13:46
Show Gist options
  • Save vinooniv/b5c6d25e88ef67de81bb87ee933e0baf to your computer and use it in GitHub Desktop.
Save vinooniv/b5c6d25e88ef67de81bb87ee933e0baf to your computer and use it in GitHub Desktop.
ORB FLANN based matcher
import argparse
import cv2
import numpy as np
def get_corrected_img(img1, img2):
MIN_MATCHES = 50
orb = cv2.ORB_create(nfeatures=500)
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
index_params = dict(algorithm=6,
table_number=6,
key_size=12,
multi_probe_level=2)
search_params = {}
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
# As per Lowe's ratio test to filter good matches
good_matches = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good_matches.append(m)
if len(good_matches) > MIN_MATCHES:
src_points = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
dst_points = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
m, mask = cv2.findHomography(src_points, dst_points, cv2.RANSAC, 5.0)
corrected_img = cv2.warpPerspective(img1, m, (img2.shape[1], img2.shape[0]))
return corrected_img
return img2
if __name__ == "__main__":
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--src", default='book_cover.jpg', help="path for the object image")
parser.add_argument("--dest", default='book_cover_rotated.jpg', help="path for image containing the object")
args = parser.parse_args()
im1 = cv2.imread(args.src)
im2 = cv2.imread(args.dest)
img = get_corrected_img(im2, im1)
cv2.imshow('Corrected image', img)
cv2.waitKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment