-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsift.py
More file actions
29 lines (20 loc) · 871 Bytes
/
Copy pathsift.py
File metadata and controls
29 lines (20 loc) · 871 Bytes
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
import cv2
def sift_alignment(image_1: str, image_2: str):
im1 = cv2.imread(image_1,)
im2 = cv2.imread(image_2,)
sift = cv2.xfeatures2d.SIFT_create()
key_points_1, descriptors_1 = sift.detectAndCompute(im1, None)
key_points_2, descriptors_2 = sift.detectAndCompute(im2, None)
bf_matcher = cv2.BFMatcher() #暴力匹配
matches = bf_matcher.knnMatch(descriptors_1, descriptors_2, k=2)
# 应用比率匹配
good_matches = []
for m, n in matches:
if m.distance < 0.7 * n.distance: # this parameter affects the result filtering
good_matches.append([m])
match_img = cv2.drawMatchesKnn(im1, key_points_1, im2, key_points_2,
good_matches, None, flags=2)
return match_img
match_img = sift_alignment('01.jpg', '02.jpg')
cv2.imshow('match', match_img)
cv2.waitKey(0)