Mahotas 简明教程
Mahotas - Zernike Features
Zernike 特征是一组数学量,它们描述图像中对象形状。这些量反映了有关形状的具体细节,例如它的圆度、对称性或某些模式的存在。
Zernike features are a set of mathematical values that describe the shape of objects in an image. These value reflects specific details about the shape, such as its roundness, symmetry, or the presence of certain patterns.
Zernike 特征具有一些特殊属性,使得它们对于形状分析非常有用。例如,它们可以描述形状而不论其大小如何,这意味着即使形状旋转或缩放,其特征也会保持不变。
The Zernike features have some special properties that make them useful for shape analysis. For example, they can describe the shape regardless of its size, meaning that the features remain the same even if the shape is rotated or scaled.
此属性有助于在不同条件下识别对象。
This property helps in recognizing objects under different conditions.
Zernike 特征通过使用称为 Zernike 多项式的数学函数将形状分解成更小的部分来工作。这些多项式起到构建模块的作用,我们通过将它们组合起来重新创建和表示对象形状。
Zernike features work by breaking down the shape into smaller pieces using mathematical functions called Zernike polynomials. These polynomials act like building blocks, and by combining them, we can recreate and represent the shape of the object.
Zernike Features in Mahotas
要在 mahotas 中计算 Zernike 特征,我们可以使用 mahotas.features.zernike() 函数。
To calculate the Zernike features in mahotas, we can use the mahotas.features.zernike() function.
在 Mahotas 中,Zernike 特征是通过执行以下步骤来计算的−
In Mahotas, Zernike features are calculated by performing the following steps −
Step1 − 生成一组 Zernike 多项式,它们是表示各种形状和轮廓的特殊数学函数。这些多项式可用作分析对象形状的构建模块。
Step1 − Generate 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.
Step2 − 通过将对象的形状投影到 Zernike 多项式上来计算 Zernike 矩。这些矩捕捉了重要的形状特征。
Step2 − Compute Zernike moments by projecting the shape of the object onto the Zernike polynomials. These moments capture important shape characteristics.
Step3 − 从计算出的距中提取 Zernike 特征,它们表示基本的形状信息。
Step3 − Extract the Zernike features from the computed moments, which represent the essential shape information.
The mahotas.features.zernike() function
mahotas.features.zernike() 函数接受三个参数:图像对象、Zernike 多项式的最大半径和要计算的特征(度数)。该函数返回图像的 Zernike 特征的 1-D 数组。
The mahotas.features.zernike() function takes three arguments: the image object, the maximum radius for the Zernike polynomials, and the number of features (degree) to compute. The function returns a 1−D array of the Zernike features for the image.
Zernike 矩的度数是矩的复杂度的度量。度数越高,矩越复杂。
The degree of a Zernike moment is a measure of the complexity of the moment. The higher the degree, the more complex the moment.
以下是 mahotas.features.zernike() 函数的基本语法−
Following is the basic syntax of the mahotas.features.zernike() function −
mahotas.features.zernike(im, degree, radius, cm={center_of_mass(im)})
其中,
Where,
-
im − It is the input image on which the Zernike moments will be computed.
-
degree − It specifies the maximum number of the Zernike moments to be calculated.
-
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.
-
cm (optional) − It specifies the center of mass of the image. By default, the center of mass of the image is used.
以下是计算 Zernike 特征以对图像进行形状识别的基本示例:
Following is the basic example to calculate the Zernike features for shape recognition of an image −
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)
在执行上述代码后,我们如下获得输出 −
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 中的自定义质心计算图像的 Zernike 矩,我们需要将 cm 参数传递给 mahotas.features.zernike() 函数。
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() function.
cm 参数取两个数字的元组,表示自定义重心的坐标。
The cm parameter takes a tuple of two numbers, which represent the coordinates of the custom center of mass.
Example
在此,我们尝试使用自定义质心计算图像的 Zernike 特征:
In here, we are trying to calculate the Zernike features 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 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)
以下是上面代码的输出: -
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]
Calculating Zernike Features of Multiple Images
我们还可以计算不同格式的多个图像的 Zernike 特征。以下是实现此目的的方法:
We can also calculate the Zernike features of multiple images in different formats. Following is the approach to achieve this −
-
Creates an empty list.
-
Use a for loop to iterate over the list of images.
-
Calculate the Zernike features of each image.
features.zernike() 函数返回一个 Zernike 矩向量,然后将其附加到 Zernike 特征列表中。
The features.zernike() function returns a vector of Zernike moments, which is then appended to the list of Zernike features.
Example
现在,我们尝试计算不同格式的多个图像的 Zernike 特征:
Now, we are trying to calculate the Zernike features of multiple images of different formats altogether −
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)
上述代码的输出如下:
Output of the above code is as follows −
[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])]