Search code examples
matlabcluster-analysispixel

Assigning pixels to their local maximum matlab


I need to assign pixels to their local maximum in matlab. I can easily find the local maxima in matlab using imregional max or other derivatives such as extrema2. However, I also want to cluster the pixels based on these local maxima.

For example, if I have a peak then I also want to know if the pixels surrounding this peaks are connected to the maxima. I cant think of any efficient ways of doing this and any help or advice would be much appreciated.

Thank You


Solution

  • Basically you could limit the question to "does the pixel belong to the previous local maximum or to the next one?" and then look at the derivatives (gradients) of the pixels: if negative, it belongs to the previous, if positive it belongs to the next one :)

    Following image

    enter image description here

    with maxima Max1=[17,59] and Max2=[83,59]

    and the idea I mentioned for the dimension X:

    Pixel=[randi(100),randi(100)];
    
    [XG,YG]=imgradientxy(Image);
    
    Maxima=[Max1;Max2];
    
    ii=1;
    while Pixel(1)>Maxima(ii,1);
        ii=ii+1;
    end
    
    if XG(Pixel)>0
        LocalMax=Maxima(ii,:);
    else
        LocalMax=Maxima(ii-1,:);
    end
    
    imshow(Image);hold on
    
    plot(Pixel(1),Pixel(2),'r.');
    plot(LocalMax(1),LocalMax(2),'gx');
    

    will (sometimes) give you results like this:enter image description here

    It's just a sketch of my idea, not robust by anymeans, and probably with a couple of erros, so don't go copy-pasting it. You still have the homework to implement that for the 2 dimensional scenario ;)