Scikit-image 简明教程
Scikit Image - Numpy Images
NumPy (也称为“Numerical Python”)是 Python 中用于数值计算的其中一个最重要的基本软件包。NumPy 的核心数据结构是 ndarray(N 维数组),它是同种数据类型中同类元素的集合。这些数组可以是任何维度,如 1D、2D 甚至更高维度的数组。NumPy 提供大量数学函数,可以高效地对这些 N 维数组进行操作。
NumPy (also known as "Numerical Python") is one of the most crucial fundamental packages in Python for numerical computing. The core data structure of NumPy is the ndarray (N-dimensional array), which is a homogeneous collection of elements of the same data type. These arrays can be of any dimension, such as 1D, 2D, or even higher-dimensional arrays. NumPy provides a vast collection of mathematical functions to operate on these N-dimensional arrays efficiently.
scikit-image 中的 Images 表示为 NumPy ndarrays(多维数组)。scikit-image 库基于 NumPy 构建,且使用 NumPy 数组来表示图像。因此,scikit-image 库可以有效地执行各种图像处理任务。
Images in scikit-image are represented as NumPy ndarrays (multidimensional arrays). The scikit-image library is built on top of NumPy and it uses the NumPy arrays to represent images. Hence, the scikit-image library can perform various image-processing tasks effectively.
Representing images as NumPy arrays
使用 NumPy 数组表示图像提供了存储和处理图像数据的便捷且高效的方式。
Representing images as NumPy arrays, provide a convenient and efficient way to store and manipulate image data.
这里,NumPy 数组的维度对应于图像维度,如高、宽和色彩通道。对于灰度图像,数组通常是二维的(高 x 宽)。对于彩色图像,数组是三维的(高 x 宽 x 3),其中最后一维表示红色、绿色和蓝色色彩通道。
Here, the dimensions of the NumPy array correspond to the image dimensions, such as height, width, and color channels. For grayscale images, the array is typically two-dimensional (height x width). For color images, the array is three-dimensional (height x width x 3), where the last dimension represents the Red, Green, and Blue color channels.
Example 1
以下示例演示了如何在 scikit-image 中将彩色图像表示为 NumPy 数组。
The following example demonstrates how a color image can be represented as a NumPy array in scikit-image.
from skimage import io
# Read an image as a grayscale image
img_array = io.imread('Images/Dog.jpg')
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Data type of the image object:", type(img_array))
print("Image shape:", img_array.shape)
print("Image data type:", img_array.dtype)
The following are the properties of the loaded image:
Data type of the image object: <class 'numpy.ndarray'>
Image shape: (479, 500, 3)
Image data type: uint8
Example 2
让我们看看灰度图像的 NumPy 数组表示方式。
Let’s see the NumPy array representation of a grayscale image.
from skimage import io
# Read an image as a grayscale image
img_array = io.imread('Images/dog.jpg', as_gray=True)
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Data type of the image object:", type(img_array))
print("Image shape:", img_array.shape)
print("Image data type:", img_array.dtype)
The following are the properties of the loaded image:
Data type of the image object: <class 'numpy.ndarray'>
Image shape: (479, 500)
Image data type: float64
Indexing and Slicing
可以利用 NumPy 的索引和切片特性来访问和操作图像数据。通过使用 NumPy 的灵活索引和切片语法,可以裁剪图像、选择特定的色彩通道或对图像中特定区域应用操作。
NumPy’s indexing and slicing feature can be used to access and manipulate image data. Croping images, selecting specific color channels, or applying operations to specific regions within the image can be possible by using NumPy’s flexible indexing and slicing syntax.
Example
以下示例演示如何在 Scikit-image 中使用 NumPy 的索引和切片语法来修改图像。
The following example demonstrates how the indexing and slicing syntax of NumPy can be used to modify an image in Scikit-image.
from skimage import io
# Read an image as a grayscale image
img_array = io.imread('Images/Tajmahal.jpg')
# Get the value of the pixel at the 10th row and 20th column
pixel_value = img_array[10, 20]
print('The pixel at the 10th row and 20th column of the image array', pixel_value)
# Set value 0 to the pixel at the 3rd row and 10th column
img_array[3, 10] = 0
# Select a region in the image
roi = img_array[100:200, 200:300]
# Set the pixel values in the selected region to red (255, 0, 0)
roi[:] = (255, 0, 0)
# Display the modified image
io.imshow(img_array)
io.show()
运行以上代码将产生以下结果 -
Running the above code gives us the following result −
The pixel at the 10th row and 20th column of the image array [ 81 97 110]
此外,它生成以下图像:
In addition it generates the following image −