美文网首页matlab学习
matlab 霍夫检测

matlab 霍夫检测

作者: Kerwin_H | 来源:发表于2019-06-21 09:52 被阅读0次

    %霍夫检测

    BW=imread('lexp_1_2.bmp');

    BW=rgb2gray(BW); 

    thresh=[0.01,0.17]; 

    sigma=2;%定义高斯参数 

    f = edge(double(BW),'canny',thresh,sigma); 

    figure(1),imshow(f,[]); 

    title('canny 边缘检测'); 

    [H, theta, rho]= hough(f,'RhoResolution', 0.5); 

    %imshow(theta,rho,H,[],'notruesize'),axis on,axis normal 

    %xlabel('\theta'),ylabel('rho'); 

    peak=houghpeaks(H,5); 

    hold on 

    lines=houghlines(f,theta,rho,peak); 

    figure,imshow(f,[]),title('Hough Transform Detect Result'),hold on 

    for k=1:length(lines) 

        xy=[lines(k).point1;lines(k).point2]; 

        plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.6 .6 .6]); 

    end

    %%

    I=imread('1.jpg');

    Ihsv=rgb2hsv(I);

    Iv=Ihsv(:,:,3);                    %提取v空间

    Ivl=Iv(500:end,:);              %截取下半部

    Iedge=edge(Ivl,'sobel');    %边沿检测

    Iedge = imdilate(Iedge,ones(3));%图像膨胀

    %新建窗口,绘图用

    figure (2)

    imshow(Iedge);

    hold on

    %左方直线检测与绘制

    %得到霍夫空间

    [H1,T1,R1] = hough(Iedge,'Theta',20:0.1:75);

    %求极值点

    Peaks=houghpeaks(H1,5);

    %得到线段信息

    lines=houghlines(Iedge,T1,R1,Peaks);

    %绘制线段

    for k=1:length(lines)

    xy=[lines(k).point1;lines(k).point2]; 

    plot(xy(:,1),xy(:,2),'LineWidth',4);

    end

    %右方直线检测与绘制

    [H2,T2,R2] = hough(Iedge,'Theta',-75:0.1:-20);

    Peaks1=houghpeaks(H2,5);

    lines1=houghlines(Iedge,T2,R2,Peaks1);

    for k=1:length(lines1)

    xy1=[lines1(k).point1;lines1(k).point2]; 

    plot(xy1(:,1),xy1(:,2),'LineWidth',4);

    end

    hold off

    %%

    %以下只是做一个带直线的图像而已

    r=300;         

    jiaodu=30;      %更改这个值测试,90度270度时不管用

    jiaodu1=mod(jiaodu,360);   

    flag=0;

    if jiaodu1>=0 && jiaodu1<90

        jiaodu1=jiaodu1;

        flag=1;

    end

    if jiaodu1>=90 && jiaodu1<180

        jiaodu1=180-jiaodu1;

        flag=2;

    end

    if jiaodu1>=180 && jiaodu1<270

        jiaodu1=jiaodu1-180;

        flag=3;

    end

    if jiaodu1>=270 && jiaodu1<360

        jiaodu1=360-jiaodu1;

        flag=4;

    end

    H=floor(r*sin(jiaodu1*pi/180));

    W=floor(r*cos(jiaodu1*pi/180));

    if mod(H,2)==0

        H=H+1;

    end

    if mod(W,2)==0

        W=W+1;

    end

    w=zeros(H,W);

    if jiaodu1 ~= 90 && jiaodu1 ~= 270

        for i=1:H

            for j=1:W

                tmp=floor(j*tan(jiaodu1*pi/180));

                if tmp+1==i

                    w(i,j)=r;

                end

            end

        end

    else

        for i=1:H

            w(i,1)=r;

        end

    end

    if flag==1 || flag==3      %如果角度在1,3象限,卷积矩阵上下翻转

        w=flipud(w);       

    end

    %下面是真正的霍夫变换

    img=mat2gray(w);      %处理这个图像

    [m n]=size(img);

    imshow(img);

    data=zeros(314,2*(m+n));

    for i=1:m                      %将图像二维空间的一个点映射到p=x*cos(theta)+y*sin(theta)方程对应的参数空间的一条曲线

        for j=1:n

            if img(i,j)==1           

                for theta=0.01:0.01:3.14

                    data(round(theta*100),round(i*sin(theta)+j*cos(theta)+m+n))= ...

                    data(round(theta*100),round(i*sin(theta)+j*cos(theta)+m+n))+1;

                end       

            end

        end

    end

    theta=0;

    ma=0;

    for i=1:314                %寻找曲线相交最多的那个点,即找最大值

        for j=1:2*(m+n)

            if data(i,j)>ma

                ma=data(i,j);

                theta=i/100;

                rou=j-m-n;

            end

        end

    end

    figure;imshow(data)    %形象的显示参数空间曲线

    sr_k=tan(jiaodu*pi/180)    %设置的斜率

    re_k=cos(theta)/sin(theta)  %求得的斜率

    相关文章

      网友评论

        本文标题:matlab 霍夫检测

        本文链接:https://www.haomeiwen.com/subject/uuveqctx.html