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

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('City block')
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) 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') 输入参数

'chessboard'

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

'cityblock'

│x1 – x2│ + │y1 – y2

'euclidean'

$\sqrt{{\left({x}_{1}-{x}_{2}\right)}^{2}+{\left({y}_{1}-{y}_{2}\right)}^{2}}.$

'quasi-euclidean'

输出参数

'uint32'numel(BW) <= 232 − 1
'uint64'numel(BW) >= 232

提示

• bwdist 使用快速算法来计算真欧几里德距离变换，尤其是在二维情况下。其他方法主要是出于教学原因而提供。然而，对于多维输入图像，尤其是那些具有许多非零元素的图像，替代距离变换有时要快得多。

• 版本 6.4 (R2009b) 中更改了 bwdist 函数。早期版本的 Image Processing Toolbox 使用不同的算法来计算欧几里德距离变换和相关联的标签矩阵。如果您需要实现与之前相同的结果，请使用函数 bwdist_old

算法

• 对于欧几里德距离变换，bwdist 使用快速算法。

• 对于 cityblock、chessboard 和准欧几里德距离变换，bwdist 使用两遍顺序扫描算法。

• 不同距离测量是通过在扫描中使用不同权重集来实现的，如  中所述。

 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.

 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.

 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.