clear all;
clc;
image=imread('2222.bmp');
subplot(1,2,1);
imshow(image);
[m n]=size(image);
i_fit=zeros(m,n);%初始化边缘检测结果数据存储矩阵
i_tran=zeros(m,n);%过渡判断矩阵
thre=100; %阈值
x=[-1 -2 -1;0 0 0;1 2 1]; %sobel算子模板x方向
y=[-1 0 1;-2 0 2;-1 0 1];%sobel算子模板y方向
for i=2:m-1%开始对行列进行下面扫描卷积
for j=2:n-1
i_temp=[ image(i-1,j-1) image(i-1,j) image(i-1,j+1);image(i,j-1) image(i,j) image(i,j+1);...
image(i+1,j-1) image(i+1,j) image(i+1,j+1)];
x_temp=double(i_temp).*x;
y_temp=double(i_temp).*y;
gx=sum(sum(x_temp));
gy=sum(sum(y_temp));
i_tran(i,j)=sqrt(gx^2+gy^2); %得到相应的微分值
if i_tran(i,j)>=thre; %进行判断是否大于该阈值
i_fit(i,j)=1; %大于该阈值记录为0
end
end
end
subplot(1,2,2);
imshow(i_fit);