logo资料库

Matlab做图像边缘检测的多种方法(matlab代码).doc

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
1、用 Prewitt 算子检测图像的边缘 % 0.04 为梯度阈值 I = imread('bacteria.BMP'); BW1 = edge(I,'prewitt',0.04); figure(1); imshow(I); figure(2); imshow(BW1); 2、用不同σ值的 LoG 算子检测图像的边缘 I = imread('bacteria.BMP'); BW1 = edge(I,'log',0.003); % σ=2 imshow(BW1);title('σ=2') BW1 = edge(I,'log',0.003,3); % σ=3 figure, imshow(BW1);title('σ=3') 3、用 Canny 算子检测图像的边缘 I = imread('bacteria.BMP'); imshow(I); BW1 = edge(I,'canny',0.2); figure,imshow(BW1); 4、图像的阈值分割 I=imread('blood1.tif'); imhist(I); I1=im2bw(I,140/255); % im2bw 函数需要将灰度值转换到[0,1]范围内 figure,imshow(I1); % 观察灰度直方图, 灰度 140 处有谷,确定阈值 T=140 5、用水线阈值法分割图像 afm = imread('afmsurf.tif');figure, imshow(afm); se = strel('disk', 15); Itop = imtophat(afm, se); % 高帽变换 Ibot = imbothat(afm, se); % 低帽变换 figure, imshow(Itop, []); % 高帽变换,体现原始图像的灰度峰值
figure, imshow(Ibot, []); % 低帽变换,体现原始图像的灰度谷值 Ienhance = imsubtract(imadd(Itop, afm), Ibot);% 高帽图像与低帽图像相减,增强图像 figure, imshow(Ienhance); Iec = imcomplement(Ienhance); % 进一步增强图像 Iemin = imextendedmin(Iec, 20); figure,imshow(Iemin) % 搜索 Iec 中的谷值 Iimpose = imimposemin(Iec, Iemin); wat = watershed(Iimpose); % 分水岭分割 rgb = label2rgb(wat); figure, imshow(rgb); % 用不同的颜色表示分割出的不同区域 6、对矩阵进行四叉树分解 I = [ 1 1 1 1 2 3 6 2 1 1 1 1 1 4 10 20 5 6 15 25 6 7 7 8 7 7 1 1 1 20 20 20 22 1 1 1 22 22 22 22 20 22 20 20 22 20 20 20 1 5 9 13 2 6 10 14 4 8 3 7 11 15 12 16]; S = qtdecomp(I,5); full(S) 7、将图像分为文字和非文字的两个类别 I=imread('4-11.jpg'); I1=I(:,:,1); I2=I(:,:,2); I3=I(:,:,3); [y,x,z]=size(I); d1=zeros(y,x); d2=d1; myI=double(I); I0=zeros(y,x); for i=1:x for j=1:y %欧式聚类
d1(j,i)=sqrt((myI(j,i,1)-180)^2+(myI(j,i,2)-180)^2+(myI(j,i,3)-180)^2); d2(j,i)=sqrt((myI(j,i,1)-200)^2+(myI(j,i,2)-200)^2+(myI(j,i,3)-200)^2); if (d1(j,i)>=d2(j,i)) I0(j,i)=1; end end end figure(1); imshow(I); % 显示 RGB 空间的灰度直方图,确定两个聚类中心(180,180,180)和(200,200,200) figure(2); subplot(1,3,1); imhist(I1); subplot(1,3,2); imhist(I2); subplot(1,3,3); imhist(I3); figure(4); imshow(I0); 8、形态学梯度检测二值图像的边缘 % 腐蚀运算对灰度值为 1 的进行 I=imread('wrod213.bmp'); imshow(I); I=~I; figure, imshow(I); SE=strel('square',3); % 定义 3×3 腐蚀结构元素 J=imerode(~I,SE); BW=(~I)-J; figure,imshow(BW); % 检测边缘 9、形态学实例——从 PCB 图像中删除所有电流线,仅保留芯片对象 I=imread('circbw.tif'); imshow(I); SE=strel('rectangle',[40 30]); % 结构定义
J=imopen(I,SE); figure,imshow(J); % 开启运算
分享到:
收藏