メインコンテンツ

houghlines

ハフ変換に基づくライン セグメントの抽出

説明

lines = houghlines(BW,theta,rho,peaks) は、ハフ変換で特定のビンに関連付けられたイメージ BW のライン セグメントを抽出します。thetarho は関数 hough が返すベクトルです。peaks は、関数 houghpeaks が返す行列で、ライン セグメントの探索に使用するハフ変換ビンの行座標と列座標が含まれます。戻り値 lines には、抽出されたライン セグメントに関する情報が含まれています。

lines = houghlines(BW,theta,rho,peaks,Name=Value) は、ライン抽出のさまざまな特性を制御する名前と値の引数を使用します。

すべて折りたたむ

イメージをワークスペースに読み取ります。

I  = imread('circuit.tif');

イメージを回転します。

rotI = imrotate(I,33,'crop');

バイナリ イメージを作成します。

BW = edge(rotI,'canny');

バイナリ イメージを使用してハフ変換を作成します。

[H,T,R] = hough(BW);
imshow(H,[],'XData',T,'YData',R,...
            'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;

Figure contains an axes object. The axes object with xlabel theta, ylabel rho contains an object of type image.

イメージのハフ変換でピークを検出します。

P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');

Figure contains an axes object. The axes object with xlabel theta, ylabel rho contains 2 objects of type image, line. One or more of the lines displays its values using only markers

ラインを検出してプロットします。

lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   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');

   % 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

Figure contains an axes object. The hidden axes object contains 37 objects of type image, line. One or more of the lines displays its values using only markers

最長のライン セグメントをシアンに色付けして強調表示します。

plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');

Figure contains an axes object. The hidden axes object contains 38 objects of type image, line. One or more of the lines displays its values using only markers

入力引数

すべて折りたたむ

バイナリ イメージ。2 次元 logical 行列または 2 次元数値行列として指定します。数値入力の場合、非ゼロのピクセルは 1 (true) であると見なされます。

データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

ラインの回転角度 (度単位)。数値行列として指定します。角度は x 軸と rho ベクトルの間で測定されます。

データ型: double

座標原点からの距離。数値行列として指定します。座標原点はイメージの左上隅 (0,0) です。

データ型: double

ハフ変換ビンの行座標と列座標。数値行列として指定します。

データ型: double

名前と値の引数

すべて折りたたむ

オプションの引数のペアを Name1=Value1,...,NameN=ValueN として指定します。ここで、Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後に指定しなければなりませんが、ペアの順序は重要ではありません。

例: lines = houghlines(BW,T,R,P,MinLength=7) は、ラインの最小の長さが 7 ピクセルであることを指定します。

R2021a より前では、コンマを使用して名前と値をそれぞれ区切り、Name を引用符で囲みます。

例: lines = houghlines(BW,T,R,P,"MinLength",7);

同じハフ変換ビンに関連付けられている 2 つのライン セグメント間の距離。正の数値として指定します。ライン セグメント間の距離が指定された値よりも小さい場合、関数 houghlines は 2 つのライン セグメントを結合して 1 つにします。

データ型: double

ラインの最小の長さ。正の数値として指定します。houghlines は指定された値より短いラインを取り除きます。

データ型: double

出力引数

すべて折りたたむ

検出されたライン。見つかった結合ライン セグメントの数と長さが等しい構造体配列として返されます。構造体配列の各要素には、これらのフィールドがあります。

フィールド

説明

point1

ライン セグメントの終了点の座標を指定する 2 要素ベクトル [X Y]

point2

ライン セグメントの終了点の座標を指定する 2 要素ベクトル [X Y]

theta

ハフ変換ビンの角度 (度単位)

rho

ハフ変換ビンの rho 軸位置

拡張機能

すべて展開する

バージョン履歴

R2006a より前に導入