Scikit-image 简明教程
Scikit Image - Image datatypes
datatype 在计算机编程的上下文中,是指根据数据的属性及其可以执行的操作对数据进行分类或分类。它决定了计算机如何解释、存储和处理数据。
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
在 scikit-image 中,图像表示为 numpy 数组,并支持各种数据类型,也称为“dtypes”。库为 dtype 设置特定范围以避免图像强度失真。以下是 scikit-image 中常用的 dtypes 及其相应范围 −
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.
请注意,尽管浮点数据类型本身可以超出此范围,但浮点图像通常应限制在 -1 到 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
在 scikit-image 中, skimage.util 模块中提供了一些函数,用于转换图像数据类型并确保图像强度的适当重新缩放。这些函数旨在在保留图像数据范围的同时处理转换和重新缩放。以下是在 scikit-image 中的图像数据类型转换函数−
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
这些函数提供了一种便捷的方法,用于在保持正确强度范围的同时将图像转换为所需的数据类型。此外,请务必避免直接在图像上使用 astype 函数,因为它可能违反与数据类型范围有关的假设。相反,您可以使用上面的转换函数来确保适当的数据类型转换和强度重新缩放。
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
以下示例演示了在 scikit-image 中使用 astype() 方法和 img_as_float() 函数转换图像数组的数据类型之间的差异。
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))
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]]
通过使用 img_as_float() 函数,可以将图像数组正确转换为浮点数据类型,并且强度值按有效范围正确缩放。
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
以下示例演示了使用 skimage.util 模块中的 img_as_ubyte() 函数将浮点图像数组转换为 8 位无符号整数表示。
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))
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]