Mahotas 简明教程

Mahotas - Roundness of Image

图像的圆度是指图像中对象或区域与完美圆形的相似程度的测量值。它是一个用于量化圆形度或偏离圆形度的度量的指标。

The roundness of an image refers to the measure of how closely an object or a region in the image resembles a perfect circle. It is a metric used to quantify the degree of circularity or deviation from circularity.

通过比较对象的形状与圆形的形状,可以计算出圆度值。

The roundness value is calculated by comparing the object’s shape to that of a circle.

一个完美的圆形对象将具有接近 1 的圆度值,而形状更细长或不规则的对象将具有接近 0 的圆度值。

A perfectly round object would have a roundness value close to 1, while objects that are more elongated or irregular in shape would have roundness values closer to 0.

Roundness of Image in Mahotas

在 Mahotas 中,我们可以使用 mahotas.features.roundness() 函数计算对象的圆度。此函数以二进制图像作为输入。

In Mahotas, we can calculate the roundness of an object using the mahotas.features.roundness() function. This function takes a binary image as input.

二进制图像是一个其中每个像素均被分类为前景(感兴趣的对象)或背景的图像。通常,前景像素以白色(像素值 = 1)表示,而背景像素以黑色(像素值 = 0)表示。

A binary image is an image where each pixel is either classified as foreground (object of interest) or background. Generally, the foreground pixels are represented by white (pixel value = 1), and the background pixels are represented by black (pixel value = 0).

输入的二进制图像应为布尔格式,或表示为具有布尔值的 NumPy 数组。

The input binary image should be in a boolean format or represented as a NumPy array with boolean values.

The mahotas.features.roundness() function

'mahotas.features.roundness()' 函数接受二进制图像作为输入,并返回 0 到 1 之间的一个浮点值。值越接近 1.0,则形状越接近于完美的圆形。

The 'mahotas.features.roundness()' function accepts a binary image as input and returns a float value between 0 and 1. The closer the value is to 1.0, the closer the shape is to a perfect circle.

Syntax

以下是在 mahotas 中 roundness() 函数的基本语法 −

Following is the basic syntax of the roundness() function in mahotas −

mahotas.features.roundness(image)

其中 'image' 是布尔图像输入。

Where, 'image' is the Boolean image input.

Example

在以下示例中,我们使用 roundness() 函数查找图像的圆度 −

In the following example, we are finding the roundness of an image using the roundness() function −

import mahotas as mh
import numpy as np
image = mh.imread('sun.png', as_grey = True)
roundness = mh.features.roundness(image)
print("Roundness of the image= ", roundness)

获得的输出如下 −

The output obtained is as follows −

Roundness of the image= 4.98542867728303e-05

Blob Roundness in Binary Image

斑点圆度是指斑点与完美圆形相似程度的测量值。接近 1 的圆度值表示更圆形的斑点,而显著低于 1 的值表示更细长或更不规则的形状。

Blob roundness refers to a measure of how closely a blob resembles a perfect circle. A roundness value close to 1 indicates a more circular blob, while a value significantly lower than 1 indicates a more elongated or irregular shape.

为了使用 Mahotas 计算二进制图像中的斑点圆度,我们需要获取一个在前景(斑点)和背景之间有明显分离的图像。然后,标记二进制图像中的斑点,为每个斑点分配一个唯一的标识符(索引)。

To compute blob roundness in a binary image using Mahotas, we need to take an image having a clear separation between the foreground (blobs) and the background. Then, label the blobs in the binary image to assign a unique identifier (index) to each blob.

这有助于区分各个斑点。之后,计算每个标记斑点的圆度。

It helps in distinguishing individual blobs. Thereafter, compute the roundness of each labeled blob.

Example

在此,我们尝试计算一个二进制图像中的斑点圆度——

In here, we are trying to compute the blob roundness in a binary image −

import mahotas as mh
import numpy as np
image = mh.imread(tree.tiff', as_grey=True)
# Labelling the blobs in the image
labeled, _ = mh.label(image)
# Computing the roundness of each blob
roundness = mh.features.roundness(labeled)
print("Blob Roundness:", roundness)

上述代码的输出如下:

Output of the above code is as follows −

Blob Roundness: 2.0659091361803767

Using zernike_moment

Zernike 矩是图像中对象形状的数学表示。

Zernike moments are mathematical representations of the shape of an object in an image.

它通过分析对象中的强度或颜色变化的分布来捕捉图像的圆度和其他形状属性。

It captures the roundness and other shape attributes of an image by analyzing the distribution of intensity or color variations within the object.

为了使用 Zernike 矩确定图像的圆度,我们首先指定一个半径值。此半径决定了计算矩的圆形区域的大小。

To determine the roundness of an image using Zernike Moments, we start by specifying a radius value. This radius determines the size of the circular region in which the moments will be computed.

选择较小的半径适用于分析较小的对象,而较大的半径更适合较大的对象。

Choosing a smaller radius is ideal for analyzing smaller objects, while a larger radius is more suitable for larger objects.

一旦计算出 Zernike 矩,在评估图像圆度时,第一个矩便显得尤为重要。它作为图像中对象总体圆度的代表性度量。

Once we have the Zernike Moments calculated, the first moment becomes particularly important when assessing image roundness. It serves as a representative measure of the overall roundness of the object within the image.

通过从 Zernike 矩列表中提取第一个元素,我们获得了一个特定值,该值准确地量化了对象的圆度。

By extracting the first element from the Zernike Moments list, we obtain a specific value that quantifies the roundness of the object accurately.

Example

此处,我们尝试使用 Zernike 矩查找图像圆度——

Here, we are trying to find an image roundness with Zernike Moments −

import mahotas as mh
image = mh.imread('nature.jpeg', as_grey = True)
# Setting the radius for calculating Zernike moments
radius = 10
# Calculating the Zernike moments
moments = mh.features.zernike_moments(image, radius=radius)
# The first Zernike moment represents roundness
roundness = moments[0]
print(roundness)

执行以上代码后,我们得到如下所示的输出 −

After executing the above code, we get the output as shown below −

0.3183098861837907