diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index d57b6015..92ac35b3 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -202,4 +202,7 @@ Extended the image labelling implementation so it also works on 3D images. - Salvatore Scaramuzzino - RectTool example \ No newline at end of file + RectTool example + +- Kevin Keraudren + Fix and test for feature.peak_local_max diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index e70f6df1..599a6e58 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -146,10 +146,10 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, peak_threshold = max(np.max(image.ravel()) * threshold_rel, threshold_abs) # get coordinates of peaks - coordinates = np.transpose((image > peak_threshold).nonzero()) + coordinates = np.argwhere(image > peak_threshold) if coordinates.shape[0] > num_peaks: - intensities = image[coordinates[:, 0], coordinates[:, 1]] + intensities = image.flat[np.ravel_multi_index(coordinates.transpose(),image.shape)] idx_maxsort = np.argsort(intensities)[::-1] coordinates = coordinates[idx_maxsort][:num_peaks] diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index ec130c66..09e52495 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -82,6 +82,15 @@ def test_num_peaks(): assert (3, 5) in peaks_limited +def test_num_peaks3D(): + # Issue 1354: the old code only hold for 2D arrays + # and this code would die with IndexError + image = np.zeros((10, 10, 100)) + image[5,5,::5] = np.arange(20) + peaks_limited = peak.peak_local_max(image, min_distance=1, num_peaks=2) + assert len(peaks_limited) == 2 + + def test_reorder_labels(): image = np.random.uniform(size=(40, 60)) i, j = np.mgrid[0:40, 0:60]