MATLAB: Find local maxima of a matrix and the neighboring elements

imregional()matrixneighboring elements

Hi! I have a 2D matrix A and I used
B=imregionalmax(A)
Now the matrix B contains 1 for all identified maximas. But I want to include also the surrounding elements and get the position (i,j) of the elements. How do I do this?
Thanks a lot!!

Best Answer

  • Include the surrounding elements: imdilate your regionalmax binary image by the relevant structuring element. If you just want the orthogonal neighbours:
C = imdilate(B, [0 1 0;1 1 1;0 1 0]) %or imdilate(B, strel('diamond', 1))
If you also want the diagonal neighbours:
C = imdilate(B, ones(3) %or imdilate(B, strel('square', 3))
  • Get the position (i,j) of the elements:
[row, column] = find(C)
Related Question