skimage2.util#

Warning

This module is part of the experimental skimage2 namespace and is subject to change without notice. Do not use it in production code. See the migration guide for more details.

Utility functions to work with images in general.

_prescale_value_range

Rescale the value range of image according to the selected mode.

rescale_legacy

Rescale value range based on dtype (legacy skimage behavior).

rescale_minmax

Min-max normalize image values to the range [0., 1.].


skimage2.util._prescale_value_range(image, *, mode)[source]#

Rescale the value range of image according to the selected mode.

For now, this private function handles prescaling (prescale parameter) for public API that needs a value range to be known and well-defined.

Parameters:
imagendarray

Image to rescale.

mode{‘minmax’, ‘none’, ‘legacy’}, optional

Controls the rescaling behavior for image.

'minmax'

Normalize image between 0 and 1 regardless of dtype. After normalization, rescaled_image will have a floating dtype (according to _supported_float_type()).

'none'

Don’t rescale the value range of image at all and return a copy of image. Useful when image has already been rescaled.

'legacy'

Normalize only if image has an integer dtype. If image is of floating dtype, it is left alone. See img_as_float() for more details.

Returns:
rescaled_imagendarray

The rescaled image of the same shape but possibly with a different dtype.

Raises:
ValueError

Rescaling an image with mode='minmax' that contains NaN or infinity is not supported for now. In those cases, consider replacing the unsupported values manually.

Examples

>>> import numpy as np
>>> image = np.array([-10, 45, 100], dtype=np.int8)
>>> _prescale_value_range(image, mode="minmax")
array([0. , 0.5, 1. ])
>>> _prescale_value_range(image, mode="legacy")
array([-0.07874016,  0.35433071,  0.78740157])
>>> _prescale_value_range(image, mode="none")
array([-10,  45, 100], dtype=int8)

skimage2.util.rescale_legacy(image)[source]#

Rescale value range based on dtype (legacy skimage behavior).

Parameters:
imagendarray

Input image.

Returns:
rescaled_imagendarray

Rescaled image, of same shape as input image but with a floating dtype (See Notes).

See also

rescale_minmax

Rescale image to the value range [0, 1].

Notes

Rescales the value range according to the dtype of image according to the same logic as the legacy function skimage.util.img_as_float().

  • With a signed integer dtype, image is rescaled to the range [0., 1.].

  • With an unsigned integer dtype, image is rescaled to the range [-1., 1.].

  • With a floating dtype, the output range will not be modified; the range can be outside the above ranges.

Examples

Signed integers are scaled to range [-1., 1.] >>> rescale_legacy(np.array([-128, 0, 127], dtype=np.int8)) array([-1., 0., 1.])

Unsigned integers are scaled to range [0., 1.] >>> rescale_legacy(np.array([0, 127, 255], dtype=np.uint8)) array([0. , 0.49803922, 1. ])

Range of floating input is preserved >>> rescale_legacy(np.array([0, 127, 255], dtype=float)) array([ 0., 127., 255.])


skimage2.util.rescale_minmax(image)[source]#

Min-max normalize image values to the range [0., 1.].

Parameters:
imagendarray

Input image.

Returns:
rescaled_imagendarray

Rescaled image, of same shape as input image but with a floating dtype. If image has an integer dtype this will NumPy’s default float. If image already has a floating dtype it will be preserved.

Raises:
ValueError

NaN and infinity values are not supported. Replace such values before rescaling.

See also

rescale_legacy

Rescale value range based on dtype (legacy skimage behavior).

References

Examples

>>> import numpy as np
>>> image = np.array([-10, 45, 100], dtype=np.int8)
>>> rescale_minmax(image)
array([0. , 0.5, 1. ])