map1=imread('BMW.jpg');
[m,n,l]=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('原始灰度图像')
B = zeros(m,n);
A = I;
for i = 2:m-1
for j = 2:n-1
sx=-1*A(i-1,j-1)-2*A(i,j-1)-1*A(i+1,j-1)...
+1*A(i-1,j+1)+2*A(i,j+1)+1*A(i+1,j+1);
sy=-1*A(i-1,j-1)-2*A(i-1,j)-1*A(i-1,j+1)...
+1*A(i+1,j-1)+2*A(i+1,j)+1*A(i+1,j+1);
B(i,j) = max(sx,sy)/4;
end
end
figure(2)
imshow(B,[])
title('Sobel算法处理后')