Jak mogę znaleźć lokalne maksima w obrazie w MATLAB?

Mam obraz w MATLABIE:

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

i chcę na nim trochę przetworzyć:

pic = some_processing(y);

i znajdź lokalne maksima wyjścia. To znaczy wszystkie punkty wy które są większe niż wszyscy ich sąsiedzi.

Nie mogę znaleźć funkcji MATLAB, aby to zrobić dobrze. Najlepsze, co mogę wymyślić, to:

[dim_y,dim_x]=size(pic);
enlarged_pic=[zeros(1,dim_x+2);
              zeros(dim_y,1),pic,zeros(dim_y,1);
              zeros(1,dim_x+2)];

% now build a 3D array
% each plane will be the enlarged picture
% moved up,down,left or right,
% to all the diagonals, or not at all

[en_dim_y,en_dim_x]=size(enlarged_pic);

three_d(:,:,1)=enlarged_pic;
three_d(:,:,2)=[enlarged_pic(2:end,:);zeros(1,en_dim_x)];
three_d(:,:,3)=[zeros(1,en_dim_x);enlarged_pic(1:end-1,:)];
three_d(:,:,4)=[zeros(en_dim_y,1),enlarged_pic(:,1:end-1)];
three_d(:,:,5)=[enlarged_pic(:,2:end),zeros(en_dim_y,1)];
three_d(:,:,6)=[pic,zeros(dim_y,2);zeros(2,en_dim_x)];
three_d(:,:,7)=[zeros(2,en_dim_x);pic,zeros(dim_y,2)];
three_d(:,:,8)=[zeros(dim_y,2),pic;zeros(2,en_dim_x)];
three_d(:,:,9)=[zeros(2,en_dim_x);zeros(dim_y,2),pic];

A następnie sprawdź, czy maksimum wzdłuż trzeciego wymiaru pojawia się w pierwszej warstwie (czyli:three_d(:,:,1)):

(max_val, max_i) = max(three_d, 3);
result = find(max_i == 1);

Czy jest na to bardziej elegancki sposób? Wydaje się to trochę dziwne.

questionAnswers(5)

yourAnswerToTheQuestion