map1=imread('BMW.jpg');
[m,n,dep]=size(map1); %行,列,深度值
I=zeros(m,n);
%I=uint8(I);
%灰度化
for i=1:m
for j=1:n
I(i,j)=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);
end
end
%灰度化完成,输出灰度图
figure(1)
imshow(I,[])
title('原始灰度图像')
tic
B = zeros(m,n);
s = zeros(8,1);
t = zeros(8,1);
A = I;
for i = 2:m-1
for j = 2:n-1
temp = sum(sum(A([i-1:i+1],[j-1:j+1])))-A(i,j);
s(1) = A(i-1,j-1)+A(i-1,j)+A(i-1,j+1);
s(2) = A(i-1,j)+A(i-1,j+1)+A(i,j+1);
s(3) = A(i-1,j+1)+A(i,j+1)+A(i+1,j+1);
s(4) = A(i,j+1)+A(i+1,j+1)+A(i+1,j);
s(5) = A(i+1,j+1)+A(i+1,j)+A(i+1,j-1);
s(6) = A(i+1,j)+A(i+1,j-1)+A(i,j-1);
s(7) = A(i+1,j-1)+A(i,j-1)+A(i-1,j-1);
s(8) = A(i,j-1)+A(i-1,j-1)+A(i-1,j);
for k = 1:8
t(k) = temp-s(k);
end
B(i,j) = max(max(abs(5*s-3*t)))/15;
end
end
figure(2)
imshow(B,[])
title('Kirsch算法处理后')
toc %统计一下程序运行时间,可以见证这个算法速度较慢