Mahotas 简明教程

Mahotas - Zernike Moments

与泽尼克特征类似,泽尼克矩也是描述图像中对象形状的一组数学值。它们提供了关于形状的具体细节,例如它是如何圆形或对称的,或者是否存在任何特定图案。

泽尼克矩有一些特殊属性,如下所述 −

  1. Size Invariance − 无论形状尺寸如何,它们都可以对其进行描述。因此,即使你有一个较小或较大的物体,泽尼克矩仍然会准确地捕捉其形状。

  2. Rotation Invariance − 如果你旋转图像中的物体,泽尼克矩将保持不变。

  3. Scaling Invariance − 如果你调整图像中物体的尺寸,泽尼克矩将保持不变。

泽尼克矩使用称为泽尼克多项式的特殊数学函数将形状分解为更小的部分。

这些多项式起到构建模块的作用。通过组合不同的泽尼克多项式,我们可以重新创建和表示对象的形状特征。

Zernike Moments in Mahotas

要在 Mahotas 中计算泽尼克矩,我们可以使用 mahotas.features.zernike_moments() 函数。

在 Mahotas 中,泽尼克矩是通过生成一组泽尼克多项式来计算的,这些多项式是表示各种形状和轮廓的特殊数学函数。

这些多项式充当分析对象形状的构建模块。

之后,通过将对象的形状投影到泽尼克多项式上来计算泽尼克矩。这些矩记录了重要的形状特征。

The mahotas.features.zernike_moments() function

mahotas.features.zernike_moments() 函数有两个参数:图像对象和泽尼克多项式的最大半径。该函数返回图像的泽尼克矩的 1D 数组。

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

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

其中,

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

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

  3. degree (optional) − 它指定要计算的最大泽尼克矩数。默认情况下,度值为 8。

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

以下是使用默认度值计算图像的泽尼克矩的基本示例 −

import mahotas as mh
# Load images of shapes
image = mh.imread('sun.png', as_grey=True)
# Compute Zernike moments
moments = mh.features.zernike_moments(image, radius=10)
# Compare the moments for shape recognition
print(moments)

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

[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 中的自定义重心计算图像的泽尼克矩,我们需要将 cm 参数传递给 mahotas.features.zernike_moments() 函数。

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

Example

在这里,我们尝试使用自定义重心来计算图像的泽尼克矩 −

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 moments of the image, using the custom center of mass
zernike_moments = mahotas.features.zernike_moments(image, radius = 5,
cm=center_of_mass)
# Print the Zernike moments
print(zernike_moments)

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

[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
 1.94379185e-01 1.32093249e-04 8.54616882e-02 1.83274207e-04
 1.86728282e-04 3.08004108e-04 4.79437809e-04 1.97726337e-04
 3.61630733e-01 5.27467687e-04 8.25534856e-02 7.75593823e-06
 1.99419391e-01]

Using a Specific Order

泽尼克矩的阶数是其可以表示的形状复杂性的量度。阶数越高,矩可以表示的形状就越复杂。

要使用特定阶数的 mahotas 计算图像的泽尼克矩,我们需要将 degree 参数传递给 mahotas.features.zernike_moments() 函数。

Example

在以下示例中,我们尝试计算特定阶数图像的泽尼克矩。

import mahotas
import numpy as np
# Load the image
image = mahotas.imread('nature.jpeg', as_grey = True)
# Calculate the Zernike moments of the image, using the specific order
zernike_moments = mahotas.features.zernike_moments(image,1, 4)
# Print the Zernike moments
print(zernike_moments)

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

[0.31830989 0.17086131 0.03146824 0.1549947 0.30067136 0.5376049 0.30532715 0.33032683 0.47908119]