Debian Codegraph @

src/pycolmap/feature/types_test.py

raw
1 import math
2
3 import numpy as np
4
5 import pycolmap
6
7
8 def test_feature_extractor_type_enum():
9 assert pycolmap.FeatureExtractorType.UNDEFINED is not None
10 assert pycolmap.FeatureExtractorType.SIFT is not None
11
12
13 def test_feature_extractor_type_int_construction():
14 sift = pycolmap.FeatureExtractorType.SIFT
15 value = int(sift)
16 assert isinstance(value, int)
17
18
19 def test_feature_descriptors_default_init():
20 descriptors = pycolmap.FeatureDescriptors()
21 assert descriptors is not None
22
23
24 def test_feature_descriptors_type_readwrite():
25 descriptors = pycolmap.FeatureDescriptors()
26 descriptors.type = pycolmap.FeatureExtractorType.SIFT
27 assert descriptors.type == pycolmap.FeatureExtractorType.SIFT
28
29
30 def test_feature_descriptors_data_readwrite():
31 data = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8)
32 descriptors = pycolmap.FeatureDescriptors(
33 type=pycolmap.FeatureExtractorType.SIFT, data=data
34 )
35 np.testing.assert_array_equal(descriptors.data, data)
36 new_data = np.array([[10, 20, 30]], dtype=np.uint8)
37 descriptors.data = new_data
38 np.testing.assert_array_equal(descriptors.data, new_data)
39
40
41 def test_feature_descriptors_float_default_init():
42 descriptors = pycolmap.FeatureDescriptorsFloat()
43 assert descriptors is not None
44
45
46 def test_feature_descriptors_float_type_readwrite():
47 descriptors = pycolmap.FeatureDescriptorsFloat()
48 descriptors.type = pycolmap.FeatureExtractorType.SIFT
49 assert descriptors.type == pycolmap.FeatureExtractorType.SIFT
50
51
52 def test_feature_descriptors_float_data_readwrite():
53 data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32)
54 descriptors = pycolmap.FeatureDescriptorsFloat(
55 type=pycolmap.FeatureExtractorType.SIFT, data=data
56 )
57 np.testing.assert_array_almost_equal(descriptors.data, data)
58 new_data = np.array([[10.0, 20.0, 30.0]], dtype=np.float32)
59 descriptors.data = new_data
60 np.testing.assert_array_almost_equal(descriptors.data, new_data)
61
62
63 def test_feature_keypoint_default_init():
64 keypoint = pycolmap.FeatureKeypoint()
65 assert keypoint is not None
66
67
68 def test_feature_keypoint_xy_readwrite():
69 keypoint = pycolmap.FeatureKeypoint()
70 keypoint.x = 10.5
71 keypoint.y = 20.5
72 assert keypoint.x == 10.5
73 assert keypoint.y == 20.5
74
75
76 def test_feature_keypoint_affine_readwrite():
77 keypoint = pycolmap.FeatureKeypoint()
78 keypoint.a11 = 1.0
79 keypoint.a12 = 0.5
80 keypoint.a21 = -0.5
81 keypoint.a22 = 1.0
82 assert keypoint.a11 == 1.0
83 assert keypoint.a12 == 0.5
84 assert keypoint.a21 == -0.5
85 assert keypoint.a22 == 1.0
86
87
88 def test_feature_keypoint_compute_scale():
89 keypoint = pycolmap.FeatureKeypoint()
90 scale = keypoint.compute_scale()
91 assert isinstance(scale, float)
92
93
94 def test_feature_keypoint_compute_scale_x():
95 keypoint = pycolmap.FeatureKeypoint()
96 scale_x = keypoint.compute_scale_x()
97 assert isinstance(scale_x, float)
98
99
100 def test_feature_keypoint_compute_scale_y():
101 keypoint = pycolmap.FeatureKeypoint()
102 scale_y = keypoint.compute_scale_y()
103 assert isinstance(scale_y, float)
104
105
106 def test_feature_keypoint_compute_orientation():
107 keypoint = pycolmap.FeatureKeypoint()
108 orientation = keypoint.compute_orientation()
109 assert isinstance(orientation, float)
110
111
112 def test_feature_keypoint_compute_shear():
113 keypoint = pycolmap.FeatureKeypoint()
114 shear = keypoint.compute_shear()
115 assert isinstance(shear, float)
116
117
118 def test_feature_keypoint_rescale():
119 keypoint = pycolmap.FeatureKeypoint()
120 keypoint.x = 10.0
121 keypoint.y = 20.0
122 keypoint.a11 = 1.0
123 keypoint.a12 = 0.0
124 keypoint.a21 = 0.0
125 keypoint.a22 = 1.0
126 keypoint.rescale(2.0, 2.0)
127 assert keypoint.x == 20.0
128 assert keypoint.y == 40.0
129
130
131 def test_feature_keypoint_from_shape_parameters():
132 keypoint = pycolmap.FeatureKeypoint.from_shape_parameters(
133 1.0, 2.0, 2.0, 2.0, math.pi, 0.0
134 )
135 assert isinstance(keypoint, pycolmap.FeatureKeypoint)
136 assert keypoint.x == 1.0
137 assert keypoint.y == 2.0
138
139
140 def test_feature_match_default_init():
141 match = pycolmap.FeatureMatch()
142 assert match is not None
143
144
145 def test_feature_match_readwrite():
146 match = pycolmap.FeatureMatch()
147 match.point2D_idx1 = 5
148 match.point2D_idx2 = 10
149 assert match.point2D_idx1 == 5
150 assert match.point2D_idx2 == 10
151
152
153 def test_feature_keypoints_vector():
154 keypoints = pycolmap.FeatureKeypoints()
155 keypoint = pycolmap.FeatureKeypoint()
156 keypoint.x = 1.0
157 keypoint.y = 2.0
158 keypoints.append(keypoint)
159 keypoints.append(pycolmap.FeatureKeypoint())
160 assert len(keypoints) == 2
161 count = 0
162 for keypoint in keypoints:
163 assert isinstance(keypoint, pycolmap.FeatureKeypoint)
164 count += 1
165 assert count == 2
166
167
168 def test_feature_matches_vector():
169 matches = pycolmap.FeatureMatches()
170 matches.append(pycolmap.FeatureMatch(0, 1))
171 matches.append(pycolmap.FeatureMatch(2, 3))
172 assert len(matches) == 2
173
174
175 def test_keypoints_to_from_matrix_roundtrip():
176 keypoints = pycolmap.FeatureKeypoints()
177 keypoint = pycolmap.FeatureKeypoint()
178 keypoint.x = 10.0
179 keypoint.y = 20.0
180 keypoint.a11 = 1.0
181 keypoint.a12 = 0.0
182 keypoint.a21 = 0.0
183 keypoint.a22 = 1.0
184 keypoints.append(keypoint)
185 matrix = pycolmap.keypoints_to_matrix(keypoints)
186 assert matrix.shape[0] == 1
187 assert matrix.shape[1] == 4
188 recovered = pycolmap.keypoints_from_matrix(matrix)
189 assert len(recovered) == 1
190 assert recovered[0].x == keypoint.x
191 assert recovered[0].y == keypoint.y
192
193
194 def test_matches_to_from_matrix_roundtrip():
195 matches = pycolmap.FeatureMatches()
196 matches.append(pycolmap.FeatureMatch(0, 5))
197 matches.append(pycolmap.FeatureMatch(1, 3))
198 matrix = pycolmap.matches_to_matrix(matches)
199 assert matrix.shape == (2, 2)
200 recovered = pycolmap.matches_from_matrix(matrix)
201 assert len(recovered) == 2
202 assert recovered[0].point2D_idx1 == 0
203 assert recovered[0].point2D_idx2 == 5
204 assert recovered[1].point2D_idx1 == 1
205 assert recovered[1].point2D_idx2 == 3