Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

bwdist

バイナリ イメージの距離変換

説明

D = bwdist(BW) は、バイナリ イメージ BW のユークリッド距離変換を計算します。BW 内のピクセルごとに、距離変換によって、そのピクセルから BW 内の最も近い非ゼロ ピクセルまでの距離の数値が割り当てられます。

[D,idx] = bwdist(BW) は、最も近いピクセルのマップもインデックス配列 idx の形式で計算します。idx の各要素には、BW 内の最も近い非ゼロ ピクセルの線形インデックスが含まれています。最も近いピクセルのマップは、特徴マップ、特徴変換または最近傍変換とも呼ばれます。

[D,idx] = bwdist(BW,method) は、method で指定される別の距離計量を使用して、距離変換を計算します。

すべて折りたたむ

この例では、バイナリ イメージのユークリッド距離変換とイメージの最も近いピクセルのマップを計算する方法を説明します。

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

bw = zeros(5,5); 
bw(2,2) = 1; 
bw(4,4) = 1
bw = 5×5

     0     0     0     0     0
     0     1     0     0     0
     0     0     0     0     0
     0     0     0     1     0
     0     0     0     0     0

距離変換を計算します。

[D,IDX] = bwdist(bw)
D = 5x5 single matrix

    1.4142    1.0000    1.4142    2.2361    3.1623
    1.0000         0    1.0000    2.0000    2.2361
    1.4142    1.0000    1.4142    1.0000    1.4142
    2.2361    2.0000    1.0000         0    1.0000
    3.1623    2.2361    1.4142    1.0000    1.4142

IDX = 5x5 uint32 matrix

    7    7    7    7    7
    7    7    7    7   19
    7    7    7   19   19
    7    7   19   19   19
    7   19   19   19   19

最近傍行列 IDX の場合、値 7 と 19 は、線形行列インデックスを使用した非ゼロ要素の位置を表します。ピクセルに 7 が含まれている場合、最も近い非ゼロ近傍は線形位置 7 です。

この例では、サポートされている距離メソッドの 2 次元距離変換を比較する方法を説明します。Figure では、ユークリッド距離メソッドで取得した円に対し、準ユークリッド距離変換がどのようにして最良近似を行うのかに注意してください。

bw = zeros(200,200);
bw(50,50) = 1; bw(50,150) = 1; bw(150,100) = 1;
D1 = bwdist(bw,'euclidean');
D2 = bwdist(bw,'cityblock');
D3 = bwdist(bw,'chessboard');
D4 = bwdist(bw,'quasi-euclidean');
RGB1 = repmat(rescale(D1), [1 1 3]);
RGB2 = repmat(rescale(D2), [1 1 3]);
RGB3 = repmat(rescale(D3), [1 1 3]);
RGB4 = repmat(rescale(D4), [1 1 3]);

figure
subplot(2,2,1), imshow(RGB1), title('Euclidean')
hold on, imcontour(D1)
subplot(2,2,2), imshow(RGB2), title('Cityblock')
hold on, imcontour(D2)
subplot(2,2,3), imshow(RGB3), title('Chessboard')
hold on, imcontour(D3)
subplot(2,2,4), imshow(RGB4), title('Quasi-Euclidean')
hold on, imcontour(D4)

Figure contains 4 axes objects. Axes object 1 with title Euclidean contains 2 objects of type image, contour. Axes object 2 with title Cityblock contains 2 objects of type image, contour. Axes object 3 with title Chessboard contains 2 objects of type image, contour. Axes object 4 with title Quasi-Euclidean contains 2 objects of type image, contour.

この例では、中心に非ゼロ ピクセルが 1 つある 3 次元イメージの距離変換の等値面プロットを比較する方法を説明します。

bw = zeros(50,50,50); bw(25,25,25) = 1;
D1 = bwdist(bw);
D2 = bwdist(bw,'cityblock');
D3 = bwdist(bw,'chessboard');
D4 = bwdist(bw,'quasi-euclidean');
figure
subplot(2,2,1), isosurface(D1,15), axis equal, view(3)
camlight, lighting gouraud, title('Euclidean')
subplot(2,2,2), isosurface(D2,15), axis equal, view(3)
camlight, lighting gouraud, title('City block')
subplot(2,2,3), isosurface(D3,15), axis equal, view(3)
camlight, lighting gouraud, title('Chessboard')
subplot(2,2,4), isosurface(D4,15), axis equal, view(3)
camlight, lighting gouraud, title('Quasi-Euclidean')

Figure contains 4 axes objects. Axes object 1 with title Euclidean contains an object of type patch. Axes object 2 with title City block contains an object of type patch. Axes object 3 with title Chessboard contains an object of type patch. Axes object 4 with title Quasi-Euclidean contains an object of type patch.

入力引数

すべて折りたたむ

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

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

距離計量。次のいずれかの値を指定します。

メソッド

説明

'chessboard'

2 次元の場合、(x1,y1) と (x2,y2) の間のチェス盤距離は、次のとおりです。

max(│x1 – x2│,│y1 – y2│).

'cityblock'

2 次元では、(x1,y1) と (x2,y2) の間のシティブロック距離は、次のとおりです。

│x1 – x2│ + │y1 – y2

'euclidean'

2 次元の場合、(x1,y1) と (x2,y2) の間のユークリッド距離は、次のとおりです。

(x1x2)2+(y1y2)2.

'quasi-euclidean'

2 次元の場合、(x1,y1) と (x2,y2) の間の準ユークリッド距離は、次のとおりです。

|x1x2|+(21)|y1y2|, |x1x2|>|y1y2|

(21)|x1x2|+|y1y2|, otherwise.

詳細については、バイナリ イメージの距離変換を参照してください。

データ型: char | string

出力引数

すべて折りたたむ

距離。BW と同じサイズの数値配列として返されます。各要素の値は、ピクセルから BW 内の最も近い非ゼロ ピクセルまでの距離で、method 距離計量によって定義されます。

データ型: single

インデックス配列。BW と同じサイズの数値配列として返されます。idx の各要素には、BW 内の最も近い非ゼロ ピクセルの線形インデックスが含まれています。idx のクラスは入力イメージの要素の数によって異なり、次のとおり決定します。

クラス範囲
'uint32'numel(BW) <= 232 − 1
'uint64'numel(BW) >= 232

データ型: uint32 | uint64

ヒント

  • bwdist は、特に 2 次元の場合、高速アルゴリズムを使用して真のユークリッド距離変換を計算します。他のメソッドは、主に教育を目的としたものです。ただし、別の距離変換には、多次元入力イメージ、特に多数の非ゼロ要素をもつものに対しては、極めて高速なものもあります。

  • 関数 bwdist は Version 6.4 (R2009b) で変更されました。以前のバージョンの Image Processing Toolbox では、ユークリッド距離変換やそれに関連したラベル行列の計算に、さまざまなアルゴリズムが使用されていました。以前の実装時と同じ結果を得る必要がある場合、関数 bwdist_old を使用してください。

アルゴリズム

  • ユークリッド距離変換では、bwdist は、高速アルゴリズムを使用します。[1]

  • シティブロック、チェス盤、準ユークリッドの各距離変換では、bwdist は、2 パスのシーケンシャル スキャン アルゴリズムを使用します。[2]

  • 得られる距離尺度は、スキャンで使用する重み集合によって異なります。これについては、[3] で説明されています。

参照

[1] Maurer, Calvin, Rensheng Qi, and Vijay Raghavan, "A Linear Time Algorithm for Computing Exact Euclidean Distance Transforms of Binary Images in Arbitrary Dimensions," IEEE Transactions on Pattern Analysis and Machine Intelligence, Vol. 25, No. 2, February 2003, pp. 265-270.

[2] Rosenfeld, Azriel and John Pfaltz, "Sequential operations in digital picture processing," Journal of the Association for Computing Machinery, Vol. 13, No. 4, 1966, pp. 471-494.

[3] Paglieroni, David, "Distance Transforms: Properties and Machine Vision Applications," Computer Vision, Graphics, and Image Processing: Graphical Models and Image Processing, Vol. 54, No. 1, January 1992, pp. 57-58.

拡張機能

バージョン履歴

R2006a より前に導入

すべて展開する