利用Matlab编写Sobel算子边缘检测函数,计算公式参考姚敏《数字图像处理 第三版》
function SobelSo = MySobel( I , T )
% MySobel函数用来应用一阶算子检测边缘
% I为图像,T为阈值
[height,width] = size(I); % 获得图像的高度和宽度
Z = double(I); % sqrt函数的参数需double类型
SobelSo = I;
for i = 2:height - 1
for j = 2:width - 1
Gx = (Z(i+1,j-1) + 2*Z(i+1,j) + Z(i+1,j+1)) - (Z(i-1,j-1) + 2*Z(i-1,j) + Z(i-1,j+1));
Gy = (Z(i-1,j+1) + 2*Z(i,j+1) + Z(i+1,j+1)) - (Z(i-1,j-1) + 2*Z(i,j-1) + Z(i+1,j-1));
if(sqrt(Gx^2 + Gy^2) < T)
SobelSo(i,j) = 0;
else
SobelSo(i,j) = sqrt(Gx^2 + Gy^2);
end
end
end
end