-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_matching.py
33 lines (24 loc) · 932 Bytes
/
feature_matching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cv2
import numpy as np
import matplotlib.pyplot as plt
if __name__ == "__main__":
# Load image and template as grayscaled images
# When we use both matplotlib and cv2 it is important to remember
# that matplotlib stores images in RGB and cv2 stores images in BGR
image = ""
template = ""
img = cv2.imread(template, 0)
templ= cv2.imread(image, 0)
# Create orb for matching
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img,None)
kp2, des2 = orb.detectAndCompute(templ, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Get all the matches
matches = bf.match(des1, des2)
# Sort matches - important to sort them - results will be better
matches = sorted(matches, key = lambda x :x.distance)
img3 = cv2.drawMatches(img, kp1, templ, kp2, matches[:12], None, flags=2)
# Show matches with plt
plt.imshow(img3)
plt.show()