数字图像处理第三章 sobel算子

2019-04-15 13:05发布

function [ edge ] = SobelOperator( pic ) %%sobel edge = uint8(zeros(size(pic))); h = size(pic, 1); w = size(pic, 2); gx = [-1, -2, -1; 0, 0, 0; 1, 2, 1];%sobel算子水平梯度模板3*3,中间权重大,prewitt算子是中间2变为1 gy = gx'; for i = 2 : h - 1 for j = 2 : w - 1 sub = double(pic(i - 1 : i + 1, j - 1 : j + 1)); g1 = abs(sum(sum(sub .* gx)));%x,y两维,两个sum g2 = abs(sum(sum(sub .* gy))); if g1 > g2 edge(i, j) = uint8(g1); else edge(i, j) = uint8(g2); end end end end Robert function [ edge ] = RobertsOperator( pic ) %%robet算子求微分 edge = uint8(zeros(size(pic))); h = size(pic, 1); w = size(pic, 2); for i = 1 : h - 1 for j = 1 : w - 1 edge(i, j) = uint8(abs(pic(i, j) - pic(i + 1, j + 1)) + abs(pic(i, j + 1) - pic(i + 1, j))); end end end 拉普拉斯 function [ edge ] = LaplaceOperator( pic ) edge = uint8(zeros(size(pic))); h = size(pic, 1); w = size(pic, 2); l = [0, 1, 0; 1, -4, 1; 0, 1, 0]; for i = 2 : h - 1 for j = 2 : w - 1 sub = double(pic(i - 1 : i + 1, j - 1 : j + 1)); d = sum(sum(sub .* l)); edge(i, j) = uint8(d); end end end