《边缘提取和边缘检测matlab程序代码大全(共11页).doc》由会员分享,可在线阅读,更多相关《边缘提取和边缘检测matlab程序代码大全(共11页).doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上附 录Part1:对cameraman原始图像处理的仿真程序:clcclear allclose allA = imread(cameraman.bmp); % 读入图像subplot(2,4,1);imshow(A);title(原图);x_mask = 1 0;0 -1; % 建立X方向的模板y_mask = rot90(x_mask); % 建立Y方向的模板I = im2double(A); % 将图像数据转化为双精度dx = imfilter(I, x_mask); % 计算X方向的梯度分量dy = imfilter(I, y_mask); % 计算Y方向的梯
2、度分量grad = sqrt(dx.*dx + dy.*dy); % 计算梯度grad = mat2gray(grad); % 将梯度矩阵转换为灰度图像level = graythresh(grad); % 计算灰度阈值BW = im2bw(grad,level); % 用阈值分割梯度图像subplot(2,4,2);imshow(BW); % 显示分割后的图像即边缘图像title(Roberts);y_mask = -1 -2 -1;0 0 0;1 2 1; x_mask = y_mask; I = im2double(A); dx = imfilter(I, x_mask); dy = i
3、mfilter(I, y_mask); grad = sqrt(dx.*dx + dy.*dy); % 计算梯度grad = mat2gray(grad); level = graythresh(grad); BW = im2bw(grad,level); subplot(2,4,3);imshow(BW); % 显示分割后的图像即边缘图像title(Sobel);y_mask = -1 -1 -1;0 0 0;1 1 1; x_mask = y_mask; dx = imfilter(I, x_mask); dy = imfilter(I, y_mask); grad = sqrt(dx.*
4、dx + dy.*dy); % 计算梯度grad = mat2gray(grad); level = graythresh(grad); BW = im2bw(grad,level); % 用阈值分割梯度图像subplot(2,4,4);imshow(BW); % 显示分割后的图像即边缘图像title(Prewitt);mask=0,-1,0;-1,4,-1;0,-1,0; % 建立模板dx = imfilter(I, mask); % 计算梯度矩阵grad = mat2gray(dx); % 将梯度矩阵转化为灰度图像BW = im2bw(grad,0.58); % 用阈值分割梯度图像subp
5、lot(2,4,5);imshow(BW); % 显示分割后的图像,即梯度图像title(Laplacian);mask=0,0,-1,0,0;0,-1,-2,-1,0;-1,-2,16,-2,-1;0,-1,-2,-1,0;0,0,-1,0,0; % 建立模板dx = imfilter(I, mask); % 计算梯度矩阵grad = mat2gray(dx); % 将梯度矩阵转化为灰度图像BW = im2bw(grad,0.58); subplot(2,4,6);imshow(BW); % 显示分割后的图像,即梯度图像title(log);BW1 = edge(I,canny); % 调用
6、canny函数subplot(2,4,7);imshow(BW1); % 显示分割后的图像,即梯度图像title(Canny);mask1=-1 -2 -1;0 0 0;1 2 1; % 建立方向模板mask2=-2 -1 0;-1 0 1;0 1 2;mask3=-1 0 1;-2 0 2;-1 0 1;mask4=0 1 2;-1 0 1;-2 -1 0;mask5=1 2 1;0 0 0;-1 -2 -1;mask6=2 1 0;1 0 -1;0 -1 -2;mask7=1 0 -1;2 0 -2;1 0 -1; mask8=0 -1 -2;1 0 -1;2 1 0; d1 = imfi
7、lter(I, mask1); % 计算8个领域的灰度变化d2 = imfilter(I, mask2);d3 = imfilter(I, mask3);d4 = imfilter(I, mask4);d5 = imfilter(I, mask5);d6 = imfilter(I, mask6);d7 = imfilter(I, mask7);d8 = imfilter(I, mask8);dd = max(abs(d1),abs(d2); % 取差值变化最大的元素组成灰度变化矩阵dd = max(dd,abs(d3);dd = max(dd,abs(d4);dd = max(dd,abs(d
8、5);dd = max(dd,abs(d6);dd = max(dd,abs(d7);dd = max(dd,abs(d8);grad = mat2gray(dd); % 将灰度变化矩阵转化为灰度图像BB = grad;FW=median(BB(:)/0.6745;B = BB.*BB;B = sum(B(:);FX= sqrt(B/2562);FS=sqrt(max(FX2-FW2,0);T=sqrt(2)*FW2/FS; % 计算最佳阈值grad = mat2gray(BB); BW2=im2bw(grad,T); % 用最佳阈值分割梯度图像subplot(2,4,8);imshow(BW
9、2); % 显示分割后的图像,即边缘图像title(sobel改进算子);Part2加入高斯噪声后的cameraman仿真程序:clcclear allclose allA = imread(cameraman.bmp); % 读入图像V=0.009;X=imnoise(A,gaussian,0,V);subplot(2,4,1);imshow(X);%添加均值为0、方差0.09的高斯噪声x_mask = 1 0;0 -1; % 创建X方向的模板y_mask = rot90(x_mask); % 创建Y方向的模板I = im2double(X); % 图像数据双精度转化dx = imfilte
10、r(I, x_mask); % X方向的梯度分量的计算dy = imfilter(I, y_mask); % Y方向的梯度分量的计算grad = sqrt(dx.*dx + dy.*dy); % 梯度计算grad = mat2gray(grad); % 梯度矩阵转换成灰度图像level = graythresh(grad); % 计算灰度阈值BW = im2bw(grad,level); % 使用阈值分割梯度图像subplot(2,4,2);imshow(BW); % 显示分割后的图像即边缘图像title(Roberts);y_mask = -1 -2 -1;0 0 0;1 2 1; x_ma
11、sk = y_mask; dx = imfilter(I, x_mask); dy = imfilter(I, y_mask); grad = sqrt(dx.*dx + dy.*dy); grad = mat2gray(grad); % 梯度矩阵转为灰度图像level = graythresh(grad); % 计算灰度阈值BW = im2bw(grad,level); % 用阈值分割梯度图像subplot(2,4,3);imshow(BW); % 显示分割后的图像即边缘图像title(Sobel);y_mask = -1 -1 -1;0 0 0;1 1 1; x_mask = y_mask
12、; dx = imfilter(I, x_mask); dy = imfilter(I, y_mask); grad = sqrt(dx.*dx + dy.*dy); grad = mat2gray(grad); level = graythresh(grad); BW = im2bw(grad,level); subplot(2,4,4);imshow(BW); % 显示分割后的图像即边缘图像title(Prewitt);mask=0,-1,0;-1,4,-1;0,-1,0; % 建立模板dx = imfilter(I, mask); % 计算梯度矩阵grad = mat2gray(dx);
13、 % 将梯度矩阵转化为灰度图像BW = im2bw(grad,0.58); % 用阈值分割梯度图像subplot(2,4,5);imshow(BW); % 显示分割后的图像,即梯度图像title(Laplacian);mask=0,0,-1,0,0;0,-1,-2,-1,0;-1,-2,16,-2,-1;0,-1,-2,-1,0;0,0,-1,0,0; % 建立模板dx = imfilter(I, mask); % 计算梯度矩阵grad = mat2gray(dx); % 将梯度矩阵转化为灰度图像BW = im2bw(grad,0.58); % 用阈值分割梯度图像subplot(2,4,6);
14、imshow(BW); % 显示分割后的图像,即梯度图像title(log);BW1 = edge(I,canny); % 调用canny函数subplot(2,4,7);imshow(BW1); % 显示分割后的图像,即梯度图像title(Canny);mask1=-1 -2 -1;0 0 0;1 2 1; % 建立方向模板mask2=-2 -1 0;-1 0 1;0 1 2;mask3=-1 0 1;-2 0 2;-1 0 1;mask4=0 1 2;-1 0 1;-2 -1 0;mask5=1 2 1;0 0 0;-1 -2 -1;mask6=2 1 0;1 0 -1;0 -1 -2;m
15、ask7=1 0 -1;2 0 -2;1 0 -1; mask8=0 -1 -2;1 0 -1;2 1 0; d1 = imfilter(I, mask1); % 计算8个领域的灰度变化d2 = imfilter(I, mask2);d3 = imfilter(I, mask3);d4 = imfilter(I, mask4);d5 = imfilter(I, mask5);d6 = imfilter(I, mask6);d7 = imfilter(I, mask7);d8 = imfilter(I, mask8);dd = max(abs(d1),abs(d2); % 取差值变化最大的元素组
16、成灰度变化矩阵dd = max(dd,abs(d3);dd = max(dd,abs(d4);dd = max(dd,abs(d5);dd = max(dd,abs(d6);dd = max(dd,abs(d7);dd = max(dd,abs(d8);grad = mat2gray(dd); % 将灰度变化矩阵转化为灰度图像BB = grad;FW=median(BB(:)/0.6745;B = BB.*BB;B = sum(B(:);FX= sqrt(B/2562);FS=sqrt(max(FX2-FW2,0);T=sqrt(2)*FW2/FS; % 计算最佳阈值grad = mat2gr
17、ay(BB); % 将梯度矩阵转化为灰度图像BW2=im2bw(grad,T); % 用最佳阈值分割梯度图像subplot(2,4,8);imshow(BW2); % 显示分割后的图像,即边缘图像title(sobel改进算子);加入椒盐噪声的边缘检测程序:function jingdianI=imread(lenna.bmp);I1=imnoise(I,salt & pepper);%添加椒盐噪声,默认值为0.02figure,imshow(I1);%添加均值为0、方差0.002的高斯噪声title(添加椒盐噪声后原图)B1=edge(I1,roberts);B2=edge(I1,sobel
18、);B3=edge(I1,prewitt);B4=edge(I1,canny);B5=edge(I1,log);subplot(2,3,1);imshow(B1);title(roberts算子检测);subplot(2,3,2);imshow(B2);title(sobel算子检测);subplot(2,3,3);imshow(B3);title(prewitt算子检测);subplot(2,3,4);imshow(B4);title(canny算子检测);subplot(2,3,5)imshow(B5);title(log算子检测);B1=edge(I1,roberts);%调用rober
19、ts算子检测图像B2=edge(I1,sobel);%调用soble算子进行边缘检测B3=edge(I1,prewitt);%调用prewitt算子进行边缘检测B4=edge(I1,canny);%调用canny算子对图像进行边缘检测B5=edge(I1,log);%调用log算子对图像进行边缘检测subplot(2,3,1);%设置图像布局imshow(B1);title(roberts算子检测);%现实图像并命名为roberts算子检测subplot(2,3,2);imshow(B2);title(sobel算子检测);subplot(2,3,3);imshow(B3);title(pre
20、witt算子检测);subplot(2,3,4);imshow(B4);title(canny算子检测);subplot(2,3,5)imshow(B5);title(log算子检测);mask1=-1 -2 -1;0 0 0;1 2 1; % 建立方向模板mask2=-2 -1 0;-1 0 1;0 1 2;mask3=-1 0 1;-2 0 2;-1 0 1;mask4=0 1 2;-1 0 1;-2 -1 0;mask5=1 2 1;0 0 0;-1 -2 -1;mask6=2 1 0;1 0 -1;0 -1 -2;mask7=1 0 -1;2 0 -2;1 0 -1; mask8=0
21、-1 -2;1 0 -1;2 1 0; I = im2double(I1); % 将数据图像转化为双精度d1 = imfilter(I, mask1); % 计算8个领域的灰度变化d2 = imfilter(I, mask2);d3 = imfilter(I, mask3);d4 = imfilter(I, mask4);d5 = imfilter(I, mask5);d6 = imfilter(I, mask6);d7 = imfilter(I, mask7);d8 = imfilter(I, mask8);dd = max(abs(d1),abs(d2); % 取差值变化最大的元素组成灰度
22、变化矩阵dd = max(dd,abs(d3);dd = max(dd,abs(d4);dd = max(dd,abs(d5);dd = max(dd,abs(d6);dd = max(dd,abs(d7);dd = max(dd,abs(d8);grad = mat2gray(dd); % 将灰度变化矩阵转化为灰度图像level = graythresh(grad); % 计算灰度阈值BW = im2bw(grad,level); % 用阈值分割梯度图像BB = grad;FW=median(BB(:)/0.6745;B = BB.*BB;B = sum(B(:);FX= sqrt(B/2562);FS=sqrt(max(FX2-FW2,0);T=sqrt(2)*FW2/FS; % 计算最佳阈值grad = mat2gray(BB); % 将梯度矩阵转化为灰度图像BW2=im2bw(grad,T); % 用最佳阈值分割梯度图像subplot(2,3,6);imshow(BW2); % 显示分割后的图像,即边缘图像title(加入椒盐噪声的sobel改进算子);专心-专注-专业