How can I find local maxima in an image in MATLAB?

后端 未结 5 1603
无人及你
无人及你 2020-12-08 00:54

I have an image in MATLAB:

y = rgb2gray(imread(\'some_image_file.jpg\'));

and I want to do some processing on it:

pic = som         


        
相关标签:
5条回答
  • 2020-12-08 01:41

    In addition to imdilate, which is in the Image Processing Toolbox, you can also use ordfilt2.

    ordfilt2 sorts values in local neighborhoods and picks the n-th value. (The MathWorks example demonstrates how to implemented a max filter.) You can also implement a 3x3 peak finder with ordfilt2 with the following logic:

    1. Define a 3x3 domain that does not include the center pixel (8 pixels).

      >> mask = ones(3); mask(5) = 0 % 3x3 max
      mask =
           1     1     1
           1     0     1
           1     1     1
      
    2. Select the largest (8th) value with ordfilt2.

      >> B = ordfilt2(A,8,mask)
      B =
           3     3     3     3     3     4     4     4
           3     5     5     5     4     4     4     4
           3     5     3     5     4     4     4     4
           3     5     5     5     4     6     6     6
           3     3     3     3     4     6     4     6
           1     1     1     1     4     6     6     6
      
    3. Compare this output to the center value of each neighborhood (just A):

      >> peaks = A > B
      peaks =
           0     0     0     0     0     0     0     0
           0     0     0     0     0     0     0     0
           0     0     1     0     0     0     0     0
           0     0     0     0     0     0     0     0
           0     0     0     0     0     0     1     0
           0     0     0     0     0     0     0     0
      
    0 讨论(0)
  • 2020-12-08 01:48

    or, just use the excellent: extrema2.m

    0 讨论(0)
  • 2020-12-08 01:56

    If you have the Image Processing Toolbox, you could use the IMREGIONALMAX function:

    BW = imregionalmax(y);
    

    The variable BW will be a logical matrix the same size as y with ones indicating the local maxima and zeroes otherwise.

    NOTE: As you point out, IMREGIONALMAX will find maxima that are greater than or equal to their neighbors. If you want to exclude neighboring maxima with the same value (i.e. find maxima that are single pixels), you could use the BWCONNCOMP function. The following should remove points in BW that have any neighbors, leaving only single pixels:

    CC = bwconncomp(BW);
    for i = 1:CC.NumObjects,
      index = CC.PixelIdxList{i};
      if (numel(index) > 1),
        BW(index) = false;
      end
    end
    
    0 讨论(0)
  • 2020-12-08 01:57
    bw = pic > imdilate(pic, [1 1 1; 1 0 1; 1 1 1]);
    
    0 讨论(0)
  • 2020-12-08 02:01

    Alternatively, you can use nlfilter and supply your own function to be applied to each neighborhood.

    This "find strict max" function would simply check if the center of the neighborhood is strictly greater than all the other elements in that neighborhood, which is always 3x3 for this purpose. Therefore:

    I = imread('tire.tif');
    BW = nlfilter(I, [3 3], @(x) all(x(5) > x([1:4 6:9])) );
    imshow(BW)
    
    0 讨论(0)
提交回复
热议问题