For the purpose of detecting grey-level blobs (local extrema with extent) from a watershed analogy, Lindeberg developed an algorithm based on pre-sorting the pixels, alternatively connected regions having the same intensity, in decreasing order of the intensity values.
Else, if it has at least one higher neighbour, which is background, then it cannot be part of any blob and must be background.