Scikit-image 简明教程
Scikit Image - Reading Images
在使用图像处理工具执行裁剪、调整大小、旋转或应用滤镜等操作时,读取图像是一项基本步骤。读取图像的过程涉及获取图像文件中的像素值和元数据,并将它们表示为合适的数据结构,例如 NumPy 数组或矩阵。
Reading images is a fundamental step when performing operations like cropping, resizing, rotating, or applying filters using image processing tools. The process of reading images involves capturing the pixel values and metadata from an image file and representing them in a suitable data structure, such as a NumPy array or matrix.
在 Scikit-image 库中,io 模块提供了一个 imread() 函数,用于将图像文件作为 NumPy 数组加载到内存中。一旦将图像作为一个 NumPy 数组加载到内存中,我们就可以访问 Scikit-image 中提供的各种图像处理函数,以执行过滤、分割、特征提取等任务。
In the Scikit-image library, the io module provides a function imread() for loading image files into memory as NumPy arrays. Once the image is loaded into memory as a NumPy array, we can get access to a wide range of image processing functions available in Scikit-image to perform tasks like filtering, segmentation, feature extraction, and much more.
The imread() method
Scikit-image 中的 io.imread() 方法能够读取各种格式的图像,包括 JPEG、PNG、TIFF、BMP 等。此函数在内部根据特定图像格式(如 imageio、PIL 或 tifffile)使用不同的库。
The io.imread() method in Scikit-image is capable of reading images in various formats, including JPEG, PNG, TIFF, BMP, and more. The function internally utilizes different libraries based on the specific image format, such as imageio, PIL, or tifffile.
Syntax
以下是这种方法的语法和参数−
Following is the syntax and parameters of this method −
skimage.io.imread(fname, as_gray=False, plugin=None, **plugin_args)
-
Fname − A string representing the file name or path or a URL of the image.
-
as_gray (optional) − If set to True, it converts color images to grayscale represented as 64-bit floats. Images that are already in grayscale format are not converted.
-
plugin (optional) − A string specifying the plugin to use for reading the image. If the plugin parameter is not provided, the function will automatically try different plugins, starting with imageio, until a suitable one is found. However, if the file name (fname) has a ".tiff" extension, the tifffile plugin will be used by default.
-
**plugin_args (optional) − Additional keyword arguments that are passed to the specified plugin.
Return Value
该方法返回一个表示图像的 NumPy 数组。该数组包含像素值,其形状对应于图像的尺寸。图像的颜色波段或通道存储在数组的第三维中。
The method returns a NumPy array that represents the image. The array contains the pixel values, and its shape corresponds to the dimensions of the image. The color bands or channels of the image are stored in the third dimension of the array.
例如,灰度图像的尺寸为 MxN, ,RGB 图像的尺寸为 MxNx3, ,RGBA 图像的尺寸为 MxNx4. 。
For instance, a grayscale image would have dimensions MxN, an RGB image would have dimensions MxNx3, and an RGBA image would have dimensions MxNx4.
Example 1
以下示例演示了如何使用文件名加载图像。
The following example demonstrates how to load an image using the file name.
import skimage
from skimage import io
# Read an image
image = io.imread('Images/logo.png')
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Image shape:", image.shape)
print("Image data type:", image.dtype)
print("Number of color channels:", image.shape[2])
The following are the properties of the loaded image:
Image shape: (225, 225, 4)
Image data type: uint8
Number of color channels: 4
Example 2
以下示例演示了如何使用图像 URL 加载图像。
The following example demonstrates how to load an image using the image URL.
import skimage
from skimage import io
# Read an image
image = io.imread('https://www.tutorialspoint.com/images/logo.png')
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Image shape:", image.shape)
print("Image data type:", image.dtype)
print("Number of color channels:", image.shape[2])
The following are the properties of the loaded image:
Image shape: (140, 568, 3)
Image data type: uint8
Number of color channels: 3
Example 3
以下示例演示了如何使用 imread() 方法的 as_gray 参数将彩色图像转换为灰度。
The following example demonstrates how to convert color images to grayscale by using the as_gray parameter of the imread() method.
import skimage
from skimage import io
# Read an image
image = io.imread('https://www.tutorialspoint.com/images/logo.png', as_gray=True)
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Image shape:", image.shape)
print("Image data type:", image.dtype)
The following are the properties of the loaded image:
Image shape: (140, 568)
Image data type: float64