forked from alyssaq/3Dreconstruction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.py
38 lines (29 loc) · 1.13 KB
/
features.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
33
34
35
36
37
38
import cv2
import numpy as np
def find_correspondence_points(img1, img2):
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(
cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY), None)
kp2, des2 = sift.detectAndCompute(
cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY), None)
# Find point matches
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
# Apply Lowe's SIFT matching ratio test
good = []
for m, n in matches:
if m.distance < 0.8 * n.distance:
good.append(m)
src_pts = np.asarray([kp1[m.queryIdx].pt for m in good])
dst_pts = np.asarray([kp2[m.trainIdx].pt for m in good])
# Constrain matches to fit homography
retval, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 100.0)
mask = mask.ravel()
# We select only inlier points
pts1 = src_pts[mask == 1]
pts2 = dst_pts[mask == 1]
return pts1.T, pts2.T