Mahotas 简明教程

Mahotas - Handling Images

Mahotas 是一个流行的 Python 包,用于处理图像并在其上执行各种操作。它可以对图像执行许多操作,例如将它们分成不同的部分、查找边缘和识别其中的对象。

使用 Mahotas,我们可以查找一组图片中的所有面孔,或识别一组图像中不同类型的花卉。我们还可以找到图片中对象边缘或在 Mahotas 中使模糊的图片更清晰。

在本教程中,我们将简要概述如何使用 Mahotas 处理图像,包括如何读取、写入和显示图像。我们还将学习如何执行常见的图像处理任务,例如过滤、分割和特征提取。

Image Handling with Mahotas

Mahotas 的一个主要特点是能够处理各种格式的图像,例如 JPEG、PNG 和 BMP。Mahotas 为我们提供了用于读取和写入图像以及在不同格式之间转换图像的函数。

让我们学习如何在 Mahotas 中读取图像。

Reading Images

在 mahotas 中读取图像是指从文件中加载图像数据的过程。要在 Mahotas 中读取图像,我们可以使用 imread() 函数。该函数从文件中读取图像并返回一个表示图像的 NumPy 数组。

Example

在下面的代码中,我们尝试读取名为“nature.jpeg”的 JPEG 图像 -

import mahotas as ms
image = ms.imread('nature.jpeg')
print ("The image is read.")

这将从文件“nature.jpeg”读取图像并将其存储在变量“image”中 -

The image is read.

读完图像后,我们还可以按照以下讨论内容将其显示。

Displaying an Image

在您读完图像后,我们可以使用 matplotlib 库将其显示。matplotlib 库用于数据可视化和绘图。

Example

让我们使用 matplotlib 库显示图像“nature.jpeg”,如下所示:

import matplotlib.pyplot as plt
import mahotas as ms
image = ms.imread('nature.jpeg')
plt.imshow(image)
plt.show()

以下是执行上述代码时得到的图像:

displaying an image

Writing Images

Mahotas 中的图像写入是指将图像数据以特定图像格式(如 PNG、JPEG、BMP、TIFF 等)保存到文件中。我们可以使用 imsave() 函数在 Mahotas 中写入图像。此函数是 mahotas 中的图像输入/输出 (IO) 模块的一部分。

Example

在下面的代码中,我们尝试保存名为“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”中:

The image is read.
The image data is saved.

Image Processing with Mahotas

图像处理是指用于对图像执行多项操作的一系列技术。这些技术有助于提高图像的视觉质量、提取有用的信息或为分析准备图像。

一些常见的图像处理任务是滤波、分割和特征提取。在本节中,我们将探讨 Mahotas 提供的一些关键图像处理函数。

Filtering

在 mahotas 中,滤波是指修改图像的外观或从中提取有用的信息。这是通过对图像应用数学运算来完成的。滤波流程有助于去除噪声、平滑图像、增强边缘或执行其他操作,以帮助提高图像的质量或可解释性。

在下面的示例中,我们尝试使用高斯滤波器平滑图像。高斯滤波器模糊图像,可用于降低噪声或平滑图像:

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 的高斯滤波器平滑原始图像,如下所示:

image filtering

Segmentation

mahotas 中的分割是指根据某些特征或标准将图像划分为有意义的区域或部分的过程。这些部分可以表示不同的对象、感兴趣区域或图像中的不同区域。

现在,让我们通过 Mahotas 阈值处理来了解图像分割的基本示例。阈值处理用于根据像素强度值将对象从背景中分离出来。它通过将图像转换为二进制图像来简化图像,其中每个像素被归类为前景(对象)或背景:

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()

以上代码的输出如下所示 −

original segmented image

Feature Extraction

Mahotas 中的特征提取是指从图像中提取有意义且有益特征的过程。这些特征可以表示图像的各个方面,如纹理、形状或颜色,并且可用于描述和区分图像中的对象或区域。

现在,让我们看一个使用 Mahotas 计算 Zernike 矩的示例。Zernike 矩是一组数值,描述图像中对象或区域的形状。它们通过捕获对象的轮廓和空间特征,来提供对象的形状的紧凑表示:

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)

在执行上述代码后,我们会得到一组数值,这些数值描述了图像的形状特征,如下所示:

[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]