digitopo.grayscale
¶
Homotopic operators on grayscale images.
A grayscale image is seen as a stack of binary images, its sections: for
each level k, the section is the set of the points whose value is at
least k. An operator is homotopic when every section of the result has
the topology of the same section of the input (same components and holes in
2D; also cavities and tunnels in 3D). On a 0/1 image there is one nonempty
section, and these operators become binary thinning and thickening.
The functions, parameters and results are those of pink26.grayscale,
point by point: digitopo ports the Pink code that pink26 calls (lhtkern.c,
lhtkern3d.c and llambdakern.c, with pink26's fix of the lambda-kernel) and
keeps its scan orders, queues and neighbour orders. The operators come from
Pink and from the papers [BEC97] and [CBB01] (see the references of each
function).
Words used by the docstrings
- destructible point: a point
pthat is simple in the section of levelF(p): it can leave that section without changing its topology. Loweringpto the largest value of its neighbours belowF(p)keeps the topology of every section. - constructible point: the dual, a point that can join the section above
it without changing its topology. Raising it to the smallest value of its
neighbours above
F(p)keeps the topology of every section. - lambda-destructible point: a destructible point, or a peak (no
neighbour at its level or above) at most
lambdaabove its highest lower neighbour, or a pass betweenk >= 2lower regions of its neighbourhood of which at leastk - 1lie entirely at mostlambdabelow it. Lowering such a point may merge minima or erase a peak of contrast at mostlambda: the topology changes, but only by details of small contrast.
Connectivity
In grayscale, connectivity is the connectivity of the minima (the
dark regions), as in [CBB01] and Pink. The sections (the bright regions) use
the complementary connectivity. This is not the convention of the binary
functions, where the connectivity is that of the object: on a 0/1 image,
connectivity=4 thins like a binary 8-connected object on a 4-connected
background, and connectivity=8 like a 4-connected object on an
8-connected background. Accepted values: 4 or 8 in 2D, 6 or 26 in 3D (18 is
refused: Pink would silently treat it as 26).
Border
The points on the border of the image (its first and last rows and columns,
and its first and last planes in 3D) are never modified: digitopo behaves like
pink26, Pink and its commands htkern, hthin, hthick and lambdaskel.
Hence two minima that touch the border are never merged through the outside
of the image, and a crest that runs to the border is kept. A point next to
the border is judged with its full 3 x 3 (3 x 3 x 3 in 3D) neighbourhood,
which lies inside the image. To have the border processed too, pad the
image yourself and crop the result, for instance:
padded = np.pad(image, 1, mode="edge")
result = digitopo.grayscale.homotopic_kernel(padded, 4)[1:-1, 1:-1]
Condition image
Each operator accepts an optional condition image of the same shape as
the image, which limits the change. For the thinning and the kernels it is a
lower bound; for the thickening it is an upper bound (see each function).
References
[BEC97] G. Bertrand, J.-C. Everat, M. Couprie, "Image segmentation through operators based upon topology", Journal of Electronic Imaging 6(4), 1997, pp. 395-405.
[CBB01] M. Couprie, F. N. Bezerra, G. Bertrand, "Topological operators for grayscale image processing", Journal of Electronic Imaging 10(4), 2001, pp. 1003-1015.
homotopic_thinning
¶
homotopic_thinning(
image: ArrayLike,
connectivity: int,
condition: ArrayLike | None = None,
max_steps: int | None = None,
*,
convert: bool = False,
) -> NDArray[np.uint8]
Lower the destructible points of a grayscale image, step by step.
Each step lowers, one after the other, every point that was destructible
at the start of the step, each to the lowest value that keeps the
topology of all the sections, but never below the value it could reach at
the start of the step. A step shrinks the bright regions by about one
point. Run until stability, the result is a homotopic kernel (see
homotopic_kernel for the difference between the two).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array_like
|
2D or 3D |
required |
connectivity
|
int
|
Connectivity of the minima: 4 or 8 in 2D, 6 or 26 in 3D. The bright regions use the complementary connectivity (see the module documentation: this is not the convention of the binary functions). |
required |
condition
|
array_like
|
|
None
|
max_steps
|
int or None
|
Number of steps. |
None
|
convert
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
New |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
DigitopoError
|
If |
See Also
homotopic_thickening, homotopic_kernel, lambda_kernel
Notes
Complexity: O(N) time per step for N points, and about as many steps as the size of the bright regions.
References
[BEC97], [CBB01] appendix A (see the module documentation). Pink
functions: lhthindelta (2D), lhthindelta3d (3D), command
hthin.
Examples:
A bump of height 40 with a peak of 90, on a background at 0. One step removes the border of the bump (the points next to the background) and keeps the peak. Run until stability, a single point is left, at the level of the peak: a last point cannot be lowered without deleting the bump, which would change the topology:
>>> import numpy as np, digitopo
>>> image = np.zeros((7, 7), dtype=np.uint8)
>>> image[1:6, 1:6] = 40
>>> image[3, 3] = 90
>>> digitopo.grayscale.homotopic_thinning(image, 4, max_steps=1)
array([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 40, 40, 40, 0, 0],
[ 0, 0, 40, 90, 40, 0, 0],
[ 0, 0, 40, 40, 40, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
>>> digitopo.grayscale.homotopic_thinning(image, 4)
array([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 90, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
homotopic_thickening
¶
homotopic_thickening(
image: ArrayLike,
connectivity: int,
condition: ArrayLike | None = None,
max_steps: int | None = None,
*,
convert: bool = False,
) -> NDArray[np.uint8]
Raise the constructible points of a grayscale image, step by step.
The dual of homotopic_thinning: each step raises, one after the
other, every point that was constructible at the start of the step, each
to the highest value that keeps the topology of all the sections, but
never above the value it could reach at the start of the step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array_like
|
2D or 3D |
required |
connectivity
|
int
|
Connectivity of the minima: 4 or 8 in 2D, 6 or 26 in 3D (see the module documentation). |
required |
condition
|
array_like
|
|
None
|
max_steps
|
int or None
|
As for |
None
|
convert
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
New |
Raises:
| Type | Description |
|---|---|
(TypeError, ValueError, DigitopoError)
|
As for |
See Also
homotopic_thinning, homotopic_kernel
Notes
Complexity: as homotopic_thinning.
References
[BEC97], [CBB01] (see the module documentation). Pink functions:
lhthickdelta (2D), lhthickdelta3d (3D), command hthick.
Examples:
The dual of the thinning example, with the dual connectivity (8 for 4): a plateau at 255 with a pit at 215, which has a deeper point at 165. One step raises the border of the pit to the plateau; run until stability, only the deepest point is left:
>>> import numpy as np, digitopo
>>> image = np.full((7, 7), 255, dtype=np.uint8)
>>> image[1:6, 1:6] = 215
>>> image[3, 3] = 165
>>> digitopo.grayscale.homotopic_thickening(image, 8, max_steps=1)
array([[255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255],
[255, 255, 215, 215, 215, 255, 255],
[255, 255, 215, 165, 215, 255, 255],
[255, 255, 215, 215, 215, 255, 255],
[255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255]], dtype=uint8)
>>> digitopo.grayscale.homotopic_thickening(image, 8)
array([[255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 165, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255]], dtype=uint8)
homotopic_kernel
¶
homotopic_kernel(
image: ArrayLike,
connectivity: int,
condition: ArrayLike | None = None,
*,
convert: bool = False,
) -> NDArray[np.uint8]
Lower the destructible points of a grayscale image until none is left.
Also called the ultimate homotopic thinning. The destructible points are lowered, always taking a point of the LOWEST level first (Pink uses a priority queue), until no point above its condition value is destructible. The minima therefore spread as much as possible, and end separated by thin crest lines.
This is not the same as homotopic_thinning run until stability.
The thinning lowers the points of a step in the order of its work list
(the first step takes the points in reverse raster order, the next ones
the points just lowered and their neighbours), whatever their level,
whereas the kernel lowers first the points next to the deepest minima.
Both results have no destructible point and the topology of the input,
but they may differ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array_like
|
2D or 3D |
required |
connectivity
|
int
|
Connectivity of the minima: 4 or 8 in 2D, 6 or 26 in 3D (see the module documentation). |
required |
condition
|
array_like
|
|
None
|
convert
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
New |
Raises:
| Type | Description |
|---|---|
(TypeError, ValueError, DigitopoError)
|
As for |
See Also
homotopic_thinning, lambda_kernel
Notes
Complexity: about O(N) time for N points.
References
[BEC97], [CBB01] section 4.1 (see the module documentation). Pink
functions: lhtkern (2D), mctopo3d_lhtkern3d (3D), command
htkern.
Examples:
A bump of height 40 with a peak of 90 has the topology of a point: its kernel is a single point, at the level of the peak.
>>> import numpy as np, digitopo
>>> image = np.zeros((7, 7), dtype=np.uint8)
>>> image[1:6, 1:6] = 40
>>> image[3, 3] = 90
>>> digitopo.grayscale.homotopic_kernel(image, 4)
array([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 90, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
lambda_kernel
¶
lambda_kernel(
image: ArrayLike,
connectivity: int,
lambda_: int,
condition: ArrayLike | None = None,
*,
convert: bool = False,
) -> NDArray[np.uint8]
Remove the minima and the peaks of contrast at most lambda_, then thin.
Lowers the lambda-destructible points above their condition value,
lowest level first, until none is left. The minima and the peaks of
contrast at most lambda_ disappear: this is a filter, not a
homotopic operator (for lambda_ = 0 it is homotopic). The result is
at most the image, and no lambda-destructible point above its condition
is left off the border (the border is never modified, see the module
documentation).
Note
Difference with Pink. Pink's lambdaskel command (function
llambdakern) misses points that stay lambda-destructible above the
condition: it starts its work from the neighbours of the regional
minima strictly above the condition only, so a minimum at the level of
the condition (every minimum at level 0 when there is no condition)
starts nothing, and a bump of height 40 on a background at 0 is left
unchanged. With a condition image, points frozen by the condition can
also cut a region off from the minima. pink26 fixes both by also
starting from every lambda-destructible point above its condition
value. The results of this function therefore differ from those of
Pink's lambdaskel; they are those of pink26.
Two behaviours of Pink are kept:
- a peak (a point with no neighbour at its level or above) is lowered
only when it is at most
lambda_above its highest lower neighbour: a peak of larger contrast stays; - the condition only chooses which points may be lowered (those above
it). A chosen point is lowered to the highest value of its lower
neighbours, and again while it stays lambda-destructible, even below
its condition value. So the result is not always above the
condition; only the points where
condition >= imageare guaranteed unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array_like
|
2D |
required |
connectivity
|
int
|
Connectivity of the minima: only 4 is available (Pink implements
no other). 8 and 3D images raise |
required |
lambda_
|
int
|
Contrast, in [0, 255]. A |
required |
condition
|
array_like
|
|
None
|
convert
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
New |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
DigitopoError
|
If |
See Also
homotopic_kernel
Notes
Complexity: as homotopic_kernel, plus a labelling of the minima.
References
[CBB01] section 5 (see the module documentation). Pink function:
llambdakern, command lambdaskel (corrected, see above).
Examples:
Two bumps on a background at 0, of height 90 and 30. With
lambda_ = 0 (a homotopic operator) one point is left for each bump.
With lambda_ = 30 the lambda-kernel erases the bump of height 30,
whose contrast is at most 30, and keeps the other:
>>> import numpy as np, digitopo
>>> image = np.zeros((5, 11), dtype=np.uint8)
>>> image[1:4, 1:4] = 90
>>> image[1:4, 7:10] = 30
>>> digitopo.grayscale.lambda_kernel(image, 4, 0)
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 90, 0, 0, 0, 0, 0, 30, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
>>> digitopo.grayscale.lambda_kernel(image, 4, 30)
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)