图像梯度处理MATLAB代码

clear all
clc
I=imread('kids.tif')
[M,N]=size(I);
B1=[-1 -1 -1;0 0 0;1 1 1];//梯度处理水平方向特征明显
B2=[-1 0 1;-1 0 -1;-1 0 -1];//梯度处理竖直方向特征明显
I1=I;
I2=I;
temp=zeros(3,3);//采用3x3的模板进行处理
for i=2:M-1
    for j=2:N-1
        temp=I(i-1:i+1,j-1:j+1);
        temp1=double(temp).*B1;
        temp2=double(temp).*B2;
        t=sum(temp1(:));
        p=sum(temp2(:));
        I1(i,j)=abs(t);
        I2(i,j)=abs(p);
    end
end
I4=I1+I2
//显示部分
subplot(221);imshow(I);
subplot(222);imshow(I1);
subplot(223);imshow(I2);
subplot(224);imshow(I4);

猜你喜欢

转载自blog.csdn.net/qq_24163555/article/details/84110268