Mahotas 简明教程

Mahotas - Zernike Features

Zernike 特征是一组数学量,它们描述图像中对象形状。这些量反映了有关形状的具体细节,例如它的圆度、对称性或某些模式的存在。

Zernike 特征具有一些特殊属性,使得它们对于形状分析非常有用。例如,它们可以描述形状而不论其大小如何,这意味着即使形状旋转或缩放,其特征也会保持不变。

此属性有助于在不同条件下识别对象。

Zernike 特征通过使用称为 Zernike 多项式的数学函数将形状分解成更小的部分来工作。这些多项式起到构建模块的作用,我们通过将它们组合起来重新创建和表示对象形状。

Zernike Features in Mahotas

要在 mahotas 中计算 Zernike 特征,我们可以使用 mahotas.features.zernike() 函数。

在 Mahotas 中,Zernike 特征是通过执行以下步骤来计算的−

Step1 − 生成一组 Zernike 多项式,它们是表示各种形状和轮廓的特殊数学函数。这些多项式可用作分析对象形状的构建模块。

Step2 − 通过将对象的形状投影到 Zernike 多项式上来计算 Zernike 矩。这些矩捕捉了重要的形状特征。

Step3 − 从计算出的距中提取 Zernike 特征,它们表示基本的形状信息。

The mahotas.features.zernike() function

mahotas.features.zernike() 函数接受三个参数:图像对象、Zernike 多项式的最大半径和要计算的特征(度数)。该函数返回图像的 Zernike 特征的 1-D 数组。

Zernike 矩的度数是矩的复杂度的度量。度数越高,矩越复杂。

以下是 mahotas.features.zernike() 函数的基本语法−

mahotas.features.zernike(im, degree, radius, cm={center_of_mass(im)})

其中,

  1. im − 它是将计算泽尼克矩的输入图像。

  2. degree − 它指定要计算的 Zernike 矩的最大数量。

  3. radius − 它定义了以像素为单位的圆形区域的半径,将在该区域上计算泽尼克矩。忽略围绕质心定义的该半径确定的圆圈外的区域。

  4. cm (optional) − 它指定图像的重心。默认情况下,使用图像的重心。

以下是计算 Zernike 特征以对图像进行形状识别的基本示例:

import mahotas as mh
# Load images of shapes
image1 = mh.imread('sun.png', as_grey=True)
# Compute Zernike features
features = mh.features.zernike(image1, degree=8, radius=10)
# Printing the features for shape recognition
print(features)

在执行上述代码后,我们如下获得输出 −

[0.31830989 0.00534998 0.00281258 0.0057374  0.01057919 0.00429721
 0.00178094 0.00918145 0.02209622 0.01597089 0.00729495 0.00831211
 0.00364554 0.01171028 0.02789188 0.01186194 0.02081316 0.01146935
 0.01319499 0.03367388 0.01580632 0.01314671 0.02947629 0.01304526
 0.00600012]

Using Custom Center of Mass

图像的重心是图像中质量均匀分布的点。自定义重心是图像中不一定为图像重心的点。

这在您希望针对您的计算使用不同的重心时很有用。

例如,您可能希望使用图像中物体的自定义重心来计算该物体的泽尼克矩。

要使用 mahotas 中的自定义质心计算图像的 Zernike 矩,我们需要将 cm 参数传递给 mahotas.features.zernike() 函数。

cm 参数取两个数字的元组,表示自定义重心的坐标。

Example

在此,我们尝试使用自定义质心计算图像的 Zernike 特征:

import mahotas
import numpy as np
# Load the image
image = mahotas.imread('nature.jpeg', as_grey = True)
# Calculate the center of mass of the image
center_of_mass = np.array([100, 100])
# Calculate the Zernike features of the image, using the custom center of mass
zernike_features = mahotas.features.zernike(image, degree= 5, radius = 5,
cm=center_of_mass)
# Print the Zernike features
print(zernike_features)

以下是上面代码的输出: -

[3.18309886e-01 3.55572603e-04 3.73132619e-02 5.98944983e-04
3.23622041e-04  1.72293481e-04 9.16757235e-02 3.35704966e-04
7.09426259e-02  1.17847972e-04 2.12625026e-04 3.06537827e-04]

Calculating Zernike Features of Multiple Images

我们还可以计算不同格式的多个图像的 Zernike 特征。以下是实现此目的的方法:

  1. Creates an empty list.

  2. 使用 for 循环迭代图像列表。

  3. 计算每个图像的 Zernike 特征。

features.zernike() 函数返回一个 Zernike 矩向量,然后将其附加到 Zernike 特征列表中。

Example

现在,我们尝试计算不同格式的多个图像的 Zernike 特征:

import mahotas
import numpy as np
# Load the images
images = [mahotas.imread('sun.png', as_grey = True),
mahotas.imread('nature.jpeg', as_grey = True), mahotas.imread('tree.tiff',
as_grey = True)]
# Calculate the Zernike features of the images
zernike_features = []
for image in images:
   zernike_features.append(mahotas.features.zernike(image, degree=2, radius =
2))
# Print the Zernike features
print(zernike_features)

上述代码的输出如下:

[array([0.31830989, 0.05692079, 0.10311168, 0.01087613]), array([0.31830989, 0.02542476, 0.11556386, 0.01648607]), array([0.31830989, 0.12487805, 0.07212079, 0.03351757])]