Mahotas 简明教程
Mahotas - Loading an Image
若要在图像上执行任何操作,首先需要将其加载到内存中。一旦加载图像,即可访问其像素并在其上应用各种操作。
To perform any operation on an image, we first need to load it into memory. Once the image is loaded, we can access its pixels and apply various operations on it.
加载图像是指从存储设备(例如硬盘、USB 驱动器或网络位置)中读取图像文件并将其加载到内存中。
Loading an image refers to reading an image file from a storage device (such as hard drive, USB drive, or network location) into memory.
Loading an Image in Mahotas
若要使用 Mahotas 加载图像,可使用 imread() 函数,其会读取图像文件并将其作为 NumPy 数组返回。
To load an image using Mahotas, we can use the imread() function, which reads an image file and returns it as a NumPy array.
NumPy 数组是一个网格值,所有值类型相同,且由非负整数元组编制索引。对于图像,该数组表示图像的像素值。
A NumPy array is a grid of values, all of the same type, and indexed by a tuple of nonnegative integers. In the case of an image, the array represents the pixel values of the image.
Using the imread() Function
在 Mahotas 中,imread() 函数是读取加载图像的核心方法。其接受文件路径作为输入,返回表示已加载图像的 NumPy 数组。此函数可以读取各种图像文件格式,如 JPEG、PNG、BMP、TIFF 等。
The imread() function is the core method in Mahotas for reading and loading images. It accepts a file path as input and returns a NumPy array representing the loaded image. This function can read various image file formats, such as JPEG, PNG, BMP, TIFF, etc.
以下是 Mahotas 中 imread() 函数的基本语法:
Following is the basic syntax of imread() function in Mahotas −
mahotas.imread('image.file_format')
其中, 'image.file_format' 是要加载的图像的实际路径和格式。
Where, 'image.file_format' is the actual path and format of the image you want to load.
在以下示例中,我们使用 imread() 函数从当前目录中加载名为 "nature.jpeg" 的图像文件。结果图像存储在 'image' 变量中,作为 NumPy 数组 −
In the following example, we are using the imread() function to load an image file named "nature.jpeg" from the current directory. The resulting image is stored in the 'image' variable as a NumPy array −
import mahotas as mh
from pylab import imshow, show
# Loading the image using Mahotas
image = mh.imread('nature.jpeg')
# displaying the original image
imshow(image)
show()
上述代码的输出如下:
Output of the above code is as follows −
Loading Different Image Formats
图像格式是指用于以数字方式存储和编码图像的不同文件格式。每种格式都有自己的规范、特性和压缩方法。
The image format refers to the different file formats used to store and encode images digitally. Each format has its own specifications, characteristics, and compression methods.
Mahotas 提供了广泛的图像格式,包括 JPEG、PNG、BMP、TIFF 和 GIF 等常见格式。我们可以将任何这些格式中的图像文件的路径传递给 imread() 函数。
Mahotas provides a wide range of image formats, including common formats like JPEG, PNG, BMP, TIFF, and GIF. We can pass the file path of an image in any of these formats to the imread() function.
Example
在此示例中,我们通过使用 imread() 函数加载不同格式的图像来演示 Mahotas 的通用性。每张加载的图像都存储在单独的变量中 −
In this example, we demonstrate the versatility of Mahotas by loading images in different formats using the imread() function. Each loaded image is stored in a separate variable −
import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading JPEG image
image_jpeg = ms.imread('nature.jpeg')
# Loading PNG image
image_png = ms.imread('sun.png')
# Loading BMP image
image_bmp = ms.imread('sea.bmp')
# Loading TIFF image
image_tiff = ms.imread('tree.tiff')
# Creating a figure and subplots
fig, axes = mtplt.subplots(2, 2)
# Displaying JPEG image
axes[0, 0].imshow(image_jpeg)
axes[0, 0].axis('off')
axes[0, 0].set_title('JPEG Image')
# Displaying PNG image
axes[0, 1].imshow(image_png)
axes[0, 1].axis('off')
axes[0, 1].set_title('PNG Image')
# Displaying BMP image
axes[1, 0].imshow(image_bmp)
axes[1, 0].axis('off')
axes[1, 0].set_title('BMP Image')
# Displaying TIFF image
axes[1, 1].imshow(image_tiff)
axes[1, 1].axis('off')
axes[1, 1].set_title('TIFF Image')
# Adjusting the spacing and layout
mtplt.tight_layout()
# Showing the figure
mtplt.show()
显示的图像如下 −
The image displayed is as follows −
Loading Color Images
彩色图像指的是我们通常看到的图像,其中包含各种颜色。它们由三个颜色通道组成——红色、绿色和蓝色。每个像素的颜色由这三个通道的组合决定。彩色图像可以表示广泛的颜色范围,并且与我们肉眼看到的内容类似。
The color images are the ones we typically see, containing various colors. They are composed of three color channels− red, green, and blue. Each pixel’s color is determined by the combination of these three channels. Color images can represent a wide range of colors and are similar to what we see with our eyes.
在 Mahotas 中加载彩色图像意味着读取包含颜色信息的图像文件。结果图像表示为 3D 数组,其中每个元素代表红色、绿色和蓝色通道中每个像素的颜色值。
Loading color images in Mahotas means reading an image file that contains color information. The resulting image is represented as a 3D array, where each element represents the color values for each pixel in the red, green, and blue channels.
以下是 Mahotas 中加载灰度图像的基本语法 −
Following is the basic syntax for loading grayscale Images in Mahotas −
mahotas.imread('image.file_format')
其中, 'image.file_format' 是要加载的图像的实际路径和格式。
Where, 'image.file_format' is the actual path and format of the image you want to load.
Example
以下是加载彩图的方法 −
Following is an example of loading a color image in Mahotas −
import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading colored image
colored_image = ms.imread('nature.jpeg')
# Displaying colored image
mtplt.imshow(colored_image)
mtplt.axis('off')
mtplt.show()
上述代码的输出如下:
Output of the above code is as follows −
Loading Color and Grayscale image
要在 mahotas 中加载灰度图像,我们需要将 'as_grey=True' 参数传递给 imread() 函数。
To load a grayscale image in mahotas, we need to pass the 'as_grey=True' parameter to the imread() function.
Example
在以下示例中,我们尝试使用 Mahotas 同时加载灰度图像和彩色图像 −
In the following example, we are trying to load a grayscale image and a color image together using Mahotas −
import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading color image
color_image = ms.imread('tree.tiff')
# Loading grayscale image
grayscale_image = ms.imread('tree.tiff', as_grey=True)
# Creating a figure and subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying colored image
axes[0].imshow(color_image)
axes[0].axis('off')
axes[0].set_title('Colored Image')
# Displaying grayscale image
axes[1].imshow(grayscale_image, cmap='gray')
axes[1].axis('off')
axes[1].set_title('Grayscaled Image')
# Adjusting the spacing and layout
mtplt.tight_layout()
# Showing the figure
mtplt.show()
输出如下所示:
The output is as shown below −