天天看點

基于matlab的圖像分割,基于MATLAB的圖像分割技術.doc

基于MATLAB的圖像分割技術

利用閥值法對圖像進行分割:

>> 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)=-In