如何在Matlab中使用hough线找到矩形区域?

时间:2014-12-18 13:40:41

标签: matlab image-processing hough-transform

编辑:

我使用此代码,但没有找到平板区域。我怎么找到这个盘子?

此代码仅提供水平线,但我必须找到垂直和水平线。


    I  = imread('image.jpg');
    I= rgb2gray(I);
    %I(:,1:2)=0;
    %I(1,:)=0;
    %I(end,:)=0;
    %I(:,end)=0;
    BW = edge(I,'canny');
    %imshow(BW)
    [H,T,R] = hough(BW);
    %{
    imshow(H,[],'XData',T,'YData',R,...
                'InitialMagnification','fit');
    xlabel('\theta'), ylabel('\rho');
    axis on, axis normal, hold on;
    %}
    P = houghpeaks(H, 20,'NHoodSize',[1 1],'threshold',ceil(0.1*max(H(:))));
    lines = houghlines(BW, T, R, P, 'FillGap', 40, 'MinLength', 1);
    %P  = houghpeaks(H,7,'NHoodSize','threshold',ceil(0.4*max(H(:))));
    %lines = houghlines(BW,T,R,P,'FillGap',40,'MinLength',90);
    x = T(P(:,2)); y = R(P(:,1));
    %plot(x,y,'s','color','blue');
    % Find lines and plot them
    figure;
    imagesc(I);hold on;colormap gray;
    axis image; 
    max_len = 0;
    for k = 1:length(lines)
       xy = [lines(k).point1; lines(k).point2];
       % Plot beginnings and ends of lines
       %plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
       plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red')
       plot(xy(1,1),xy(2,2),'x','LineWidth',2,'Color','blue');
       %plot(xy(2,2),xy(2,2),'x','LineWidth',2,'Color','green');
      plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); %connect red and blue point
     % Determine the endpoints of the longest line segment
       len = norm(lines(k).point1 - lines(k).point2);
       if ( len > max_len)
          max_len = len;
          xy_long = xy;
       end
    end

此部分绘制连接红点和蓝点。如何连接蓝色蓝点和红色红点?如果可以的话,我找到了盘区。

% Plot beginnings and ends of lines %plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red') plot(xy(1,1),xy(2,2),'x','LineWidth',2,'Color','blue'); %plot(xy(2,2),xy(2,2),'x','LineWidth',2,'Color','green'); plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); %connect red and blue point

然后,可能会lines = houghlines(BW, T, R, P, 'FillGap', 40, 'MinLength', 90);这部分发生变化。

Input pic Output pic

0 个答案:

没有答案