diff --git a/skimage/feature/corner.py b/skimage/feature/corner.py index 0d7e1d3e..c8e45d3b 100644 --- a/skimage/feature/corner.py +++ b/skimage/feature/corner.py @@ -2,7 +2,7 @@ import numpy as np from scipy import ndimage from scipy import stats from skimage.color import rgb2grey -from skimage.util import img_as_float +from skimage.util import img_as_float, pad from skimage.feature import peak_local_max @@ -356,6 +356,11 @@ def corner_subpix(image, corners, window_size=11, alpha=0.99): # window extent in one direction wext = (window_size - 1) / 2 + image = pad(image, pad_width=wext, mode='constant', constant_values=0) + + # add pad width, make sure to not modify the input values in-place + corners = corners + wext + # normal equation arrays N_dot = np.zeros((2, 2), dtype=np.double) N_edge = np.zeros((2, 2), dtype=np.double) @@ -449,6 +454,9 @@ def corner_subpix(image, corners, window_size=11, alpha=0.99): elif corner_class == 1: corners_subpix[i, :] = y0 + est_edge[0], x0 + est_edge[1] + # subtract pad width + corners_subpix -= wext + return corners_subpix diff --git a/skimage/feature/tests/test_corner.py b/skimage/feature/tests/test_corner.py index 66ef41c6..9c07c006 100644 --- a/skimage/feature/tests/test_corner.py +++ b/skimage/feature/tests/test_corner.py @@ -1,5 +1,5 @@ import numpy as np -from numpy.testing import assert_array_equal +from numpy.testing import assert_array_equal, assert_almost_equal from skimage import data from skimage import img_as_float @@ -101,6 +101,22 @@ def test_subpix(): assert_array_equal(subpix[0], (24.5, 24.5)) +def test_subpix_border(): + img = np.zeros((50, 50)) + img[1:25,1:25] = 255 + img[25:-1,25:-1] = 255 + corner = corner_peaks(corner_harris(img), min_distance=1) + subpix = corner_subpix(img, corner, window_size=11) + ref = np.array([[ 0.52040816, 0.52040816], + [ 0.52040816, 24.47959184], + [24.47959184, 0.52040816], + [24.5 , 24.5 ], + [24.52040816, 48.47959184], + [48.47959184, 24.52040816], + [48.47959184, 48.47959184]]) + assert_almost_equal(subpix, ref) + + def test_num_peaks(): """For a bunch of different values of num_peaks, check that peak_local_max returns exactly the right amount of peaks. Test