Mahotas 简明教程
Mahotas - Handling Images
Mahotas 是一个流行的 Python 包,用于处理图像并在其上执行各种操作。它可以对图像执行许多操作,例如将它们分成不同的部分、查找边缘和识别其中的对象。
Mahotas is a popular Python package for handling images and performing various operations on them. It can do many things with images, like separating them into different parts, finding edges, and recognizing objects in them.
使用 Mahotas,我们可以查找一组图片中的所有面孔,或识别一组图像中不同类型的花卉。我们还可以找到图片中对象边缘或在 Mahotas 中使模糊的图片更清晰。
With Mahotas, we can find all the faces in a set of pictures, or identify different types of flowers in a collection of images. We can also find the edges of objects in pictures, or make blurry pictures clearer in Mahotas.
在本教程中,我们将简要概述如何使用 Mahotas 处理图像,包括如何读取、写入和显示图像。我们还将学习如何执行常见的图像处理任务,例如过滤、分割和特征提取。
In this tutorial, we will take a brief overview of how to use Mahotas to handle images, including how to read, write, and display images. We will also learn how to perform common image processing tasks such as filtering, segmentation, and feature extraction.
Image Handling with Mahotas
Mahotas 的一个主要特点是能够处理各种格式的图像,例如 JPEG、PNG 和 BMP。Mahotas 为我们提供了用于读取和写入图像以及在不同格式之间转换图像的函数。
One of the key features of Mahotas is its ability to handle images in various formats, such as JPEG, PNG, and BMP. Mahotas provides us with functions for reading and writing images, as well as for converting images between different formats.
让我们学习如何在 Mahotas 中读取图像。
Let us learn how to read an image in Mahotas.
Reading Images
在 mahotas 中读取图像是指从文件中加载图像数据的过程。要在 Mahotas 中读取图像,我们可以使用 imread() 函数。该函数从文件中读取图像并返回一个表示图像的 NumPy 数组。
Reading images in mahotas refers to the process of loading image data from a file. To read an image in Mahotas, we can use the imread() function. This function reads an image from a file and returns a NumPy array representing the image.
Example
在下面的代码中,我们尝试读取名为“nature.jpeg”的 JPEG 图像 -
In the code below, we are trying to read a JPEG image named 'nature.jpeg' −
import mahotas as ms
image = ms.imread('nature.jpeg')
print ("The image is read.")
这将从文件“nature.jpeg”读取图像并将其存储在变量“image”中 -
This will read the image from the file 'nature.jpeg' and store it in the variable 'image' −
The image is read.
读完图像后,我们还可以按照以下讨论内容将其显示。
We can also display the image once we have read it as discussed below.
Displaying an Image
在您读完图像后,我们可以使用 matplotlib 库将其显示。matplotlib 库用于数据可视化和绘图。
Once you have read an image, we can display it using the matplotlib library. The matplotlib library is used for data visualization and plotting.
Example
让我们使用 matplotlib 库显示图像“nature.jpeg”,如下所示:
Let us display the image 'nature.jpeg, using matplotlib library as shown below −
import matplotlib.pyplot as plt
import mahotas as ms
image = ms.imread('nature.jpeg')
plt.imshow(image)
plt.show()
以下是执行上述代码时得到的图像:
Following is the image obtained while executing the above code−
Writing Images
Mahotas 中的图像写入是指将图像数据以特定图像格式(如 PNG、JPEG、BMP、TIFF 等)保存到文件中。我们可以使用 imsave() 函数在 Mahotas 中写入图像。此函数是 mahotas 中的图像输入/输出 (IO) 模块的一部分。
Writing images in Mahotas refers to saving the image data to a file in a specific image format such as PNG, JPEG, BMP, TIFF, etc. We can use the imsave() function to write an image in Mahotas. This function is part of the image input/output (IO) module in mahotas.
Example
在下面的代码中,我们尝试保存名为“nature.jpeg”的图像:
In the code below, we are trying to save an image named 'nature.jpeg' −
import mahotas as ms
image = ms.imread('nature.jpeg')
print ("The image is read.")
# Write the image to a file
ms.imsave('writing.jpeg', image)
print ("The image data is saved.")
这会将图像保存到文件“writing.jpeg”中:
This will save the image to a file 'writing.jpeg' −
The image is read.
The image data is saved.
Image Processing with Mahotas
图像处理是指用于对图像执行多项操作的一系列技术。这些技术有助于提高图像的视觉质量、提取有用的信息或为分析准备图像。
Image processing refers to a set of techniques which is used to perform several operations on images. These techniques are helpful in improving the visual quality of images, extract useful information, or prepare them for analysis.
一些常见的图像处理任务是滤波、分割和特征提取。在本节中,我们将探讨 Mahotas 提供的一些关键图像处理函数。
Some common image processing tasks are filtering, segmentation, and feature extraction. In this section, we will explore some of the key image processing functions provided by Mahotas.
Filtering
在 mahotas 中,滤波是指修改图像的外观或从中提取有用的信息。这是通过对图像应用数学运算来完成的。滤波流程有助于去除噪声、平滑图像、增强边缘或执行其他操作,以帮助提高图像的质量或可解释性。
In mahotas, filtering refers to modifying the appearance of an image or extracting useful information from it. This is done by applying a mathematical operation to an image. The filtering process is useful in removing noise, smoothening an image, enhancing edges, or performing other operations that help improve the quality or interpretability of an image.
在下面的示例中,我们尝试使用高斯滤波器平滑图像。高斯滤波器模糊图像,可用于降低噪声或平滑图像:
In the following example, we are trying to smooth an image using a Gaussian filter. The Gaussian filter blurs an image, which can be used to reduce noise or smooth an image −
import mahotas as ms
import numpy as np
import matplotlib.pyplot as mtplt
# Loading an image and converting it to grayscale
image = ms.imread('nature.jpeg', as_grey=True)
# Applying a Gaussian filter
# Standard deviation
sigma = 15
gaussian_filtered = ms.gaussian_filter(image, sigma)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the filtered image
axes[1].imshow(gaussian_filtered, cmap='gray')
axes[1].set_title('Gaussian Filtered')
axes[1].axis('off')
mtplt.show()
这会使用标准差为 15 的高斯滤波器平滑原始图像,如下所示:
This will smooth the original image using a Gaussian filter with a standard deviation of 15 as shown below −
Segmentation
mahotas 中的分割是指根据某些特征或标准将图像划分为有意义的区域或部分的过程。这些部分可以表示不同的对象、感兴趣区域或图像中的不同区域。
Segmentation in mahotas refers to the process of dividing an image into meaningful regions or segments based on certain characteristics or criteria. These segments can represent different objects, regions of interest, or distinct areas within the image.
现在,让我们通过 Mahotas 阈值处理来了解图像分割的基本示例。阈值处理用于根据像素强度值将对象从背景中分离出来。它通过将图像转换为二进制图像来简化图像,其中每个像素被归类为前景(对象)或背景:
Now, let’s go through a basic example of image segmentation using thresholding with Mahotas. Thresholding is used to separate objects from the background based on pixel intensity values. It simplifies an image by converting it into a binary image where eachpixel is classified as either foreground (object) or background −
import mahotas as ms
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = ms.imread('nature.jpeg', as_grey=False)
# Performing thresholding
# Calculating threshold value by taking mean of image
threshold = np.mean(image)
# creating binary image
# comparing each pixel value with the threshold
binary_image = image > threshold
# Perform connected component analysis
# assigns a unique label to each connected region in the binary image
labeled_image, num_objects = ms.label(binary_image)
# Displaying the original image
fig, ax = mtplt.subplots(1, 2, figsize=(9, 4))
ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original Image')
# Displaying the segmented image
ax[1].imshow(labeled_image, cmap='rainbow')
ax[1].set_title('Segmented Image ({} objects)'.format(num_objects))
mtplt.show()
以上代码的输出如下所示 −
The output of the above code is as shown below −
Feature Extraction
Mahotas 中的特征提取是指从图像中提取有意义且有益特征的过程。这些特征可以表示图像的各个方面,如纹理、形状或颜色,并且可用于描述和区分图像中的对象或区域。
Feature extraction in Mahotas refers to the process of extracting meaningful and informative features from an image. These features can represent various aspects of the image, such as texture, shape, or color, and can be used to describe and differentiate objects or regions within the image.
现在,让我们看一个使用 Mahotas 计算 Zernike 矩的示例。Zernike 矩是一组数值,描述图像中对象或区域的形状。它们通过捕获对象的轮廓和空间特征,来提供对象的形状的紧凑表示:
Now, let’s see an example of how to calculate Zernike moments using Mahotas. The Zernike moments are a set of numerical values that describe the shape of an object or region within an image. They provide a compact representation of the object’s shape by capturing its contours and spatial characteristics −
import mahotas as ms
from mahotas.features import zernike_moments
# Loading the image
image = ms.imread('nature.jpeg', as_grey=True)
# Normalizing the image
normalized_image = ms.stretch(image)
# Specifying the radius for Zernike moments calculation
radius = 30
# Calculating Zernike moments
moments = zernike_moments(normalized_image, radius)
# Printing the resulting moments
print(moments)
在执行上述代码后,我们会得到一组数值,这些数值描述了图像的形状特征,如下所示:
After executing the above code, we get a set of numerical values that describe the shape features of the image as shown below −
[0.31830989 0.01252728 0.00854837 0.00677556 0.00239216 0.00742469
0.00553204 0.00306795 0.01058855 0.00368774 0.00456233 0.01123103
0.00336877 0.00349998 0.00387494 0.0108442 0.00294575 0.00490895
0.00243368 0.01113736 0.00229705 0.00511743 0.00668574 0.0083512
0.00767699]