-
Notifications
You must be signed in to change notification settings - Fork 0
/
35_bruteforce_orb.py
33 lines (24 loc) · 909 Bytes
/
35_bruteforce_orb.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
img1 = cv2.imread('cereal_frostedflakes.jpg', 0)
img2 = cv2.imread('cereal_frostedflakes_more.jpg',0)
# initiate ORB detector
orb = cv2.ORB_create()
# find keypoints nad descriptors with ORB
kp1, desc1 = orb.detectAndCompute(img1, None)
kp2, desc2 = orb.detectAndCompute(img2, None)
# create matcher, use norm_hamming for ORB
bf_matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# get the best matches in two images
# matches objects have: distance, train descriptor index,
# query descriptor index, train image index
matches = bf_matcher.match(desc1, desc2)
# sort so best matches (lower distances) are first
matches = sorted(matches, key = lambda x: x.distance)
# draw first 10 matches
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=2)
# show image
cv2.imshow('orb matches', img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)