Mahotas 简明教程

Mahotas - Eccentricity of an Image

图像的偏心率是指图像中对象或区域的形状拉伸或延伸的程度。它为形状与其圆形的完美程度之间的偏差提供了定量测量。

The eccentricity of an image refers to the measure of how elongated or stretched the shape of an object or region within the image is. It provides a quantitative measure of how how much the shape deviates from a perfect circle.

偏心率值介于 0 和 1 之间,其中 −

The eccentricity value ranges between 0 and 1, where −

  1. 0 − Indicates a perfect circle. Objects with an eccentricity of 0 have the least elongation and are perfectly symmetric.

  2. Close to 1 − Indicates increasingly elongated shapes. As the eccentricity value approaches 1, the shapes become more elongated and less circular.

Eccentricity of an Image in Mahotas

我们可以在 Mahotas 中使用 'mahotas.features.eccentricity()' 函数计算图像的偏心率。

We can calculate the eccentricity of an image in Mahotas using the 'mahotas.features.eccentricity()' function.

如果偏心率值较高,它表明该图像中的形状更加拉伸或细长。在另一方面,如果偏心率值较低,它表明形状接近于正圆或不那么细长。

If the eccentricity value is higher, it indicates that the shapes in the image are more stretched or elongated. On the other hand, if the eccentricity value is lower, it indicates that the shapes are closer to being perfect circles or less elongated.

The mahotas.features.eccentricity() function

mahotas 中的 eccentricity() 函数可帮助我们度量图像中的形状拉伸或细长程度。该函数采用具有单通道的图像作为输入,并返回介于 0 和 1 之间的浮点数。

The eccentricity() function in mahotas helps us measure how stretched or elongated the shapes are in an image. This function takes an image with single channel as input and returns a float point number between 0 and 1.

Syntax

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

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

mahotas.features.eccentricity(bwimage)

其中, 'bwimage' 是被解释为布尔值输入的图像。

Where, 'bwimage' is the input image interpreted as a boolean value.

Example

在以下示例中,我们发现了一幅图像的偏心率 −

In the following example, we are finding the eccentricity of an image −

import mahotas as mh
import numpy as np
from pylab import imshow, show
image = mh.imread('nature.jpeg', as_grey = True)
eccentricity= mh.features.eccentricity(image)
print("Eccentricity of the image =", eccentricity)

上述代码的输出如下:

Output of the above code is as follows −

Eccentricity of the image = 0.8902515127811386

Calculating Eccentricity using Binary Image

要将灰度图像转换为二进制格式,我们使用一种称为阈值化的技术。此过程有助于我们图像划分为两部分 − 前景色(白色)和背景(黑色)。

To convert a grayscale image into a binary format, we use a technique called thresholding. This process helps us to separate the image into two parts − foreground (white) and background (black).

我们通过选择一个阈值(表示像素强度)来完成此操作,它充当一个临界点。

We do this by picking a threshold value (indicates pixel intensity), which acts as a cutoff point.

Mahotas 通过提供 “>” 运算符简化了此过程,使我们能够比较像素值与阈值,并创建二进制图像。二进制图像准备就绪后,即可计算偏心率。

Mahotas simplifies this process for us by providing the ">" operator, which allows us to compare pixel values with the threshold value and create a binary image. With the binary image ready, we can now calculate the eccentricity.

Example

在此处,我们尝试计算二进制图像的偏心率 −

Here, we are trying to calculate the eccentricity of a binary image −

import mahotas as mh
image = mh.imread('nature.jpeg', as_grey=True)
# Converting image to binary based on a fixed threshold
threshold = 128
binary_image = image > threshold
# Calculating eccentricity
eccentricity = mh.features.eccentricity(binary_image)
print("Eccentricity:", eccentricity)

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

Eccentricity: 0.7943319646935899

Calculating Eccentricity using the Skeletonization

骨架化(又称为细化)是一个旨在缩小对象形状或结构,将其表示为一个细骨架的过程。我们可以使用 mahotas 中的 thin() 函数来实现这一点。

Skeletonization, also known as thinning, is a process that aims to reduce the shape or structure of an object, representing it as a thin skeleton. We can achieve this using the thin() function in mahotas.

mahotas.thin() 函数采用二进制图像作为输入,其中感兴趣的对象由白色像素(像素值为 1)表示,而背景由黑色像素(像素值为 0)表示。

The mahotas.thin() function takes a binary image as input, where the object of interest is represented by white pixels (pixel value of 1) and the background is represented by black pixels (pixel value of 0).

我们可以通过将图像简化为其骨架表示,使用骨架化计算图像的偏心率。

We can calculate the eccentricity of an image using skeletonization by reducing the image to its skeleton representation.

Example

现在,我们使用骨架化计算图像的偏心率 −

Now, we are calculating the eccentricity of an image using the skeletonization −

import mahotas as mh
import matplotlib.pyplot as plt
# Read the image and convert it to grayscale
image = mh.imread('tree.tiff')
grey_image = mh.colors.rgb2grey(image)
# Skeletonizing the image
skeleton = mh.thin(grey_image)
# Calculating the eccentricity of the skeletonized image
eccentricity = mh.features.eccentricity(skeleton)
# Printing the eccentricity
print(eccentricity)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(7,5 ))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the skeletonized image
axes[1].imshow(skeleton, cmap='gray')
axes[1].set_title('Skeletonized Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

获得的输出如下所示 −

The output obtained is as shown below −

0.8975030064719701

显示的图像如下所示:

The image displayed is as shown below −

calculating eccentricity