霍夫变换检测任意形状一、实验目的1.掌握MATLAB软件的使用,以及其设计流程;2.掌握霍夫变换的实现方法;3.用MATLAB语言设计基于霍夫变换的任意图形的识别。二、实验仪器或设备装MATLAB软件的微机一台三、总体设计原理及流程图1、程序设计的原理霍夫变换的基本思想就是把图像平面上的点对应到参数平面上的曲线,最后通过统计特性来解决问题。具有良好的抗噪声性能和对部分遮盖的不敏感等特性。2、程序流程图程序开始打开图形基于霍夫曼变换的简单图形识别霍夫变换检测直线霍夫变换检测圆识别得到的图像保存图像程序结束四、主要程序代码1、霍夫变换检测圆程序clearall;closeall;clc;%[cr,st]=circle_product;%st(1)-x,st(2)-y,st(3)-rcr=imread('sample.bmp');%circle1.bmpfigure;imshow(cr);[row,range]=size(cr);p=row*range;a=0;Y=zeros(1,p);Q=zeros(1,p);fork1=1:1:rowfork2=1:1:rangeifcr(k1,k2)==0a=a+1;XXX=k2-1;YYY=row+1-k1;Y(a)=YYY;Q(a)=XXX;endendendfork3=1:1:pifY(k3)==0&Q(k3)==0break;endend%z=ones(1,5);%a=1:1:300;b=1:1:300;%z1=sqrt((a-Q(1)).^2+(b-Y(1)).^2);%z2=sqrt((a-Q(2)).^2+(b-Y(2)).^2);%z3=sqrt((a-Q(3)).^2+(b-Y(3)).^2);%z4=sqrt((a-Q(4)).^2+(b-Y(4)).^2);%z5=sqrt((a-Q(5)).^2+(b-Y(5)).^2);%%z1=sqrt((a-Q(1))*(a-Q(1))+(b-Y(1))*(b-Y(1)));%%Z1=(a-Q(1)).^2+(b-Y(1)).^2;r0=(abs(z1-z2)=0.1&abs(z1=z3)=0.1&abs(z1=z4)=0.1&abs(z1=z5)=0.1&abs(z2=z3)=0.1&abs(z2=z4)=0.1&abs(z2=z5)=0.1);%r=r0.*z1;%aa=r0.*a;%bb=r0.*b;%rrr=round(r(r0~=0))%aaa=aa(r0~=0)%bbb=bb(r0~=0)k7=floor(k3/6);k5=2;a=1:1:300;b=1:1:300;rrr=zeros(1,p);aaa=zeros(1,p);bb=zeros(1,p);k6=0;as=0;k11=1;fork5=1:1:k7%k6=0;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%z1=sqrt((a-Q(k5)).^2+(b-Y(k5)).^2);z2=sqrt((a-Q(k5+1)).^2+(b-Y(k5+1)).^2);z3=sqrt((a-Q(k5+2)).^2+(b-Y(k5+2)).^2);z4=sqrt((a-Q(k5+3)).^2+(b-Y(k5+3)).^2);z5=sqrt((a-Q(k5+4)).^2+(b-Y(k5+4)).^2);z6=sqrt((a-Q(k5+5)).^2+(b-Y(k5+5)).^2);r0=(abs(z1-z2)=.1&abs(z1=z3)=.1&abs(z1=z4)=.1&abs(z1=z5)=.1&abs(z1=z6)=.1&abs(z2=z3)=1&abs(z2=z4)=.1&abs(z2=z5)=.1&abs(z2=z6)=.1);rr=r0.*z1;aa=r0.*a;bb=r0.*b;[m,n]=size(rr);ifrr==zeros(1,n);as=as+1;continue;end[pz1,pz]=size(rr(r0~=0));rrr=round(rr(r0~=0))aaa=aa(r0~=0)bbb=bb(r0~=0)pz2=pz+k11-1;k9=1;fork8=k11:pz2RR(k8)=rrr(k9);XX(k8)=aaa(k9);YY(k8)=bbb(k9);k9=k9+1;endk11=k8+1;%ifrr==zeros(1,n);%as=as+1;continue;%endend%ifaa==zeros(1,300)%as=1%endrangex=1:1:range;rowy=1:1:row;[RX,RY]=meshgrid(rangex,rowy);RT=RX;figure;mesh(RX,RY,RT);holdon;plot3(XX,YY,RR,'ko');%%%%%%%%%%%%%%%5[rhao,nn]=the_max1(XX,YY,RR);i1=ones(row,range);rhaocenter=rhao;%t2=1.5;t3=1.5;fork1=1:1:rowfork2=1:1:rangeXXX=k2-1;YYY=row+1-k1;ifabs(((XXX-center(1))*(XXX-center(1))+(YYY-center(2))*(YYY-center(2)))-center(3)*center(3))=101i1(k1,k2)=0;endendendimwrite(i1,'找出的圆.bmp','bmp');i2=imread('找出的圆.bmp');figure;imshow(i2);%st2、霍夫变换检测直线clearall;closeall;clc;i=imread('sample.bmp');[row,range]=size(i);p=row*range;a=0;Y=zeros(1,p);Q=zeros(1,p);fork1=1:1:rowfork2=1:1:rangeifi(k1,k2)==0a=a+1;XXX=k2-1;YYY=row+1-k1;Y(a)=YYY;Q(a)=YYY-XXX;endendend%fort=1:1:a%o=[Y(t)Q(t)]%endX1=[01];Y1=[Y(1)Q(1)];plot(X1,Y1,'r');holdon;YY=zeros(1,a-1);QQ=zeros(1,a-1);fort=2:1:aX2=[01];Y2=[Y(t)Q(t)];plot(X2,Y2);[YY(t-1),QQ(t-1)]=pll(X1,Y1,X2,Y2);%yyy=YY(t-1)%qqq=QQ(t-1)%看交点plot(YY(t-1),QQ(t-1),'ko');end%[res]=gather_point(YY,QQ,QQ);nn=0;res%[m,n3]=size(YY);%forno1=1:1:n3%n=0;%forno2=(no1+1):1:n3%if(abs(res(no2,1)-res(no1,1))=0.0001&abs(res(no2,2)-res(no1,2))=0.0001&abs(res(no2,2)-res(no1,2))=0.0001)%n=n+1;hao=res(no1,:);%end%end%ifnn=n3/5%rhao=hao;break;%elseifnnn%rhao=hao;nn=n;%end%end[rhao]=the_max1(YY,QQ,QQ);%yyy=YY%qqq=QQ%t2=0;t3=0;%fort1=2:1:t%t2=YY(t1)+t2;%t3=t3+QQ(t1);%end%t2=t2/(t1-1)%t3=t3/(t1-1)%t2=m,t3=b%i1=ones(row,range);%t2=2;t3=0;i1=ones(row,range);rhaot2=rhao(1);t3=rhao(2);t2=round(t2);t3=round(t3);%t2=1.5;t3=1.5;fork1=1:1:rowfork2=1:1:rangeXXX=k2-1;YYY=row+1-k1;ifabs(YYY-(t2*XXX+t3))=1%YYY==t2*XXX+t3i1(k1,k2)=0;endendendimwrite(i1,'找出的直线.bmp','bmp');i2=imread('找出的直线.bmp');figure;subplot(2,1,1);imshow(i);subplot(2,1,2);imshow(i2);五、实验结果1、霍夫变换检测圆实验结果截图待检测的图:检测出的圆:2、霍夫变换检测直线实验结果截图待检测的图检测出的直线