Find local maxima of a matrix and the neighboring elements

2 次查看(过去 30 天)
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!!

采纳的回答

Guillaume
Guillaume 2017-4-5
  • 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)

更多回答(1 个)

KSSV
KSSV 2017-4-5
If (i,j) is the position and you want it's surrounding element positions, you can use:
[in,jn] = meshgrid(i-1:i+1,j-1:j+1) ;
You have to take care of edges. And in above in, jn includes (i,j) too.
Alternatively you can use knnsearch also.
  4 个评论
Antonio Sereira
Antonio Sereira 2017-4-5
Hi, I used
[M,I]=max(B)
with
B=imregionalmax(A)
Now I is a row vector 1x60 (B was 100x60) where i correspond to the column of B and the number in cell i correspond to the j value of B. As I said before, B contains logical values, i.e. 1 for local maxima and zero else. Now I know the indices (i,j) of the local maxima. I want to create a new Matrix C (100x60) with logical numbers but the zeros surrounding 1 in B should be turned to 1, too.
How do I do this?

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by