scikit-image
321 строка · 7.9 Кб
1import numpy as np2from skimage._shared.testing import assert_equal3from skimage import data4from skimage import transform5from skimage.color import rgb2gray6from skimage.feature import BRIEF, match_descriptors, corner_peaks, corner_harris7from skimage._shared import testing8
9
10def test_binary_descriptors_unequal_descriptor_sizes_error():11"""Sizes of descriptors of keypoints to be matched should be equal."""12descs1 = np.array([[True, True, False, True], [False, True, False, True]])13descs2 = np.array(14[[True, False, False, True, False], [False, True, True, True, False]]15)16with testing.raises(ValueError):17match_descriptors(descs1, descs2)18
19
20def test_binary_descriptors():21descs1 = np.array(22[[True, True, False, True, True], [False, True, False, True, True]]23)24descs2 = np.array(25[[True, False, False, True, False], [False, False, True, True, True]]26)27matches = match_descriptors(descs1, descs2)28assert_equal(matches, [[0, 0], [1, 1]])29
30
31def test_binary_descriptors_rotation_crosscheck_false():32"""Verify matched keypoints and their corresponding masks results between33image and its rotated version with the expected keypoint pairs with
34cross_check disabled."""
35img = data.astronaut()36img = rgb2gray(img)37tform = transform.SimilarityTransform(scale=1, rotation=0.15, translation=(0, 0))38rotated_img = transform.warp(img, tform, clip=False)39
40extractor = BRIEF(descriptor_size=512)41
42keypoints1 = corner_peaks(43corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.144)45extractor.extract(img, keypoints1)46descriptors1 = extractor.descriptors47
48keypoints2 = corner_peaks(49corner_harris(rotated_img), min_distance=5, threshold_abs=0, threshold_rel=0.150)51extractor.extract(rotated_img, keypoints2)52descriptors2 = extractor.descriptors53
54matches = match_descriptors(descriptors1, descriptors2, cross_check=False)55
56exp_matches1 = np.arange(47)57exp_matches2 = np.array(58[590,602,611,623,634,645,657,668,6714,689,6911,7013,7123,7215,7316,7422,7517,7619,7737,7818,7924,8027,8130,8225,8326,8432,8528,8635,8737,8842,8929,9038,9133,9240,9336,9439,9510,9636,9743,9815,9935,10041,1016,10237,10332,10424,1058,106]107)108
109assert_equal(matches[:, 0], exp_matches1)110assert_equal(matches[:, 1], exp_matches2)111
112# minkowski takes a different code path, therefore we test it explicitly113matches = match_descriptors(114descriptors1, descriptors2, metric='minkowski', cross_check=False115)116assert_equal(matches[:, 0], exp_matches1)117assert_equal(matches[:, 1], exp_matches2)118
119# it also has an extra parameter120matches = match_descriptors(121descriptors1, descriptors2, metric='minkowski', p=4, cross_check=False122)123assert_equal(matches[:, 0], exp_matches1)124assert_equal(matches[:, 1], exp_matches2)125
126
127def test_binary_descriptors_rotation_crosscheck_true():128"""Verify matched keypoints and their corresponding masks results between129image and its rotated version with the expected keypoint pairs with
130cross_check enabled."""
131img = data.astronaut()132img = rgb2gray(img)133tform = transform.SimilarityTransform(scale=1, rotation=0.15, translation=(0, 0))134rotated_img = transform.warp(img, tform, clip=False)135
136extractor = BRIEF(descriptor_size=512)137
138keypoints1 = corner_peaks(139corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.1140)141extractor.extract(img, keypoints1)142descriptors1 = extractor.descriptors143
144keypoints2 = corner_peaks(145corner_harris(rotated_img), min_distance=5, threshold_abs=0, threshold_rel=0.1146)147extractor.extract(rotated_img, keypoints2)148descriptors2 = extractor.descriptors149
150matches = match_descriptors(descriptors1, descriptors2, cross_check=True)151
152exp_matches1 = np.array(153[1540,1551,1562,1573,1584,1595,1606,1617,1628,1639,16410,16511,16612,16713,16814,16915,17016,17117,17219,17320,17421,17522,17623,17724,17826,17927,18028,18129,18230,18331,18432,18533,18634,18738,18841,18942,190]191)192exp_matches2 = np.array(193[1940,1952,1961,1973,1984,1995,2007,2018,20214,2039,20411,20513,20623,20715,20816,20922,21017,21119,21218,21324,21427,21530,21625,21726,21828,21935,22037,22142,22229,22338,22433,22540,22636,22743,22841,2296,230]231)232assert_equal(matches[:, 0], exp_matches1)233assert_equal(matches[:, 1], exp_matches2)234
235
236def test_max_distance():237descs1 = np.zeros((10, 128))238descs2 = np.zeros((15, 128))239
240descs1[0, :] = 1241
242matches = match_descriptors(243descs1, descs2, metric='euclidean', max_distance=0.1, cross_check=False244)245assert len(matches) == 9246
247matches = match_descriptors(248descs1,249descs2,250metric='euclidean',251max_distance=np.sqrt(128.1),252cross_check=False,253)254assert len(matches) == 10255
256matches = match_descriptors(257descs1, descs2, metric='euclidean', max_distance=0.1, cross_check=True258)259assert_equal(matches, [[1, 0]])260
261matches = match_descriptors(262descs1,263descs2,264metric='euclidean',265max_distance=np.sqrt(128.1),266cross_check=True,267)268assert_equal(matches, [[1, 0]])269
270
271def test_max_ratio():272descs1 = 10 * np.arange(10)[:, None].astype(np.float32)273descs2 = 10 * np.arange(15)[:, None].astype(np.float32)274
275descs2[0] = 5.0276
277matches = match_descriptors(278descs1, descs2, metric='euclidean', max_ratio=1.0, cross_check=False279)280assert_equal(len(matches), 10)281
282matches = match_descriptors(283descs1, descs2, metric='euclidean', max_ratio=0.6, cross_check=False284)285assert_equal(len(matches), 10)286
287matches = match_descriptors(288descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False289)290assert_equal(len(matches), 9)291
292descs1[0] = 7.5293
294matches = match_descriptors(295descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False296)297assert_equal(len(matches), 9)298
299descs2 = 10 * np.arange(1)[:, None].astype(np.float32)300
301matches = match_descriptors(302descs1, descs2, metric='euclidean', max_ratio=1.0, cross_check=False303)304assert_equal(len(matches), 10)305
306matches = match_descriptors(307descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False308)309assert_equal(len(matches), 10)310
311descs1 = 10 * np.arange(1)[:, None].astype(np.float32)312
313matches = match_descriptors(314descs1, descs2, metric='euclidean', max_ratio=1.0, cross_check=False315)316assert_equal(len(matches), 1)317
318matches = match_descriptors(319descs1, descs2, metric='euclidean', max_ratio=0.5, cross_check=False320)321assert_equal(len(matches), 1)322