基于MATLAB的图像分割技术

更新时间:2024-06-11 09:09:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

利用阀值法对图像进行分割: >> f=imread('peppers.png'); >> f=rgb2gray(f); >> f=im2double(f);

>> t=0.5*(min(f(:))+max(f(:))); >> done=false; >> while ~done g=f>=t;

tn=0.5*(mean(f(g))+mean(f(~g))); done=abs(t-tn)<0.1; t=tn; end;

>> display('Threshold(t)-Iterative'); Threshold(t)-Iterative >> t t =

0.4691

>> r=im2bw(f,t);

>> subplot(2,2,1);imshow(f); >> subplot(2,2,2);imshow(r);

>> xlabel('迭代法全局阀值分割'); >> th=graythresh(f); >> th th =

0.3961

>> s=im2bw(f,th);

>> subplot(2,2,3);imshow(s); >> xlabel('全局阀值Otsu分割'); >> se=strel('disk',10); >> ft=imtophat(f,se); >> thr=graythresh(ft); >> thr thr =

0.1098

>> lt=im2bw(ft,thr);

>> subplot(2,2,4);imshow(lt); >> xlabel('局部阀值分割');

用迭代法对图像进行分割: >> i=imread('eight.tif'); >> zmax=max(max(i)); >> zmin=min(min(i)); >> tk=(zmax+zmin)/2; >> bcal=1;

>> isize=size(i); >> while (bcal) ifg=0; ibg=0; fg=0; bg=0;

for j=1:isize(1) for k=1:isize(2) tmp=i(j,k); if(tmp>=tk) ifg=ifg+1;

fg=fg+double(tmp); else

ibg=ibg+1;

bg=bg+double(tmp); end;

end; end;

zo=fg/ifg; zb=bg/ibg;

tktmp=uint8((zo+zb)/2); if(tktmp==tk) bcal=0; else

tk=tktmp; end; end;

>> disp(strcat('迭代后阀值',num2str(tk))); 迭代后阀值165

>> newi=im2bw(i,double(tk)/255); >> subplot(1,2,1);imshow(i); >> subplot(1,2,2);imshow(newi); >> xlabel('迭代法');

用Otsu法进行阀值选择: >> i=imread('coins.png'); >> subplot(1,2,1);imshow(i);

>> bw=im2bw(i,graythresh(getimage)); >> subplot(1,2,2);imshow(bw);

使用分水岭算法对图像进行分割:

>> c1=-10; >> c2=-c1;

>> dist=sqrt(2*(2*c1)^2); >> rad=dist/2*1.4;

>> li=[floor(c1-1.2*rad) ceil(c2+1.2*rad)]; >> [x,y]=meshgrid(li(1):li(2));

>> bw1=sqrt((x-c1).^2+(y-c1).^2)<=rad; >> bw2=sqrt((x-c2).^2+(y-c2).^2)<=rad; >> bw=bw1|bw2;

>> subplot(1,3,1);imshow(bw); >> d=bwdist(~bw);

>> subplot(1,3,2);imshow(d,[]); >> d=-d;

>> d(~bw)=-Inf; >> l=watershed(d);

>> rgb=label2rgb(l,'jet',[.5 .5 .5]); >> subplot(1,3,3);imshow(rgb);

使用分水岭算法: >> c1=-10; >> c2=-c1;

>> dist=sqrt(3*(2*c1)^2); >> rad=dist/2*1.4;

>> li=[floor(c1-1.2*rad) ceil(c2+1.2*rad)]; >> [x,y,z]=meshgrid(li(1):li(2));

>> bw1=sqrt((x-c1).^2+(y-c1).^2+(z-c1).^2)<=rad; >> bw2=sqrt((x-c2).^2+(y-c2).^2+(z-c2).^2)<=rad; >> bw=bw1|bw2;

>> figure;isosurface(x,y,z,bw,0.5);axis equal; >> set(gcf,'color','w');

>> xlim(li);ylim(li);zlim(li);

>> view(3);camlight;lighting gouraud; >> d=bwdist(~bw);

>> figure;isosurface(x,y,z,d,rad/2);axis equal; >> set(gcf,'color','w');

>> xlim(li);ylim(li);zlim(li);

>> view(3);camlight;lighting gouraud;

>> d=-d;

>> d(~bw)=-Inf; >> l=watershed(d); >> figure;

>> isosurface(x,y,z,l==2,0.5); >> isosurface(x,y,z,l==3,0.5); >> axis equal;

>> set(gcf,'color','w');

>> xlim(li);ylim(li);zlim(li);

>> view(3);camlight;lighting gouraud;

使用Sobel进行边缘检测: >> i=imread('circuit.tif'); >> bw1=edge(i,'roberts'); >> subplot(1,2,1);imshow(i); >> subplot(1,2,2);imshow(bw1); >> clear;

>> image=imread('circuit.tif'); >> i0=edge(image,'sobel'); >> i1=edge(image,'sobel',0.06); >> i2=edge(image,'sobel',0.04); >> i3=edge(image,'sobel',0.02); >> subplot(2,3,1);imshow(image); >> subplot(2,3,2);imshow(i0); >> subplot(2,3,3);imshow(i1); >> subplot(2,3,4);imshow(i2); >> subplot(2,3,5);imshow(i3);

使用Prewitt算子进行边缘检测: >> i=imread('rice.png'); >> subplot(2,2,1);imshow(i); >> bw3=edge(i,'prewitt');

>> subplot(2,2,2);imshow(bw3); >> [bw3,th3]=edge(i,'prewitt');

>> bw3=edge(i,'prewitt',0.05,'horizontal'); >> subplot(2,2,3);imshow(bw3);

>> bw3=edge(i,'prewitt',0.05,'vertical'); >> subplot(2,2,4);imshow(bw3);

使用Log算子进行边缘检测: >> i=imread('circuit.tif'); >> [bw1,th]=edge(i,'log'); >> subplot(2,3,1);imshow(i); >> subplot(2,3,2);imshow(bw1); >> bw2=edge(i,'log',0.0056); >> subplot(2,3,3);imshow(bw2); >> h=fspecial('gaussian',5);

>> [bw3,th3]=edge(i,'zerocross',[],h); >> subplot(2,3,4);imshow(bw3); >> bw4=edge(i,'zerocross',0.025,h); >> subplot(2,3,5);imshow(bw4);

使用Canny算子进行边缘检测: >> i=imread('circuit.tif'); >> subplot(1,3,1);imshow(i); >> [bw,th]=edge(i,'canny'); >> subplot(1,3,2);imshow(bw);

>> [bw1,th1]=edge(i,'canny',[0.2,0.6]); >> subplot(1,3,3);imshow(bw1);

本文来源:https://www.bwwdw.com/article/2a86.html

Top