Scikit Image - Image datatypes



A datatype, in the context of computer programming, refers to the classification or categorization of data based on its properties and the operations that can be performed on it. It determines how the computer interprets, stores, and manipulates the data.

Different programming languages have their own set of data types, and each data type has its properties. Common data types include integers, floating-point numbers, characters, strings, booleans, and arrays.

Datatypes in Python Scikit Image

In scikit-image, images are represented as numpy arrays and support a variety of data types, also known as "dtypes." The library sets certain ranges for dtype to avoid distorting image intensities. The following are the commonly used dtypes and their corresponding ranges in scikit-image −

  • uint8 − Unsigned 8-bit integer, ranging from 0 to 255.
  • uint16 − Unsigned 16-bit integer, ranging from 0 to 65535.
  • uint32 − Unsigned 32-bit integer, ranging from 0 to 2^32 - 1.
  • float − Floating-point values, typically ranging from -1 to 1 or 0 to 1.
  • int8 − Signed 8-bit integer, ranging from -128 to 127.
  • int16 − Signed 16-bit integer, ranging from -32768 to 32767.
  • int32 − Signed 32-bit integer, ranging from -2^31 to 2^31 - 1.

Note that float images should typically be restricted to the range -1 to 1, even though the float dtype itself can exceed this range. On the other hand, integer dtypes can span the entire range of their respective data types. It's important to stick to these ranges to avoid data loss or incorrect interpretations of pixel intensities.

Image data type conversion functions

In scikit-image, there are few functions available in skimage.util module to convert image data types and ensure the proper rescaling of image intensities. These functions are designed to handle the conversion and rescaling while preserving the data range of the image. Following are the image data type conversion functions in scikit-image −

  • Img_as_float
  • Img_as_ubyte
  • Img_as_uint
  • Img_as_int

These functions provide a convenient way to convert images to the desired data type while maintaining the correct range of intensities. Also, it is important to avoid using the astype function directly on an image, as it can violate assumptions about the dtype range. Instead, you can use the above conversion functions to ensure proper dtype conversion and intensity rescaling.

Example 1

The following example demonstrates the difference between using the astype() method and the img_as_float() function for converting the data type of an image array in scikit-image.

from skimage import util
import numpy as np

# Create an image with 8-bit unsigned integers
image = np.random.randint(0, 256, size=(1, 4), dtype=np.uint8)

print("Image array:", image)

# Convert the image to float using astype()
print('Converted to float using astype :',image.astype(float)) # These float values are out of range.

# Convert the image to float using img_as_float()
print("Converted to float using img_as_float:",util.img_as_float(image))

Output

Image array: [[173 104 167  25]]
Converted to float using astype : [[173. 104. 167.  25.]]
Converted to float using img_as_float: [[0.67843137 0.40784314 0.65490196 0.09803922]]

By using the img_as_float() function, the image array is correctly converted to the floating point data type with the intensity values properly scaled within the valid range.

This ensures proper datatype conversion and intensity rescaling.

Example 2

The following example demonstrates the conversion of a floating-point image array to an 8-bit unsigned integer representation using the img_as_ubyte() function from the skimage.util module.

from skimage import util
import numpy as np

# Create an image with floating point numbers
image = np.array([0, 0.1, 0, 0.8, 0.3, 1], dtype=float)

print("Image array:", image)

# Convert the image data to 8-bit Unsigned integers
print("Converted to 8-bit uint using img_as_ubyte:",util.img_as_ubyte(image))

Output

Image array: [0. 0.1 0. 0.8 0.3 1.]
Converted to 8-bit uint using img_as_ubyte: [0 26 0 204 76 255]
Advertisements