利用阀值法对图像进行分割: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)-Iterativett=0.4691r=im2bw(f,t);subplot(2,2,1);imshow(f);subplot(2,2,2);imshow(r);xlabel('迭代法全局阀值分割');th=graythresh(f);thth=0.3961s=im2bw(f,th);subplot(2,2,3);imshow(s);xlabel('全局阀值Otsu分割');se=strel('disk',10);ft=imtophat(f,se);thr=graythresh(ft);thrthr=0.1098lt=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;forj=1:isize(1)fork=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)));迭代后阀值165newi=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);axisequal;set(gcf,'color','w');xlim(li);ylim(li);zlim(li);view(3);camlight;lightinggouraud;d=bwdist(~bw);figure;isosurface(x,y,z,d,rad/2);axisequal;set(gcf,'color','w');xlim(li);ylim(li);zlim(li);view(3);camlight;lightinggouraud;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);axisequal;set(gcf,'color','w');xlim(li);ylim(li);zlim(li);view(3);camlight;lightinggouraud;改进的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;ifnumel(s)==1si=i1==s;s1=s;elsesi=bwnorph(s,'shrink',Inf);j=find(si);s1=i1(j);end;ti=false(size(i1));fork=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));fordim=[512256128643216842];numblocks=length(find(s==dim));if(numblocks0)values=repmat(uint8(1),[dimdimnumblocks]);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=[1111236611214568111110157711112025772022202212342022222056782022202091011122222202013141516];s=qtdecomp(i,5);[vals,r,c]=qtgetblk(i,s,4)vals(:,:,1)=1111112111111111vals(:,:,2)=20222022202222202022202022222020r=15c=11i=[1111236611214568111110157711112025772022202212342022222056782022202091011122222202013141516];s=qtdecomp(i,5);newvals=cat(3,zeros(4),ones(4));j=qtsetblk(i,s,4,newvals)j=000023660000456800001015770000202577111112341111567811119101112111113141516使用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]=