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。

此示例说明如何比较支持的距离方法的二维距离变换。在图中,请注意准欧几里德距离变换如何以最佳方式逼近通过欧几里德距离方法获得的圆形。

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)

此示例说明如何比较中心包含单个非零像素的三维图像的距离变换的等值面图。

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')

输入参数

全部折叠

二值图像,指定为任意维度的数值或逻辑数组。对于数值输入,任何非零像素都被视为 1 (true)。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

距离度量,指定为下列值之一。

方法

描述

'chessboard'

在二维中,(x1,y1) 和 (x2,y2) 之间的棋盘距离为

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

'cityblock'

在二维空间中,(x1,y1) 和 (x2,y2) 之间的城市街区距离为

│x1 – x2│ + │y1 – y2

'euclidean'

在二维空间中,(x1,y1) 和 (x2,y2) 之间的欧几里德距离为

(x1x2)2+(y1y2)2.

'quasi-euclidean'

在二维空间中,(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 使用快速算法来计算真欧几里德距离变换,尤其是在二维情况下。其他方法主要是出于教学原因而提供。然而,对于多维输入图像,尤其是那些具有许多非零元素的图像,替代距离变换有时要快得多。

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

算法

  • 对于欧几里德距离变换,bwdist 使用快速算法。[1]

  • 对于城市街区、棋盘和准欧几里德距离变换,bwdist 使用两遍顺序扫描算法。[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 之前推出

全部展开