Mahotas - 图像圆度



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

圆度值是通过将物体的形状与圆形进行比较来计算的。

完美的圆形物体其圆度值接近1,而形状更细长或不规则的物体其圆度值接近0。

Mahotas中的图像圆度

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

二值图像是一种图像,其中每个像素都被分类为前景(感兴趣的物体)或背景。通常,前景像素由白色(像素值=1)表示,背景像素由黑色(像素值=0)表示。

输入的二值图像应为布尔格式或用布尔值的NumPy数组表示。

mahotas.features.roundness()函数

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

语法

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

mahotas.features.roundness(image)

其中,“image”是布尔图像输入。

示例

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

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)

输出

获得的输出如下:

Roundness of the image= 4.98542867728303e-05

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

二值图像中的斑点圆度

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

要使用Mahotas计算二值图像中的斑点圆度,我们需要一个前景(斑点)和背景之间清晰分离的图像。然后,标记二值图像中的斑点,为每个斑点分配一个唯一的标识符(索引)。

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

示例

在这里,我们尝试计算二值图像中的斑点圆度:

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)

输出

上述代码的输出如下:

Blob Roundness: 2.0659091361803767

使用Zernike矩

Zernike矩是图像中物体形状的数学表示。

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

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

选择较小的半径非常适合分析较小的物体,而较大的半径更适合较大的物体。

一旦我们计算出Zernike矩,第一个矩在评估图像圆度时就显得尤为重要。它作为图像中物体整体圆度的代表性度量。

通过从Zernike矩列表中提取第一个元素,我们得到一个特定值,该值可以准确地量化物体的圆度。

示例

在这里,我们尝试使用Zernike矩查找图像圆度:

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)

输出

执行上述代码后,我们将得到如下所示的输出:

0.3183098861837907
广告