【摘要】 要求:
运用Roberts算子、Prewitt算子、Sobel算子、LoG算子进行边缘提取分别显示边缘提取后的水平特征和垂直特征使用欧几里得,棋盘距离进行模梯度的计算使用[-2 -1 0;-1 0 1;0 1 2]模板,显示45度方向边缘特征提取效果
代码:
roberts prewitt sobel 算子通用
f=imread(‘lena.png’);
% f…
要求:
- 运用Roberts算子、Prewitt算子、Sobel算子、LoG算子进行边缘提取
- 分别显示边缘提取后的水平特征和垂直特征
- 使用欧几里得,棋盘距离进行模梯度的计算
- 使用[-2 -1 0;-1 0 1;0 1 2]模板,显示45度方向边缘特征提取效果
代码:
- roberts prewitt sobel 算子通用
f=imread('lena.png');
% f=imnoise(f,'gaussian',0.2);%均值为0 方差为0.2
subplot(4,2,1),imshow(f),title('原图')
%roberts算子边缘提取
[gv,t1]=edge(f,'roberts','vertical');%使用edge函数对图像f提取水平的边缘
new_gv=~gv;%取反 将黑底白边转为白低黑边
subplot(4,2,2),imshow(new_gv),title('水平特征')
[gh,t2]=edge(f,'roberts','horizontal');%使用edge函数对图像f提取垂直的边缘
new_gh=~gh;%取反 将黑底白边转为白低黑边
subplot(4,2,3),imshow(new_gh),title('垂直特征')
%梯度模计算
f=double(f);
[fx,fy]=gradient(f);%基础梯度 (fx1/x1-fx2/x2)/2
subplot(4,2,4),imshow(fx),title('x梯度');
subplot(4,2,5),imshow(fy),title('y梯度');
ojld_distance=(fx^2+fy^2)^0.5;%欧几里得距离
subplot(4,2,6),imshow(real(ojld_distance)),title('欧几里得距离');
qp_distance=abs(fx)+abs(fy);%棋盘距离
subplot(4,2,7),imshow(real(qp_distance)),title('棋盘距离');
%45度边缘提取
w45=[-2 -1 0;-1 0 1;0 1 2];%指定模版
g45=imfilter(double(f),w45,'replicate');%使用imfilter计算45度方向的边缘
T=0.3*max(abs(g45(:)));%设定阈值
g45=g45>=T;%进行阈值处理
subplot(4,2,8),imshow(~g45),title('45度方向');
- LoG算子
% 原图
f=imread('lena.png');
subplot(4,4,1),imshow(f),title('原图');
LoG=edge(f,'log',0.005);
subplot(4,4,2),imshow(~LoG),title('LoG边缘 0.005');
LoG=edge(f,'log',0.01);
subplot(4,4,3),imshow(~LoG),title('LoG边缘 0.01');
LoG=edge(f,'log',0.02);
subplot(4,4,4),imshow(~LoG),title('LoG边缘 0.02');
% 加噪声 均值为0 方差为0.2
f=imnoise(f,'gaussian',0.2);
subplot(4,4,5),imshow(f),title('加噪声图 方差0.2');
LoG=edge(f,'log',0.005);
subplot(4,4,6),imshow(~LoG),title('LoG边缘 0.005');
LoG=edge(f,'log',0.01);
subplot(4,4,7),imshow(~LoG),title('LoG边缘 0.01');
LoG=edge(f,'log',0.02);
subplot(4,4,8),imshow(~LoG),title('LoG边缘 0.02');
% 加噪声 均值为0 方差为0.1
f=imnoise(f,'gaussian',0.1);
subplot(4,4,9),imshow(f),title('加噪声图 方差0.1');
LoG=edge(f,'log',0.005);
subplot(4,4,10),imshow(~LoG),title('LoG边缘 0.005');
LoG=edge(f,'log',0.01);
subplot(4,4,11),imshow(~LoG),title('LoG边缘 0.01');
LoG=edge(f,'log',0.02);
subplot(4,4,12),imshow(~LoG),title('LoG边缘 0.02');
% 加噪声 均值为0 方差为0.001
f=imnoise(f,'gaussian',0.01);
subplot(4,4,13),imshow(f),title('加噪声图 方差0.001');
LoG=edge(f,'log',0.005);
subplot(4,4,14),imshow(~LoG),title('LoG边缘 0.005');
LoG=edge(f,'log',0.01);
subplot(4,4,15),imshow(~LoG),title('LoG边缘 0.01');
LoG=edge(f,'log',0.02);
subplot(4,4,16),imshow(~LoG),title('LoG边缘 0.02');
- 不同算子的代码只有部分不同
%roberts算子边缘提取
[gv,t1]=edge(f,'roberts','vertical');%使用edge函数对图像f提取水平的边缘
%prewitt算子边缘提取
[gv,t1]=edge(f,'prewitt','vertical');%使用edge函数对图像f提取水平的边缘
%sobel算子边缘提取
[gv,t1]=edge(f,'sobel','vertical');%使用edge函数对图像f提取水平的边缘
%LoG算子边缘提取
LoG=edge(f,'log',0.005);%0.005表示边缘提取阈值 越大提取的边缘信息越少
结果:
-
Roberts
-
Prewitt
-
Sobel
-
LoG
结论:
- 边缘检测效果
LoG>Sobel>Prewitr>Roberts - 对噪声鲁棒性
LoG>Sobel>Prewitr>Roberts
算子原理:(暂空)
文章来源: blog.csdn.net,作者:Your_Julia,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Dedication_/article/details/116719453
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END