pysdic.quantize_image#

quantize_image(image, *, bit_depth)[source]#

Quantize the gray levels of an image (analog-to-digital conversion of a sensor).

\[g' = \text{clip}\left(\text{round}(g), \, 0, \, 2^{b} - 1\right)\]

The values are rounded to the nearest integer and clipped to \([0, 2^b - 1]\) (saturation of the sensor). The image is returned with an unsigned integer dtype: uint8 for \(b \leq 8\), uint16 otherwise.

See also

add_gaussian_noise() for the usual order of the simulation of an acquisition (the quantization is the last step).

Parameters:
  • image (ArrayLike) – The image, an array of any shape with finite values.

  • bit_depth (Integral) – The number of bits \(b \in [1, 16]\) of the quantization.

Returns:

The quantized image, a uint8 or uint16 array with the shape of image.

Return type:

numpy.ndarray

Raises:

ValueError – If the image has non-finite values or bit_depth is out of \([1, 16]\).

Examples

Simulation of the acquisition of a synthetic image (see pysdic.generate_synthetic_image()):

from pysdic import add_gaussian_blur, add_gaussian_noise, add_poisson_noise, quantize_image

acquired = add_gaussian_blur(image[..., 0], sigma=0.5)
acquired = add_poisson_noise(acquired, gain=0.1, seed=0)
acquired = add_gaussian_noise(acquired, std=1.0, seed=1)
acquired = quantize_image(acquired, bit_depth=8)  # uint8 (H, W)