Skip to content

digitopo.interop

Conversions between the conventions of digitopo and those of scikit-image.

digitopo keeps the conventions of pink26, which are not those of scikit-image and SciPy on two points:

  • connectivity: digitopo names a connectivity by its number of neighbours (4 or 8 in 2D, 6, 18 or 26 in 3D, 8 or 80 in 4D) and always asks for it; scikit-image and scipy.ndimage.generate_binary_structure name it by the largest number of coordinates in which a neighbour differs, from 1 to ndim. connectivity and skimage_connectivity convert one into the other.
  • grey levels: the grey-level functions of digitopo take uint8 images, while scikit-image works on floats in [0, 1]. as_uint8 converts an image explicitly; the grey-level functions call it when they are given convert=True.

These functions are additions of digitopo; they have no equivalent in pink26.

connectivity

connectivity(ndim: int, skimage_connectivity: int) -> int

Return the digitopo connectivity of a scikit-image connectivity.

scikit-image (connectivity of skimage.measure.label, skimage.segmentation.watershed, skimage.morphology) and SciPy (scipy.ndimage.generate_binary_structure(ndim, connectivity)) count as neighbours the points that differ from the point by at most skimage_connectivity coordinates, each by 1. digitopo gives the number of these neighbours instead.

====== ========================== ====================== ndim scikit-image connectivity digitopo connectivity ====== ========================== ====================== 2 1, 2 4, 8 3 1, 2, 3 6, 18, 26 4 1, 4 8, 80 ====== ========================== ======================

The result is not always accepted by the function it is given to: 18 is accepted only by digitopo.watershed, and 4D only by digitopo.topology.simple_points.

Parameters:

Name Type Description Default
ndim int

Number of dimensions of the image, at least 1.

required
skimage_connectivity int

From 1 to ndim.

required

Returns:

Type Description
int

The number of neighbours of a point.

Raises:

Type Description
TypeError

If ndim or skimage_connectivity is not an integer.

ValueError

If ndim is below 1, or skimage_connectivity is not between 1 and ndim.

Examples:

>>> import digitopo
>>> digitopo.connectivity(2, 1), digitopo.connectivity(2, 2)
(4, 8)
>>> digitopo.connectivity(3, 3)
26

skimage_connectivity

skimage_connectivity(ndim: int, connectivity: int) -> int

Return the scikit-image connectivity of a digitopo connectivity.

The inverse of connectivity: for example, to label the objects whose topology digitopo counts with connectivity=8, call skimage.measure.label(mask, connectivity=skimage_connectivity(2, 8)).

Parameters:

Name Type Description Default
ndim int

Number of dimensions of the image, at least 1 (8 means two different neighbourhoods in 2D and in 4D).

required
connectivity int

A number of neighbours: 4 or 8 in 2D, 6, 18 or 26 in 3D...

required

Returns:

Type Description
int

From 1 to ndim.

Raises:

Type Description
TypeError

If ndim or connectivity is not an integer.

ValueError

If ndim is below 1, or connectivity is not a number of neighbours of the ndim-dimensional grid (4 or 8 in 2D, 6, 18 or 26 in 3D, ...).

Examples:

>>> import digitopo
>>> digitopo.skimage_connectivity(2, 8), digitopo.skimage_connectivity(3, 6)
(2, 1)

as_uint8

as_uint8(image: ArrayLike) -> NDArray[np.uint8]

Return a grey-level image as uint8, for the grey-level functions.

The conversion depends on the dtype, and refuses the values that it could only clip:

  • uint8: unchanged (no copy when the array is C-contiguous);
  • bool: False becomes 0 and True 255;
  • other integers: the values are grey levels, kept as they are; they must lie between 0 and 255;
  • floats: the convention of scikit-image, an intensity between 0 and 1 (skimage.util.img_as_float); v becomes rint(255 * v), as with skimage.util.img_as_ubyte. NaN and values outside [0, 1] are refused: rescale them first, for example with skimage.exposure.rescale_intensity(image, out_range=(0.0, 1.0)).

The 256 levels merge close values: a smooth gradient may get plateaus, where the place of a crest line depends on the order in which digitopo visits the points.

Parameters:

Name Type Description Default
image array_like

Array of any shape, of dtype bool, integer or float.

required

Returns:

Type Description
ndarray

C-contiguous uint8 array of the shape of image.

Raises:

Type Description
TypeError

For another dtype (complex, object...).

ValueError

For integers outside 0..255, or floats outside [0, 1] or NaN.

Examples:

>>> import numpy as np, digitopo
>>> digitopo.interop.as_uint8(np.array([0.0, 0.5, 1.0]))
array([  0, 128, 255], dtype=uint8)
>>> digitopo.interop.as_uint8(np.array([True, False]))
array([255,   0], dtype=uint8)