MATLAB: Speed up the code

3x3 neighbourhoodMATLABminima

I want to speed up the code(replace the for loop) for checking whether the pixel at the center of the 3×3 neighbourhood is a minima or not. Can anyone help me with that.
function [critical_map] = Make_Critical_Map(gray_image)
[rows cols] = size(gray_image);
critical_map = zeros(rows,cols);
for i = 1:rows
for j = 1:cols
test = true;
%Check whether the gray value is lowest in the 3x3 neighbourhood
for row_inc = -1:1
if(test == true)
for col_inc = -1:1
if(test == true)
if ((i+row_inc)>=1) && ((i+row_inc)<=rows) && ((j+col_inc)>=1) && ((j+col_inc)<=cols) && (gray_image(i+row_inc,j+col_inc)<gray_image(i,j))
test = false;
end
end
end
end
end
critical_map(i,j) = test;
end
end

Best Answer

Hi,
do you have access to the Image Processing toolbox? If so, you can use the imregionalmin function:
A = randint(10,10,[0 255])
BW = imregionalmin(A,ones(3))