Mahotas 简明教程

Mahotas - Zernike Moments

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

Like Zernike features, Zernike moments are also a set of mathematical values that describe the shape of objects in an image. They provide specific details about the shape, like how round or symmetrical it is, or if there are any particular patterns present.

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

Zernike moments have some special properties as discussed below −

  1. Size Invariance − They can describe the shape regardless of its size. So, even if you have a small or large object, the Zernike moments will still capture its shape accurately.

  2. Rotation Invariance − If you rotate the object in the image, the Zernike moments will remain the same.

  3. Scaling Invariance − If you resize the object in the image, the Zernike moments will remain the same.

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

Zernike moments break down the shape into smaller pieces using special mathematical functions called Zernike polynomials.

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

These polynomials act like building blocks. By combining different Zernike polynomials, we can recreate and represent the unique features of the object’s shape.

Zernike Moments in Mahotas

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

To calculate the Zernike moments in mahotas, we can use the mahotas.features.zernike_moments() function.

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

In Mahotas, Zernike moments are calculated by generating a set of Zernike polynomials, which are special mathematical functions representing various shapes and contours.

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

These polynomials act as building blocks for analyzing the object’s shape.

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

After that, compute Zernike moments by projecting the shape of the object onto the Zernike polynomials. These moments capture important shape characteristics.

The mahotas.features.zernike_moments() function

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

The mahotas.features.zernike_moments() function takes two arguments: the image object and the maximum radius for the Zernike polynomials. The function returns a 1−D array of the Zernike moments for the image.

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

Following is the basic syntax of the mahotas.features.zernike_moments() function −

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

其中,

Where,

  1. im − It is the input image on which the Zernike moments will be computed.

  2. radius − It defines the radius of the circular region, in pixels, over which the Zernike moments will be calculated. The area outside the circle defined by this radius, centered around the center of mass, is ignored.

  3. degree (optional) − It specifies the maximum number of the Zernike moments to be calculated. By default, the degree value is 8.

  4. cm (optional) − It specifies the center of mass of the image. By default, the center of mass of the image is used.

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

Following is the basic example to calculate the Zernike moments of an image with a default degree value −

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)

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

After executing the above code, we get the output as follows −

[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

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

The center of mass of an image is the point in the image where the mass is evenly distributed. The custom center of mass is a point in an image that is not necessarily the center of mass of the image.

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

This can be useful in cases where you want to use a different center of mass for your calculations.

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

For example, you might want to use the custom center of mass of an object in an image to calculate the Zernike moments of the object.

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

To calculate the Zernike moments of an image using a custom center of mass in mahotas, we need to pass the cm parameter to the mahotas.features.zernike_moments() function.

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

The cm parameter takes a tuple of two numbers, which represent the coordinates of the custom center of mass.

Example

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

In here, we are trying to calculate the Zernike moments of an image using the custom center of mass −

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)

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

Following is the output of the above code −

[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

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

The order of a Zernike moment is a measure of the complexity of the shape that it can represent. The higher the order, the more complex the shape that the moment can represent.

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

To compute the Zernike moments of an image with a specific order in mahotas, we need to pass the degree parameter to the mahotas.features.zernike_moments() function.

Example

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

In the following example, we are trying to compute the Zernike moments of an image with a specified order.

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)

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

Output of the above code is as shown below −

[0.31830989 0.17086131 0.03146824 0.1549947 0.30067136 0.5376049 0.30532715 0.33032683 0.47908119]