From 1227507c9e42f44e5f8de08664315f7d4641063c Mon Sep 17 00:00:00 2001 From: Ankit Agrawal Date: Thu, 8 Aug 2013 18:08:49 +0530 Subject: [PATCH] Actively filtering border keypoints for all the scales part 2 --- skimage/feature/censure.py | 39 +++++++++++++++------------ skimage/feature/tests/test_censure.py | 34 ++++++++++++----------- skimage/feature/util.py | 13 +++++---- 3 files changed, 49 insertions(+), 37 deletions(-) diff --git a/skimage/feature/censure.py b/skimage/feature/censure.py index 1a2f495c..7500106d 100644 --- a/skimage/feature/censure.py +++ b/skimage/feature/censure.py @@ -5,6 +5,7 @@ from skimage.transform import integral_image from skimage.feature.corner import _compute_auto_correlation from skimage.util import img_as_float from skimage.morphology import convex_hull_image +from skimage.feature.util import _remove_border_keypoints from skimage.feature.censure_cy import _censure_dob_loop @@ -204,24 +205,28 @@ def censure_keypoints(image, n_scales=7, mode='DoB', non_max_threshold=0.15, feature_mask[:, :, i] = _suppress_lines(feature_mask[:, :, i], image, (1 + i / 3.0), line_threshold) - if mode == 'Octagon': - for i in range(1, n_scales - 1): - c = (OCTAGON_OUTER_SHAPE[i][0] - 1) // 2 + OCTAGON_OUTER_SHAPE[i][1] - feature_mask[:c, :, i] = False - feature_mask[:, :c, i] = False - feature_mask[-c:, :, i] = False - feature_mask[:, -c:, i] = False - - elif mode == 'STAR': - for i in range(1, n_scales - 1): - c = STAR_SHAPE[STAR_FILTER_SHAPE[i][0]] + STAR_SHAPE[STAR_FILTER_SHAPE[i][0]] // 2 - feature_mask[:c, :, i] = False - feature_mask[:, :c, i] = False - feature_mask[-c:, :, i] = False - feature_mask[:, -c:, i] = False - rows, cols, scales = np.nonzero(feature_mask[..., 1:n_scales - 1]) keypoints = np.column_stack([rows, cols]) scales = scales + 2 - return keypoints, scales + if mode == 'DoB': + return keypoints, scales + + filtered_keypoints = np.empty((0, 2), dtype=np.int32) + filtered_scales = np.empty((0), dtype=np.int32) + + if mode == 'Octagon': + for i in range(2, n_scales): + c = (OCTAGON_OUTER_SHAPE[i - 1][0] - 1) // 2 + OCTAGON_OUTER_SHAPE[i - 1][1] + filtered_keypoints_for_scale = _remove_border_keypoints(image, keypoints[scales == i], c) + filtered_keypoints = np.vstack((filtered_keypoints, filtered_keypoints_for_scale)) + filtered_scales = np.hstack((filtered_scales, np.asarray(len(filtered_keypoints_for_scale) * [i], dtype=np.int32))) + + elif mode == 'STAR': + for i in range(2, n_scales): + c = STAR_SHAPE[STAR_FILTER_SHAPE[i - 1][0]] + STAR_SHAPE[STAR_FILTER_SHAPE[i - 1][0]] // 2 + filtered_keypoints_for_scale = _remove_border_keypoints(image, keypoints[scales == i], c) + filtered_keypoints = np.vstack((filtered_keypoints, filtered_keypoints_for_scale)) + filtered_scales = np.hstack((filtered_scales, np.asarray(len(filtered_keypoints_for_scale) * [i], dtype=np.int32))) + + return filtered_keypoints, filtered_scales diff --git a/skimage/feature/tests/test_censure.py b/skimage/feature/tests/test_censure.py index deebc9c1..4f1ddcfb 100644 --- a/skimage/feature/tests/test_censure.py +++ b/skimage/feature/tests/test_censure.py @@ -28,7 +28,8 @@ def test_censure_keypoints_moon_image_DoB(): [464, 132], [467, 260]]) expected_scale = np.array([2, 4, 6, 3, 4, 4, 2, 2, 3, 2, 2, 2]) - + print actual_scale + print actual_kp_DoB assert_array_equal(expected_kp_DoB, actual_kp_DoB) assert_array_equal(expected_scale, actual_scale) @@ -38,13 +39,15 @@ def test_censure_keypoints_moon_image_Octagon(): the expected values for Octagon filter.""" img = moon() actual_kp_Octagon, actual_scale = censure_keypoints(img, 7, 'Octagon', 0.15) - expected_kp_Octagon = np.array([[ 21, 496], - [ 35, 46], - [287, 250], + expected_kp_Octagon = np.array([[287, 250], [356, 239], - [463, 116]]) - expected_scale = np.array([3, 4, 2, 2, 2]) + [463, 116], + [ 21, 496], + [ 35, 46]]) + expected_scale = np.array([2, 2, 2, 3, 4], dtype=np.int32) + print actual_scale + print actual_kp_Octagon assert_array_equal(expected_kp_Octagon, actual_kp_Octagon) assert_array_equal(expected_scale, actual_scale) @@ -54,18 +57,19 @@ def test_censure_keypoints_moon_image_STAR(): the expected values for STAR filter.""" img = moon() actual_kp_STAR, actual_scale = censure_keypoints(img, 7, 'STAR', 0.15) - expected_kp_STAR = np.array([[ 21, 497], - [ 36, 46], - [117, 356], - [185, 177], - [260, 227], + expected_kp_STAR = np.array([[185, 177], [287, 250], + [463, 116], + [467, 260], + [ 21, 497], + [ 36, 46], + [260, 227], [357, 239], [451, 281], - [463, 116], - [467, 260]]) - expected_scale = np.array([3, 3, 6, 2, 3, 2, 3, 5, 2, 2]) - + [117, 356]]) + expected_scale = np.array([2, 2, 2, 2, 3, 3, 3, 3, 5, 6], dtype=np.int32) + print actual_scale + print actual_kp_STAR assert_array_equal(expected_kp_STAR, actual_kp_STAR) assert_array_equal(expected_scale, actual_scale) diff --git a/skimage/feature/util.py b/skimage/feature/util.py index aec4dfc8..f327cff5 100644 --- a/skimage/feature/util.py +++ b/skimage/feature/util.py @@ -1,3 +1,4 @@ +import numpy as np def _remove_border_keypoints(image, keypoints, dist): @@ -5,11 +6,13 @@ def _remove_border_keypoints(image, keypoints, dist): width = image.shape[0] height = image.shape[1] - keypoints = keypoints[(dist - 1 < keypoints[:, 0]) - & (keypoints[:, 0] < width - dist + 1) - & (dist - 1 < keypoints[:, 1]) - & (keypoints[:, 1] < height - dist + 1)] - + try: + keypoints = keypoints[(dist - 1 < keypoints[:, 0]) + & (keypoints[:, 0] < width - dist + 1) + & (dist - 1 < keypoints[:, 1]) + & (keypoints[:, 1] < height - dist + 1)] + except IndexError: + return np.empty((0, 2), dtype=np.int32) return keypoints