www.gusucode.com > 图像变换及频域滤波 空间域图像增强 图像边缘检测源码程序 > digitalimageprocessprograms/jiance.m

    clc;
fid=fopen('d:/img/lena.img','r');
f=fread(fid,[256,256],'uchar');
GR=f;
GP=f;
GS=f;
for x=2:255
    for y=2:255
        GRx(x,y)=f(x,y)-f(x+1,y+1);
        GRy(x,y)=f(x,y+1)-f(x+1,y);
        GR(x,y)=sqrt((GRx(x,y)).^2+(GRy(x,y)).^2);
        GPx(x,y)=(f(x-1,y+1)+f(x,y+1)+f(x+1,y+1))/3-(f(x-1,y-1)+f(x,y-1)+f(x+1,y-1))/3;
        GPy(x,y)=(f(x-1,y-1)+f(x-1,y)+f(x-1,y+1))/3-(f(x+1,y-1)+f(x+1,y)+f(x+1,y+1))/3;
        GP(x,y)=sqrt((GPx(x,y)).^2+(GPy(x,y)).^2);
        GSx(x,y)=(f(x-1,y+1)+2*f(x,y+1)+f(x+1,y+1))/3-(f(x-1,y-1)+2*f(x,y-1)+f(x+1,y-1))/3;
        GSy(x,y)=(f(x-1,y-1)+2*f(x-1,y)+f(x-1,y+1))/3-(f(x+1,y-1)+2*f(x+1,y)+f(x+1,y+1))/3;
        GS(x,y)=sqrt((GSx(x,y)).^2+(GSy(x,y)).^2);
    end
end
subplot(2,2,1);
imshow(f,[0,255]);title('原图像');
subplot(2,2,2);
imshow(GR,[0,255]);title('Roberts');
subplot(2,2,3);
imshow(GP,[0,255]);title('Prewitt');
subplot(2,2,4);
imshow(GS,[0,255]);title('Sobel');