diff --git a/skimage/feature/__init__.py b/skimage/feature/__init__.py index 8df1dc10..d0c51fb0 100644 --- a/skimage/feature/__init__.py +++ b/skimage/feature/__init__.py @@ -2,8 +2,9 @@ from ._daisy import daisy from ._hog import hog from .texture import greycomatrix, greycoprops, local_binary_pattern from .peak import peak_local_max -from .corner import (corner_kitchen_rosenfeld, corner_harris, corner_shi_tomasi, - corner_foerstner, corner_subpix, corner_peaks) +from .corner import (corner_kitchen_rosenfeld, corner_harris, + corner_shi_tomasi, corner_foerstner, corner_subpix, + corner_peaks) from .corner_cy import corner_moravec from .template import match_template from ._brief import brief, match_keypoints_brief diff --git a/skimage/feature/_brief.py b/skimage/feature/_brief.py index 30c0934f..22b8e75a 100644 --- a/skimage/feature/_brief.py +++ b/skimage/feature/_brief.py @@ -31,7 +31,7 @@ def brief(image, keypoints, descriptor_size=256, mode='normal', patch_size=49, length patch_size, pixel pairs are sampled using the `mode` parameter to build the descriptors using intensity comparison. The value of `sample_seed` should be the same for the images to be matched while - building the descriptors. Default is 1. + building the descriptors. Default is 1. variance : float Variance of the Gaussian Low Pass filter applied on the image to alleviate noise sensitivity. Default is 2. @@ -75,7 +75,7 @@ def brief(image, keypoints, descriptor_size=256, mode='normal', patch_size=49, [2, 5], [5, 2], [5, 5]]) - >>> descriptors1, keypoints1 = brief(square1, keypoints1, patch_size = 5) + >>> descriptors1, keypoints1 = brief(square1, keypoints1, patch_size=5) >>> keypoints1 array([[2, 2], [2, 5], @@ -99,7 +99,7 @@ def brief(image, keypoints, descriptor_size=256, mode='normal', patch_size=49, [2, 6], [6, 2], [6, 6]]) - >>> descriptors2, keypoints2 = brief(square2, keypoints2, patch_size = 5) + >>> descriptors2, keypoints2 = brief(square2, keypoints2, patch_size=5) >>> keypoints2 array([[2, 2], [2, 6], @@ -163,7 +163,8 @@ def brief(image, keypoints, descriptor_size=256, mode='normal', patch_size=49, else: - samples = np.random.randint(-(patch_size - 2) // 2, (patch_size // 2) + 1, + samples = np.random.randint(-(patch_size - 2) // 2, + (patch_size // 2) + 1, (descriptor_size * 2, 2)) pos1, pos2 = np.split(samples, 2) @@ -200,21 +201,21 @@ def match_keypoints_brief(keypoints1, descriptors1, keypoints2, Location of Q matched keypoint pairs from two images. """ - if keypoints1.shape[0] != descriptors1.shape[0] or \ - keypoints2.shape[0] != descriptors2.shape[0]: - raise ValueError("The number of keypoints and number of described \ - keypoints do not match. Make the optional parameter \ - return_keypoints True to get described keypoints.") + if (keypoints1.shape[0] != descriptors1.shape[0] + or keypoints2.shape[0] != descriptors2.shape[0]): + raise ValueError("The number of keypoints and number of described " + "keypoints do not match. Make the optional parameter " + "return_keypoints True to get described keypoints.") if descriptors1.shape[1] != descriptors2.shape[1]: - raise ValueError("Descriptor sizes for matching keypoints in both \ - the images should be equal.") + raise ValueError("Descriptor sizes for matching keypoints in both " + "the images should be equal.") # Get hamming distances between keeypoints1 and keypoints2 distance = pairwise_hamming_distance(descriptors1, descriptors2) temp = distance > threshold - row_check = np.any(~temp, axis = 1) + row_check = np.any(~temp, axis=1) matched_keypoints2 = keypoints2[np.argmin(distance, axis=1)] matched_keypoint_pairs = np.zeros((np.sum(row_check), 2, 2), dtype=np.intp) matched_keypoint_pairs[:, 0, :] = keypoints1[row_check] diff --git a/skimage/feature/tests/test_brief.py b/skimage/feature/tests/test_brief.py index 65c99af2..3d126770 100644 --- a/skimage/feature/tests/test_brief.py +++ b/skimage/feature/tests/test_brief.py @@ -15,48 +15,49 @@ def test_brief_color_image_unsupported_error(): def test_match_keypoints_brief_lena_translation(): - """Test matched keypoints between lena image and its translated version.""" - img = data.lena() - img = rgb2gray(img) - img.shape - tform = tf.SimilarityTransform(scale=1, rotation=0, translation=(15, 20)) - translated_img = tf.warp(img, tform) + """Test matched keypoints between lena image and its translated version.""" + img = data.lena() + img = rgb2gray(img) + img.shape + tform = tf.SimilarityTransform(scale=1, rotation=0, translation=(15, 20)) + translated_img = tf.warp(img, tform) - keypoints1 = corner_peaks(corner_harris(img), min_distance=5) - descriptors1, keypoints1 = brief(img, keypoints1, descriptor_size=512) + keypoints1 = corner_peaks(corner_harris(img), min_distance=5) + descriptors1, keypoints1 = brief(img, keypoints1, descriptor_size=512) - keypoints2 = corner_peaks(corner_harris(translated_img), min_distance=5) - descriptors2, keypoints2 = brief(translated_img, keypoints2, - descriptor_size=512) + keypoints2 = corner_peaks(corner_harris(translated_img), min_distance=5) + descriptors2, keypoints2 = brief(translated_img, keypoints2, + descriptor_size=512) - matched_keypoints = match_keypoints_brief(keypoints1, descriptors1, - keypoints2, descriptors2, - threshold=0.10) + matched_keypoints = match_keypoints_brief(keypoints1, descriptors1, + keypoints2, descriptors2, + threshold=0.10) - assert_array_equal(matched_keypoints[:, 0,:], matched_keypoints[:, 1,:] + - [20, 15]) + assert_array_equal(matched_keypoints[:, 0, :], matched_keypoints[:, 1, :] + + [20, 15]) def test_match_keypoints_brief_lena_rotation(): - """Verify matched keypoints result between lena image and its rotated version - with the expected keypoint pairs.""" - img = data.lena() - img = rgb2gray(img) - img.shape - tform = tf.SimilarityTransform(scale=1, rotation=0.10, translation=(0, 0)) - rotated_img = tf.warp(img, tform) + """Verify matched keypoints result between lena image and its rotated + version with the expected keypoint pairs.""" + img = data.lena() + img = rgb2gray(img) + img.shape + tform = tf.SimilarityTransform(scale=1, rotation=0.10, translation=(0, 0)) + rotated_img = tf.warp(img, tform) - keypoints1 = corner_peaks(corner_harris(img), min_distance=5) - descriptors1, keypoints1 = brief(img, keypoints1, descriptor_size=512) + keypoints1 = corner_peaks(corner_harris(img), min_distance=5) + descriptors1, keypoints1 = brief(img, keypoints1, descriptor_size=512) - keypoints2 = corner_peaks(corner_harris(rotated_img), min_distance=5) - descriptors2, keypoints2 = brief(rotated_img, keypoints2, - descriptor_size=512) + keypoints2 = corner_peaks(corner_harris(rotated_img), min_distance=5) + descriptors2, keypoints2 = brief(rotated_img, keypoints2, + descriptor_size=512) - matched_keypoints = match_keypoints_brief(keypoints1, descriptors1, - keypoints2, descriptors2, - threshold=0.07) - expected = np.array([[[263, 272], + matched_keypoints = match_keypoints_brief(keypoints1, descriptors1, + keypoints2, descriptors2, + threshold=0.07) + + expected = np.array([[[263, 272], [234, 298]], [[271, 120], @@ -74,5 +75,4 @@ def test_match_keypoints_brief_lena_rotation(): [[454, 176], [435, 221]]]) - - assert_array_equal(matched_keypoints, expected) + assert_array_equal(matched_keypoints, expected) diff --git a/skimage/feature/tests/test_util.py b/skimage/feature/tests/test_util.py index 6a397e5b..6e2215c5 100644 --- a/skimage/feature/tests/test_util.py +++ b/skimage/feature/tests/test_util.py @@ -2,26 +2,26 @@ import numpy as np from numpy.testing import assert_array_equal from skimage.feature.util import pairwise_hamming_distance + def test_pairwise_hamming_distance_range(): - """Values of all the pairwise hamming distances should be in the range - [0, 1]. - """ - a = np.random.random_sample((10, 50)) > 0.5 - b = np.random.random_sample((20, 50)) > 0.5 - dist = pairwise_hamming_distance(a, b) - assert np.all((0 <= dist) & (dist <= 1)) + """Values of all the pairwise hamming distances should be in the range + [0, 1].""" + a = np.random.random_sample((10, 50)) > 0.5 + b = np.random.random_sample((20, 50)) > 0.5 + dist = pairwise_hamming_distance(a, b) + assert np.all((0 <= dist) & (dist <= 1)) + def test_pairwise_hamming_distance_value(): - """The result of pairwise_hamming_distance of two fixed sets of boolean - vectors should be same as expected. - """ - np.random.seed(10) - a = np.random.random_sample((4, 100)) > 0.5 - np.random.seed(20) - b = np.random.random_sample((3, 100)) > 0.5 - result = pairwise_hamming_distance(a, b) - expected = np.array([[ 0.5 , 0.49, 0.44], - [ 0.44, 0.53, 0.52], - [ 0.4 , 0.55, 0.5 ], - [ 0.47, 0.48, 0.57]]) - assert_array_equal(result, expected) + """The result of pairwise_hamming_distance of two fixed sets of boolean + vectors should be same as expected.""" + np.random.seed(10) + a = np.random.random_sample((4, 100)) > 0.5 + np.random.seed(20) + b = np.random.random_sample((3, 100)) > 0.5 + result = pairwise_hamming_distance(a, b) + expected = np.array([[0.5 , 0.49, 0.44], + [0.44, 0.53, 0.52], + [0.4 , 0.55, 0.5 ], + [0.47, 0.48, 0.57]]) + assert_array_equal(result, expected) diff --git a/skimage/feature/util.py b/skimage/feature/util.py index a7a3670f..aec4dfc8 100644 --- a/skimage/feature/util.py +++ b/skimage/feature/util.py @@ -1,4 +1,5 @@ + def _remove_border_keypoints(image, keypoints, dist): """Removes keypoints that are within dist pixels from the image border.""" width = image.shape[0] @@ -31,5 +32,5 @@ def pairwise_hamming_distance(array1, array2): vector in array2. """ - distance = (array1[:,None] != array2[None]).mean(axis=2) + distance = (array1[:, None] != array2[None]).mean(axis=2) return distance