2016-08-10 13 views
0

Matlabを使用して画像内のすべての直線(垂直または対角)を検索しようとしています。問題は、水平線が偶数行含まれていることです。 これは私のコードです。どのように私は下の画像のすべての直線を検出することができますか(45から115度の間であり得る)?ハフ変換を使用して特定の角度範囲の線を検出する方法

function [linesnum, avg]= hh(inp_file,tresh) 

I = imread(inp_file); 

BW = edge(I,'canny'); 
% [H,T,R] = hough(BW,'Theta', 45:0.5:90); % it has no efect 

[H,T,R] = hough(BW); 
P = houghpeaks(H,300,'threshold',ceil(0.3*max(H(:)))); 

lines = houghlines(BW,T,R,P,'FillGap',10,'MinLength',30); 

if (do_plot) 
    figure, imshow(I), hold on 
    x = T(P(:,2)); 
    y = R(P(:,1)); 
    plot(x,y,'s','color','black'); 
end 
max_len = 0; 
linesnum = 0; 
sumLen = 0; 

for k = 1:length(lines) 
    xy = [lines(k).point1; lines(k).point2]; 
    vert = xy(1,1)==xy(2,1); 
    if (do_plot) 
     plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); 

     % 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'); 
    end 

    len = norm(lines(k).point1 - lines(k).point2); 
    sumLen = sumLen + len; 

    linesnum = linesnum +1; 

end 
avg = sumLen/linesnum; 
end 

enter image description here

これはサンプル画像です:そこのグリッドを検出するための同様のquestionがあるが、それは彼らがの穴に依存し、その質問に思わ

enter image description here


グリッドパターンとその向きを検出するためのグリッド、入力は異なります。

+0

あなたの質問は、重複したマークに関連しています。洞察のためのアムロの答えを見てください。 – rayryeng

+0

@rayryengグリッドのパターンと向きを検出するためにグリッドの穴に頼っているのですが、入力が違っているようです。 – Ahmad

+0

まずエッジ検出を行う必要があります。私はまだ彼らがやっていることを試みるだろう。本当にうまくいかない場合は、再開を検討します。 – rayryeng

答えて

0

ハフ変換を使用して直線を検出した後、各ラインの角度を測定し、範囲内にある場合はそれを選択できます。

% after extracting straight lines using Haugh transform. 
for k = 1:length(lines) 

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

    deltaY = xy(2,2) - xy(1,2); 
    deltaX = xy(2,1) - xy(1,1); 
    % calculate the angle of line 
    angle = atan2(deltaY, deltaX) * 180/pi; 
    if (angle > 45 && angle < 115) 

     plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); 

     % 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');   
    end 
end 
関連する問題