%============================================================================================================
%author:王同乐
%date:2016/10/30
img=imread('lena.png');
img=rgb2gray(img);
img=im2double(img);%输入图像类型为uint8,将其转换为double类型进行运算
%==========================一阶微分算子提取边缘
subplot(1,2,1);imshow(img);title('原图');
subplot(1,2,2);imshow(sharp_operator(img,3));title('提取边缘后的图像');%只能是奇数算子
%算子===============================
%一阶微分算子边缘提取
function d=sharp_operator(x,n)
d=x;
operator=zeros(n);
operator(1+(n-1)/2,1+(n-1)/2)=1;
operator(1+(n-1)/2,1+(n-1)/2+1)=-1;
[width,height]=size(x);
for ii=1:width-(n-1)
for jj=1:height-(n-1)
tmp=x(ii:ii+n-1,jj:jj+n-1).*operator;
p=sum(sum(tmp));
d(ii+(n-1)/2,jj+(n-1)/2)=p;
end
end
function d=sharp_operator(x,n)
d=x;
operator=zeros(n);
operator(1+(n-1)/2,1+(n-1)/2)=1;
operator(1+(n-1)/2,1+(n-1)/2+1)=-1;
[width,height]=size(x);
for ii=1:width-(n-1)
for jj=1:height-(n-1)
tmp=x(ii:ii+n-1,jj:jj+n-1).*operator;
p=sum(sum(tmp));
d(ii+(n-1)/2,jj+(n-1)/2)=p;
end
end
%效果图===================