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.
Rescale the value range of |
|
Rescale value range based on dtype (legacy |
|
Min-max normalize image values to the range [0., 1.]. |
- skimage2.util._prescale_value_range(image, *, mode)[source]#
Rescale the value range of
imageaccording to the selectedmode.For now, this private function handles prescaling (
prescaleparameter) 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
imagebetween 0 and 1 regardless of dtype. After normalization,rescaled_imagewill have a floating dtype (according to_supported_float_type()).'none'Don’t rescale the value range of
imageat all and return a copy ofimage. Useful whenimagehas already been rescaled.'legacy'Normalize only if
imagehas an integer dtype. Ifimageis of floating dtype, it is left alone. Seeimg_as_float()for more details.
- Returns:
- rescaled_imagendarray
The rescaled
imageof the same shape but possibly with a different dtype.
- Raises:
- ValueError
Rescaling an
imagewithmode='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
skimagebehavior).- Parameters:
- imagendarray
Input image.
- Returns:
- rescaled_imagendarray
Rescaled image, of same shape as input
imagebut with a floating dtype (See Notes).
See also
rescale_minmaxRescale
imageto the value range [0, 1].
Notes
Rescales the value range according to the dtype of
imageaccording to the same logic as the legacy functionskimage.util.img_as_float().With a signed integer dtype,
imageis rescaled to the range [0., 1.].With an unsigned integer dtype,
imageis 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
imagebut with a floating dtype. Ifimagehas an integer dtype this will NumPy’s default float. Ifimagealready 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_legacyRescale value range based on dtype (legacy
skimagebehavior).
References
Examples
>>> import numpy as np >>> image = np.array([-10, 45, 100], dtype=np.int8) >>> rescale_minmax(image) array([0. , 0.5, 1. ])