diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index d8e54dd6..57eb3bb7 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -95,9 +95,9 @@ def peak_local_max(image, min_distance=10, threshold='deprecated', # get coordinates of peaks coordinates = np.transpose(image_t.nonzero()) - if len(coordinates) > num_peaks: - intensities = image[tuple(coordinates.T)] + if coordinates.shape[0] > num_peaks: + intensities = image[coordinates[:, 0], coordinates[:, 1]] idx_maxsort = np.argsort(intensities)[::-1] - coordinates = coordinates[idx_maxsort][:2] + coordinates = coordinates[idx_maxsort][:num_peaks] return coordinates diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index eaedc84f..13457781 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -51,15 +51,23 @@ def test_flat_peak(): def test_num_peaks(): - image = np.zeros((3, 7), dtype=np.uint8) + image = np.zeros((7, 7), dtype=np.uint8) image[1, 1] = 10 image[1, 3] = 11 image[1, 5] = 12 - assert len(peak.peak_local_max(image, min_distance=1)) == 3 + image[3, 5] = 8 + image[5, 3] = 7 + assert len(peak.peak_local_max(image, min_distance=1)) == 5 peaks_limited = peak.peak_local_max(image, min_distance=1, num_peaks=2) assert len(peaks_limited) == 2 assert (1, 3) in peaks_limited assert (1, 5) in peaks_limited + peaks_limited = peak.peak_local_max(image, min_distance=1, num_peaks=4) + assert len(peaks_limited) == 4 + assert (1, 3) in peaks_limited + assert (1, 5) in peaks_limited + assert (1, 1) in peaks_limited + assert (3, 5) in peaks_limited if __name__ == '__main__':