Skip to content

corner_peaks does not return the num_peaks in Intensity order when using labels #8143

Description

@ryandeon

Description:

The documentation for parameter num_peaks (https://scikit-image.org/docs/stable/api/skimage.feature.html#skimage.feature.peak_local_max) says
"When the number of peaks exceeds num_peaks, return num_peaks peaks based on highest peak intensity."

However, when you also supply labels, it appears that the order of returned peaks is actually first, in label ID order, then within a label, in descending intensity order. This can hide the strongest peaks, if they occur in later-numbered labels.
For example, if you have three labelled regions, with peak intensities of label0 : .1, 0.2; label1: 0.01; label3: 0.5.
calling
peaks = skimage.feature.corner_peaks(match,num_peaks=2,labels=labels) would only return peaks from label0.
calling
peaks = skimage.feature.corner_peaks(match,num_peaks=2,labels=labels,num_peaks_per_label=1) returns only label0 and label1 peaks.

Since I am trying to find the highest peak in each label, then filter down to only a few of the highest across the image, my work-around is

peaks = skimage.feature.corner_peaks(match,min_distance=5,num_peaks=np.max(labels),labels=labels,num_peaks_per_label=1) ##guarantees each label returns a single peak
peak_intensities = [match[peaks[i,0],peaks[i,1]] for i in range(len(peaks))]
peaks = peaks[np.argsort(peak_intensities)[::-1],:] 
peaks = peaks[:4,:] ## in this case, 4 would have been my original argument to num_peaks, if it worked the way I expected.

Way to reproduce:

import numpy as np
import skimage

img = np.zeros((6,6))
img[1,1] = 1
img[2,3] = 2
img[4,4] = 3

labels = skimage.measure.label(img>0)
peaks = skimage.feature.corner_peaks(img,num_peaks=2,labels=labels,num_peaks_per_label=1)
print(peaks) ##should be 4,4 then 2,3. instead gives 1,1 then 2,3

Version information:

3.13.12 (tags/v3.13.12:1cbe481, Feb  3 2026, 18:22:25) [MSC v.1944 64 bit (AMD64)]
Windows-11-10.0.26100-SP0
scikit-image version: 0.25.2
numpy version: 2.4.2

Activity

  1. changed the title [-]peak_local_max does not return the num_peaks in Intensity order when using labels[/-] [+]corner_peaks does not return the num_peaks in Intensity order when using labels[/+] on May 5, 2026
  2. michaelbratsch commented on May 16, 2026

    @michaelbratsch
    Contributor

    @ryandeon Thanks for taking the time to properly document the bug report and to come up with a small reproducer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions