clear all ;
F1 = imread(’test256. bmp’) ;
F = double (F1) ;
[ a ,b ] = size (F) ;
SE = [1 1 1 1 1 ;1 1 1 1 1 ;1 1 1 1 1 ] 3 180
[ c ,d ] = size (SE) ;
e = (c + 1) / 2 ;
f = (d + 1) / 2 ;
hardcore = [1 3 ;2 1 ;2 2 ;2 3 ;2 4 ;2 5 ;3 3 ] ;
k = 2 ;
R = zeros (a ,b) ;
for x = 1 :a
for y = 1 :b
sort number = 0 ;
sort numberh = 0 ;
for i = 1 :c
m = i - e ;
for j = 1 :d
n = j - f ;
if (x + m) > = 1 & (x + m) < = a & (y
+ n) > = 1 &(y + n) < = b
sort number = sort number + 1 ;
D(sort number) = F ( (x + m) , (y + n) ) + SE(i ,
j) ;
if all ( [ i ,j ] = = hardcore (1 , :) ) | all ( [ i ,j ] = =
hardcore (2 , :) )
sort numberh = sort numberh + 1 ;
H(sort numberh) = D(sort number) ;
end
end
end % now the difference set is completed
end
number calculation = max(size (D) ) ;
number k = max(size (H) ) ;
T = zeros (1 ,number calculation + (k - 1) 3 num2
ber k) ;
for repeat number = 1 :k - 1
T( (repeat number - 1) 3 number k + 1 :repeat
number 3 number k) = H;
end
T( (k - 1) 3 number k + 1 :max ( size (T) ) ) =
D ;
T = sort (T) ; %sort the difference set
R (x ,y) = T(max ( size ( T) ) - k + 1) ; %the
kth smallest number
if R(x ,y) > 255
R(x ,y) = 255 ;
end
D = 0 ; %2003. 6. 2
H = 0 ;
end
end
参考资料:楼上