B = A( (i - 1 : i + 1) , (i-1 : i + 1) )
Consider a 3 x 3 matrix A:
A = [11 22 33 ;
44 55 66 ;
77 88 99 ];
The following command will get the sub-matrix made up of the first two rows and columns of A and store that sub-matrix in B.
B = A( 1:2 , 1:2 )
B =
11 22
44 55
The following code is an example implementation tracking the number of times the number of windows evaluated before the success condition is met.
img = double( imread( 'trees.tif') );
[ nRows , nCols ] = size( img );
result = zeros( nRows , nCols );
halfWinSize = 2;
threshold = 50;
for rI = 1:nRows
for cI = 1:nCols
rILo = rI - halfWinSize;
rIHi = rI + halfWinSize;
cILo = cI - halfWinSize;
cIHi = cI + halfWinSize;
flagConditionMet = false ;
count = 1;
while( ~ flagConditionMet )
if( rILo >= 1 && rIHi <= nRows && cILo >= 1 && cIHi <= nCols )
imgWindow = img( (rILo : rIHi) , (cILo : cIHi) );
minVal = min( imgWindow(:) );
flagConditionMet = minVal < threshold;
if( ~ flagConditionMet )
rILo = rILo - 1;
rIHi = rIHi + 1;
cILo = cILo - 1;
cIHi = cIHi + 1;
count = count + 1;
else
result( rI , cI ) = count;
end
else
break;
end
end
end
end
figure;
imshow(result./max( result(:) ))
Best Answer