Skip to content

Instantly share code, notes, and snippets.

@atinfinity
Created January 10, 2019 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atinfinity/4a30585f90045615a635acdb031963af to your computer and use it in GitHub Desktop.
Save atinfinity/4a30585f90045615a635acdb031963af to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
#import random
if __name__ == '__main__':
img1 = cv2.imread('Univ4.jpg')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.imread('Univ3.jpg')
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
dst = gray1
sift = cv2.xfeatures2d.SIFT_create()
kp1, desc1 = sift.detectAndCompute(gray1, None)
kp2, desc2 = sift.detectAndCompute(gray2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(desc1, desc2, k=2)
good_matches = []
# Apply ratio test
ratio = 0.5
pt1 = []
pt2 = []
for m, n in matches:
if m.distance < ratio * n.distance:
good_matches.append([m])
pt1.append(kp1[m.queryIdx].pt)
pt2.append(kp2[m.trainIdx].pt)
'''
for index in range(len(pt1)):
b = random.randint(0, 255)
g = random.randint(0, 255)
r = random.randint(0, 255)
cv2.circle(img1, (int(pt1[index][0]), int(pt1[index][1])), 3, (b, g, r), thickness=-1, lineType=cv2.LINE_AA)
cv2.circle(img2, (int(pt2[index][0]), int(pt2[index][1])), 3, (b, g, r), thickness=-1, lineType=cv2.LINE_AA)
'''
draw = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good_matches, None, flags=2)
cv2.imshow('draw', draw)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment