Steve on Image Processing

April 10th, 2006

Quick tip: Determining uniqueness of local maximum

A MATLAB user asked me how to determine which image pixels had local maxima that weren't unique. My answer involves a creative use of ordfilt2.

Let's review first the concept of a local maximum filter. This image operator replaces every pixel value with the maximum pixel value found in a neighborhood surrounding the pixel. For example:

a = magic(5)
a =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

What should the output be for a local maximum filter using 3-by-3 neighborhoods? The output in the 3rd row, 2nd column would be the maximum value found in this submatrix:

neighborhood = a(2:4, 1:3)
neighborhood =

    23     5     7
     4     6    13
    10    12    19

The (3,2) output is 23:

max(neighborhood(:))
ans =

    23

Local maximum filtering is the same as morphological dilation, so I usually use imdilate to compute the result for the entire image:

b = imdilate(a, ones(3,3))
b =

    24    24    24    16    16
    24    24    24    22    22
    23    23    21    22    22
    18    25    25    25    22
    18    25    25    25    21

That's the background; now back to the original question. How do you determine which pixels have multiple maxima in their neighborhoods?

I suggest using ordfilt2 twice. This function is for ''two-dimensional order-statistic filtering.'' This operation replaces every pixel value with the n-th sorted pixel value in its neighborhood. It uses an ascending sort order, so if n is the number of pixels in the neighborhood, the operation is exactly equivalent to the local maximum filter.

The specific procedure is:

1. Call ordfilt2 to get the highest pixel values in each neighborhood.

2. Call ordfilt2 again to get the second-highest pixel values in each neighborhood.

3. Determine where the outputs of steps 1 and 2 are equal. Wherever they are equal, the local maximum is not unique.

Let's try an example. First, modify our original matrix a bit:

ap = a;
ap(3,4) = 22
ap =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    22    22
    10    12    19    21     3
    11    18    25     2     9

Get the highest pixel values in each 3-by-3 neighborhood:

highest = ordfilt2(ap, 9, ones(3,3))
highest =

    24    24    24    16    16
    24    24    24    22    22
    23    23    22    22    22
    18    25    25    25    22
    18    25    25    25    21

Get the second-highest pixel values:

second_highest = ordfilt2(ap, 8, ones(3,3))
second_highest =

    23    23    14    15    15
    23    23    22    22    22
    12    19    21    22    22
    12    19    22    22    22
    12    19    21    21     9

Determine where the highest and second highest pixel values are equal:

non_unique_maxima_mask = (highest == second_highest)
non_unique_maxima_mask =

     0     0     0     0     0
     0     0     0     1     1
     0     0     0     1     1
     0     0     0     0     1
     0     0     0     0     0


Get the MATLAB code

Published with MATLAB® 7.2

12 Responses to “Quick tip: Determining uniqueness of local maximum”

  1. Steve replied on :

    Mark - see the blog policies post.

  2. Val replied on :

    “““““““““““““““““““““““
    (\(\
    (._. )
    (’)(’)_)~ thanks

  3. Amir replied on :

    Hi steve,I am new in matlab and need your help.I want to find (row,col) of local maximums in an image matrix within N*N window. what do you do suggest?

  4. Administrator replied on :

    Amir—I can think of multiple ways to interpret your question. Depending on what you really mean, you might find either the function imregionalmax or imdilate to be useful.

  5. Amir replied on :

    Thanks a lot Steve.It was very helpful.

  6. Kevin replied on :

    Hi Steve,
    The tutorial’s clear and absolutely useful. Thanks a lot.
    I have a minor question, Steve. I wonder if there is any function finding extrema in stead of maxima as ‘imregionalmax’.

    Regards.
    Kevin.

  7. Steve replied on :

    Kevin—What is your definition of “extrema” in this context?

  8. Kevin replied on :

    Hi Steve,
    I meant ‘maxima and minima’. I need to search out both of them.
    The fact that I can apply ‘imregionalmax’ and ‘imregionalmin’, then union 2 sets. But it’s great if there is any function that can do both of them probably :-).
    Thank you for the quick reply.
    Kevin

  9. Steve replied on :

    Kevin—As you suggest, just use both imregionalmax and imregionalmin.

  10. Kevin replied on :

    Thank for your advice, Steve. It’s really informative.

  11. warith replied on :

    Hi there,

    I have a 3D matrix and I want to find the local extrema of it. I mean the coordinates of points that are bigger/smaller than its 26 neighboors.

    Can anybody help me ?

    Thanks in advance !

  12. warith replied on :

    sorry for the previous question I did not read the help carefully.

    Thank you

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

These postings are the author's and don't necessarily represent the opinions of The MathWorks.