利用阀值法对图像进行分割:>> f=imread('peppers.png');>> f=rgb2gray(f);>> f=im2double(f);>> t=0.5*(min(f(:))+max(f(:)));>> done=false;>> while ~doneg=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>> tt =0.4691>> r=im2bw(f,t);>> subplot(2,2,1);imshow(f);>> subplot(2,2,2);imshow(r);>> xlabel('迭代法全局阀值分割');>> th=graythresh(f);>> thth =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);>> thrthr =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);elseibg=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;elsetk=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;改进的Watershed算法分割图像:>> i=imread('cameraman.tif'); >> subplot(2,3,1);imshow(i);>> i=double(i);>> hv=fspecial('prewitt');>> hh=hv.';>> gv=abs(imfilter(i,hv,'replicate')); >> gh=abs(imfilter(i,hh,'replicate'));>> g=sqrt(gv.^2+gh.^2);>> subplot(2,3,2);df=bwdist(i); >> imshow(uint8(df*8));>> l=watershed(df);>> em=l==0;>> subplot(2,3,3);imshow(em); >> im=imextendedmax(i,20);>> subplot(2,3,4);imshow(im);>> g2=imimposemin(g,im|em); >> subplot(2,3,5);imshow(g2);>> l2=watershed(g2);>> wr2=l2==0;>> i(wr2)=255;>> subplot(2,3,6);imshow(uint8(i));使用区域生长法对图像进行分割:>> i=imread('peppers.png'); >> i=rgb2gray(i);>> i1=double(i);>> s=255;>> t=55;>> if numel(s)==1si=i1==s;s1=s;elsesi=bwnorph(s,'shrink',Inf);j=find(si);s1=i1(j);end;>> ti=false(size(i1));>> for k=1:length(s1)sv=s1(k);s=abs(i1-sv)<=t;ti=ti|s;end;>> [g,nr]=bwlabel(imreconstruct(si,ti));>> subplot(1,2,1);imshow(i);>> subplot(1,2,2);imshow(g);>> nrnr =2对给定图像进行四叉树分解:>> i=imread('liftingbody.png');>> s=qtdecomp(i,.27);>> blocks=repmat(uint8(0),size(s));>> for dim=[512 256 128 64 32 16 8 4 2];numblocks=length(find(s==dim));if(numblocks>0)values=repmat(uint8(1),[dim dim numblocks]);values(2:dim,2:dim,:)=0;blocks=qtsetblk(blocks,s,dim,values);end;end;>> blocks(end,1:end)=1;>> blocks(1:end,end)=1;>> subplot(1,2,1);imshow(i);>> subplot(1,2,2);imshow(blocks,[]);提取四叉树分解的子块信息:>> i=[1 1 1 1 2 3 6 61 12 1 4 5 6 81 1 1 1 10 15 7 71 1 1 1 20 25 7 720 22 20 22 1 2 3 420 22 22 20 5 6 7 820 22 20 20 9 10 11 1222 22 20 20 13 14 15 16]; >> s=qtdecomp(i,5);>> [vals,r,c]=qtgetblk(i,s,4)vals(:,:,1) =1 1 1 11 12 11 1 1 11 1 1 1 vals(:,:,2) =20 22 20 2220 22 22 2020 22 20 2022 22 20 20r =15c =11>> i=[1 1 1 1 2 3 6 61 12 1 4 5 6 81 1 1 1 10 15 7 71 1 1 1 20 25 7 720 22 20 22 1 2 3 420 22 22 20 5 6 7 820 22 20 20 9 10 11 1222 22 20 20 13 14 15 16];>> s=qtdecomp(i,5);>> newvals=cat(3,zeros(4),ones(4));>> j=qtsetblk(i,s,4,newvals)j =0 0 0 0 2 3 6 60 0 0 0 4 5 6 80 0 0 0 10 15 7 70 0 0 0 20 25 7 71 1 1 1 123 41 1 1 1 5 6 7 81 1 1 1 9 10 11 121 1 1 1 13 14 15 16 使用Roberts边缘检测算子对图像进行边缘检测:>> i=imread('circuit.tif');>> bw1=edge(i,'roberts');>> subplot(1,2,1);imshow(i);>> subplot(1,2,2);imshow(bw1);使用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);。