-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfeatures.py
84 lines (72 loc) · 2.32 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import cv2
import matplotlib.tri as tri
import numpy as np
def get_unique(keypoints, descriptors):
'''
Returns integer rounding of keypoints, taking care of repeated instances.
Parameters
----------
keypoints: list of KeyPoint
Keypoints detected.
descriptors: ndarray
Corresponding descriptors to the keypoints found.
Returns
-------
keypoints: list of KeyPoint
Unique integer keypoints.
descriptors: ndarray
Corresponding unique descriptors to the keypoints.
'''
kp_found = set()
unique_kp = []
unique_desc = []
for i, (kp, desc) in enumerate(zip(keypoints, descriptors)):
x, y = kp.pt
x = round(x)
y = round(y)
if (x, y) not in kp_found:
kp_found.add((x, y))
keypoints[i].pt = (x, y)
unique_kp.append(keypoints[i])
unique_desc.append(desc)
return unique_kp, np.array(unique_desc)
def get_features(img, hessian_threshold=400, num_keypoints=None):
'''
Return keypoints and descriptors of SURF detection algorithm. For
details see
http://docs.opencv.org/3.1.0/df/dd2/tutorial_py_surf_intro.html.
Parameters
----------
img: ndarray
Input image, should be single-channel.
hessian_threshold:
Hessian threshold used by SURF.
num_keypoints:
Number of desired keypoints to return.
Returns
-------
keypoints: list of KeyPoint
Keypoints detected.
descriptors: ndarray
Corresponding descriptors to the keypoints found.
'''
surf = cv2.xfeatures2d.SURF_create(hessian_threshold)
keypoints, descriptors = surf.detectAndCompute(img, None)
return get_unique(keypoints[:num_keypoints], descriptors[:num_keypoints])
def get_triangulation(keypoints):
'''
Calculate Delaunay triangulation of given SURF keypoints.
Parameters
----------
keypoints: list of KeyPoint
Keypoints detected by SURF.
Returns
-------
triangulation: ndarray
Delaunay triangulation of keypoints as a matrix, where each row
denotes a triangle and each column the indices in the keypoints
list that form said triangle.
'''
x, y = zip(*[k.pt for k in keypoints])
triangulation = tri.Triangulation(x, y)
return triangulation.get_masked_triangles()