scikit-image
110 строк · 3.1 Кб
1import pytest2import copy3
4import numpy as np5
6from skimage._shared.testing import assert_array_equal7from skimage import data8from skimage.feature import BRIEF, corner_peaks, corner_harris9from skimage._shared import testing10
11
12def test_color_image_unsupported_error():13"""Brief descriptors can be evaluated on gray-scale images only."""14img = np.zeros((20, 20, 3))15keypoints = np.asarray([[7, 5], [11, 13]])16with testing.raises(ValueError):17BRIEF().extract(img, keypoints)18
19
20@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'int'])21def test_normal_mode(dtype):22"""Verify the computed BRIEF descriptors with expected for normal mode."""23img = data.coins().astype(dtype)24
25keypoints = corner_peaks(26corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.127)28
29extractor = BRIEF(descriptor_size=8, sigma=2)30
31extractor.extract(img, keypoints[:8])32
33expected = np.array(34[35[1, 1, 1, 0, 1, 1, 0, 1],36[0, 1, 1, 0, 1, 1, 0, 0],37[1, 1, 1, 0, 1, 1, 0, 1],38[0, 0, 0, 1, 0, 0, 1, 0],39[0, 1, 1, 0, 1, 1, 0, 0],40[0, 1, 1, 0, 1, 1, 1, 0],41[1, 1, 1, 0, 1, 1, 0, 1],42[1, 0, 1, 0, 0, 1, 1, 0],43],44dtype=bool,45)46
47assert_array_equal(extractor.descriptors, expected)48
49
50@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'int'])51def test_uniform_mode(dtype):52"""Verify the computed BRIEF descriptors with expected for uniform mode."""53img = data.coins().astype(dtype)54
55keypoints = corner_peaks(56corner_harris(img), min_distance=5, threshold_abs=0, threshold_rel=0.157)58
59extractor = BRIEF(descriptor_size=8, sigma=2, mode='uniform', rng=1)60BRIEF(descriptor_size=8, sigma=2, mode='uniform', rng=1)61
62extractor.extract(img, keypoints[:8])63
64expected = np.array(65[66[0, 1, 0, 1, 0, 1, 1, 0],67[0, 1, 0, 0, 0, 1, 0, 1],68[0, 1, 0, 0, 0, 1, 1, 1],69[1, 0, 1, 0, 1, 0, 1, 1],70[0, 0, 1, 0, 0, 1, 0, 1],71[0, 1, 0, 1, 0, 1, 0, 1],72[0, 1, 0, 0, 0, 1, 1, 1],73[1, 0, 1, 1, 1, 0, 0, 1],74],75dtype=bool,76)77
78assert_array_equal(extractor.descriptors, expected)79
80
81def test_unsupported_mode():82with testing.raises(ValueError):83BRIEF(mode='foobar')84
85
86@pytest.mark.parametrize('dtype', ['float32', 'float64', 'uint8', 'int'])87def test_border(dtype):88img = np.zeros((100, 100), dtype=dtype)89keypoints = np.array([[1, 1], [20, 20], [50, 50], [80, 80]])90
91extractor = BRIEF(patch_size=41, rng=1)92extractor.extract(img, keypoints)93
94assert extractor.descriptors.shape[0] == 395assert_array_equal(extractor.mask, (False, True, True, True))96
97
98def test_independent_rng():99img = np.zeros((100, 100), dtype=int)100keypoints = np.array([[1, 1], [20, 20], [50, 50], [80, 80]])101
102rng = np.random.default_rng()103extractor = BRIEF(patch_size=41, rng=rng)104
105x = copy.deepcopy(extractor.rng).random()106rng.random()107extractor.extract(img, keypoints)108z = copy.deepcopy(extractor.rng).random()109
110assert x == z111