cv.distanceTransform - MATLAB File Help |
Calculates the distance to the closest zero pixel for each pixel of the source image
dst = cv.distanceTransform(src)
[dst, labels] = cv.distanceTransform(src)
[...] = cv.distanceTransform(..., 'OptionName',optionValue, ...)
src
.int32
and the same size as src
.distance = |x1-x2| + |y1-y2|
distance = max(|x1-x2|,|y1-y2|)
labels
output. In case of the
'L1' or 'C' distance type, the parameter is forced to 3 because a 3x3 mask
gives the same result as 5x5 or any larger aperture. The following options
are available:
labels
output. One of:
src
(as well as all the
non-zero pixels closest to the connected component) will be assigned the
same label.dst
. It can be uint8
or single
.
Only supported by the first variant without labels
output. Type uint8
can be used only for the first variant of the function and
DistanceType = 'L1'
, otherwise the default single
is assumed.The function cv.distanceTransform calculates the approximate or precise distance from every binary image pixel to the nearest zero pixel. For zero image pixels, the distance will obviously be zero.
When MaskSize
is 'Precise' and DistanceType
is 'L2', the function
runs the algorithm described in [Felzenszwalb04]. This algorithm is
parallelized with the TBB library.
In other cases, the algorithm [Borgefors86] is used. This means that for a
pixel the function finds the shortest path to the nearest zero pixel
consisting of basic shifts: horizontal, vertical, diagonal, or knight's move
(the latest is available for a 5x5 mask). The overall distance is calculated
as a sum of these basic distances. Since the distance function should be
symmetric, all of the horizontal and vertical shifts must have the same cost
(denoted as a
), all the diagonal shifts must have the same cost (denoted
as b
), and all knight's moves must have the same cost (denoted as c
).
For the 'C' and 'L1' types, the distance is calculated precisely, whereas
for 'L2' (Euclidian distance) the distance can be calculated only with a
relative error (a 5x5 mask gives more accurate results). For a
, b
, and
c
, OpenCV uses the values suggested in the original paper:
a = 1, b = 2
a = 0.955, b = 1.3693
a = 1, b = 1.4, c = 2.1969
a = 1, b = 1
Typically, for a fast, coarse distance estimation 'L2', a 3x3 mask is used. For a more accurate distance estimation 'L2', a 5x5 mask or the precise algorithm is used. Note that both the precise and the approximate algorithms are linear on the number of pixels.
The second variant of the function does not only compute the minimum distance
for each pixel (x,y)
but also identifies the nearest connected component
consisting of zero pixels (LabelType='CComp'
) or the nearest zero pixel
(LabelType='Pixel'
). Index of the component/pixel is stored in
labels(x,y)
. When LabelType
is 'CComp' the function automatically finds
connected components of zero pixels in the input image and marks them with
distinct labels. When LabelType
is 'Pixel', the function scans through the
input image and marks all the zero pixels with distinct labels.
In this mode, the complexity is still linear. That is, the function provides
a very fast way to compute the Voronoi diagram for a binary image. Currently,
the second variant can use only the approximate distance transform algorithm,
i.e. MaskSize='Precise'
is not supported yet.
[Felzenszwalb04]:
Pedro Felzenszwalb and Daniel Huttenlocher. "Distance transforms of sampled functions". Technical report, Cornell University, 2004.
[Borgefors86]:
Gunilla Borgefors. "Distance transformations in digital images". Computer vision, graphics, and image processing, 34(3):344-371, 1986.