In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
cv2.ocl.setUseOpenCL(False)
In [2]:
img1 = cv2.imread('opencv-feature-matching-template.jpg', 0)
img2 = cv2.imread('opencv-feature-matching-image.jpg', 0)
In [3]:
plt.imshow(img1, cmap='gray')
plt.show()
In [4]:
plt.imshow(img2, cmap='gray')
plt.show()
In [5]:
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)
img3 = cv2.drawMatches(img1,kp1,img2,kp2, matches[:12], None, flags=2)
plt.figure(figsize=(15,15))
plt.imshow(img3)
plt.show()
In [6]:
img10 = cv2.drawKeypoints(img1, kp1, img1, color=(0,255,0), flags=0)
plt.figure(figsize=(15,15))
plt.imshow(img10)
plt.show()
In [7]:
img20 = cv2.drawKeypoints(img2, kp2, img2, color=(0,255,0), flags=0)
plt.figure(figsize=(15,15))
plt.imshow(img20)
plt.show()