diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index 0cc1b26d..c4d1aa32 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -24,17 +24,16 @@ def peak_local_max(image, min_distance=1, threshold_abs=None, min_distance : int, optional Minimum number of pixels separating peaks in a region of `2 * min_distance + 1` (i.e. peaks are separated by at least - `min_distance`). If `exclude_border` is True, this value also excludes - a border `min_distance` from the image boundary. + `min_distance`). To find the maximum number of peaks, use `min_distance=1`. threshold_abs : float, optional Minimum intensity of peaks. By default, the absolute threshold is the minimum intensity of the image. threshold_rel : float, optional Minimum intensity of peaks, calculated as `max(image) * threshold_rel`. - exclude_border : bool, optional - If True, `min_distance` excludes peaks from the border of the image as - well as from each other. + exclude_border : int, optional + If nonzero, `exclude_border` excludes peaks from + within `exclude_border`-pixels of the border of the image. indices : bool, optional If True, the output will be an array representing peak coordinates. If False, the output will be a boolean array shaped as @@ -89,11 +88,14 @@ def peak_local_max(image, min_distance=1, threshold_abs=None, >>> img2 = np.zeros((20, 20, 20)) >>> img2[10, 10, 10] = 1 - >>> peak_local_max(img2, exclude_border=False) + >>> peak_local_max(img2, exclude_border=0) array([[10, 10, 10]]) """ + if type(exclude_border) == bool: + exclude_border = min_distance if exclude_border else 0 + out = np.zeros_like(image, dtype=np.bool) # In the case of labels, recursively build and return an output @@ -137,12 +139,12 @@ def peak_local_max(image, min_distance=1, threshold_abs=None, image_max = ndi.maximum_filter(image, size=size, mode='constant') mask = image == image_max - if exclude_border and (footprint is not None or min_distance > 0): + if exclude_border: # zero out the image borders for i in range(mask.ndim): mask = mask.swapaxes(0, i) remove = (footprint.shape[i] if footprint is not None - else 2 * min_distance) + else 2 * exclude_border) mask[:remove // 2] = mask[-remove // 2:] = False mask = mask.swapaxes(0, i) diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index 40292cd9..c921661c 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -145,8 +145,34 @@ def test_ndarray_exclude_border(): nd_image[2,2,2] = 1 expected = np.zeros_like(nd_image, dtype=np.bool) expected[2,2,2] = True - result = peak.peak_local_max(nd_image, min_distance=2, indices=False) - assert (result == expected).all() + expectedNoBorder = nd_image > 0 + result = peak.peak_local_max(nd_image, min_distance=2, + exclude_border=2, indices=False) + assert_equal(result, expected) + # Check that bools work as expected + assert_equal( + peak.peak_local_max(nd_image, min_distance=2, + exclude_border=2, indices=False), + peak.peak_local_max(nd_image, min_distance=2, + exclude_border=True, indices=False) + ) + assert_equal( + peak.peak_local_max(nd_image, min_distance=2, + exclude_border=0, indices=False), + peak.peak_local_max(nd_image, min_distance=2, + exclude_border=False, indices=False) + ) + # Check both versions with no border + assert_equal( + peak.peak_local_max(nd_image, min_distance=2, + exclude_border=0, indices=False), + expectedNoBorder, + ) + assert_equal( + peak.peak_local_max(nd_image, + exclude_border=False, indices=False), + expectedNoBorder, + ) def test_empty():