9.5灰度图像边界跟踪 f=imread('circle.bmp'); subplot(1, 2, 1); imshow(f);
title('原始图像') g=im2double(f);
w=fspecial('laplacian',0); g=imfilter(g, w, 'replictate'); h=boundary_tracr2(g) subplot(1, 2, 2); imshow(h);
title('边界跟踪结果')
9.6用Hough变换检测直线 f=imread('zhx1.jpg'); subplot(2,2,1); imshow(f);
title('原始图像'); T=graythresh(f); f=im2bw(f,T); subplot(2,2,2); imshow(f);
title('二值化图像'); subplot(2,2,3);
f=bwmorph(f,'skel',Inf); f=bwmorph(f,'spur',8); imshow(f);
title('细化图像');
[rodetect,tetadetect,Accumulator]=houghtrans(f,0.25,1,20); subplot(2,2,4); [m,n]=size(f);
for ln=1:length(rodetect); if tetadetect(ln)~=0
x=0:n-1; y=-cot(tetadetect(ln)*pi/180)*x+rodetect(ln)/sin(tetadetect(ln)*pi/180) else x=rodetect(ln)*ones(1,n); y=0:m-1; end; xr=x+1;
yr=floor(y+1.0e-10)+1; xidx=zeros(1,n); xmin=0; xmax=0; for i=1:n if(yr(i)>=1 & yr(i) if tetadetect(ln)~=0 x=xmin-1:xmax-1; y=y(x+1); else y=xmin-1:xmax-1; x=x(y+1); end; y=m-1-y; plot(x,y,'linewidth',1); hold on end axis([0,m-1,0,n-1]) title('Hough变换检测出的直线'); 例9.7 迭代式阈值选择法. f=imread('zhx1.jpg'); subplot(1,2,1); imshow(f); title('原始图像'); f=double(f); T=(min(f(:))+max(f(:)))/2; done=false; i=0 while ~done r1=find(f<=T); r2=find(f>T); Tnew=(mean(f(r1))+mean(f(r2)))/2; done=abs(Tnew-T)<1; T=Tnew; i=i+1; end f(r1)=0; f(r2)=1; subplot(1,2,2); imshow(f); title('迭代阈值二值化图像'); 例9.8 用Otsu法进行阈值选择. f=imread('zhx1.jpg'); subplot(1,2,1); imshow(f); title('原始图像'); T=graythresh(f); g=im2bw(f,T); subplot(1,2,2); imshow(g); title('Otsu方法二值化图像'); 例9.9 用Watershed算法分割图像. f=imread('zhx2.jpg'); subplot(2,2,1); imshow(f); title('(a)原始图像'); subplot(2,2,2); %计算距离函数 hv=fspecial('prewitt'); hh=hv.'; gv=abs(imfilter(f,hv,'replicate')); gh=abs(imfilter(f,hh,'replicate')); g=sqrt(gv.^2+gh.^2); %watershed算法分割 subplot(2,2,2); L=watershed(g); wr=L==0; imshow(wr); title('(b)分水岭'); subplot(2,2,3); imshow(uint8(f)); title('(c)分割结果'); %取出梯度图中局部极小值点 rm=imregionalmin(g); subplot(2,2,4); imshow(rm); title('(d)局部极小值');