Mahotas 简明教程
Mahotas - Fraction of Zeros in an Image
图像中零的分数是指图像中像素值为零的像素与图像中像素总数的比例。在图像中,每个像素通常表示网格中的一个点,像素值可以在 0 到最大值之间,具体取决于图像的色彩深度或强度范围。
The fraction of zeros in an image refers to the proportion of zero−valued pixels compared to the total number of pixels in the image. In an image, each pixel typically represents a point in a grid, and the pixel values can range from 0 to a maximum value, depending on the image’s color depth or intensity range.
零价值较高的分数表示图像的很大一部分包含空白或背景区域,而零分数较低表示非零像素值的密度分布较高,这意味着内容更详细或更复杂。
A high fraction of zeros suggests that a significant portion of the image contains empty or background regions, while a low fraction of zeros indicates a denser distribution of nonzero pixel values, implying more detailed or complex content.
Fraction of Zeros in an Image in Mahotas
要获得 Mahotas 中图像中零的分数,我们需要遍历整个图像并通过图像中像素的总数除以零像素的计数。
To obtain the fraction of zeroes in an image in Mahotas, we need to iterate through the entire image and divide the count of zero pixels by the total number of pixels in the image.
像素的总数等于图像中的行数乘以列数。
The total number of pixels is equal to the number of rows multiplied by the number of columns in the image.
Example
在以下示例中,我们通过将图像数组与零进行比较,对 True 值求和并除以像素的总数来计算图像中零值像素分数 -
In the following example, we are calculating the fraction of zero−valued pixels in the image by comparing the image array to zero, summing the True values, and dividing by the total number of pixels −
import mahotas as mh
import numpy as np
image = mh.imread('sun.png')
# Calculating the fraction of zeros
fraction_of_zeros = np.sum(image == 0) / np.prod(image.shape)
print(f"Fraction of zeros: {fraction_of_zeros}")
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Fraction of zeros: 0.009496713127718466
Using the count_nonzero() Function
我们还可以使用 mahotas 中的 count_nonzero() 函数计算图像中零的分数。 count_nonzero() 函数用于计算数组中非零元素的数量。它接受数组作为输入,并返回非零元素的总数。
We can also calculate the fraction of zeros in an image using the count_nonzero() function in mahotas. The count_nonzero() function is used to count the number of non−zero elements in an array. It takes an array as input and returns the total count of elements that are non−zero.
Syntax
以下是在 mahotas 中 count_nonzero() 函数的基本语法 −
Following is the basic syntax of the count_nonzero() function in mahotas −
count_nonzero(arr, axis=None)
其中,
Where,
-
arr − It is the input array for which non−zero elements need to be counted.
-
axis (optional) − It is the axis or axes along which the non−zero elements are counted. If axis is not specified, all elements of the input array are considered.
Example
在此,我们使用 np.count_nonzero() 函数计算图像“nature.jpeg”中像素的数量 −
In here, we are counting the number of pixels of the image 'nature.jpeg' using the the np.count_nonzero() function −
import mahotas as mh
import numpy as np
image = mh.imread('nature.jpeg')
# Counting the number of zero pixels
zero_count = np.count_nonzero(image == 0)
# Calculating the fraction of zeros
total_pixels = image.size
fraction_of_zeros = zero_count / total_pixels
print("The fraction of zeros in the image is:", {fraction_of_zeros})
上述代码的输出如下:
Output of the above code is as follows −
The fraction of zeros in the image is: {0.010734258862206976}
Using Numpy
NumPy 库提供有效的用于处理数组和矩阵的数据结构和函数。它广泛用于诸如数学运算、数据处理和科学计算等任务,这是因为其高性能和广泛的功能。
The NumPy library provides efficient data structures and functions for working with arrays and matrices. It is widely used for tasks such as mathematical operations, data manipulation, and scientific computations due to its high performance and extensive functionality.
我们还可以使用 numpy 操作计算零的分数 −
We can also calculate the fraction of zeros using the numpy operation −
-
Firstly, the NumPy array is compared to zero to convert the image to binary form.
-
This comparison generates a boolean array where each element is True if the corresponding pixel value is greater than zero, and False otherwise.
-
The boolean array is then cast to the 'np.uint8' data type resulting in a binary image where white pixels are represented by ones and black pixels by zeros.
为了计算零的分数,计算二进制图像中零值元素的数量。此计数除以二进制图像中元素的总数以获得分数。
To calculate the fraction of zeros, the number of zero−valued elements in the binary image is computed. This count is divided by the total number of elements in the binary image to obtain the fraction.
Example
此处,我们首先将图像转换为二进制表示形式。然后,我们计算二进制图像的零分数 −
Here, we are first converting the image to a binary representation. We are then calculating the fraction of zeros of the binary image −
import mahotas as mh
import numpy as np
image = mh.imread('tree.tiff')
# Convert the image to binary
image_binary = (image > 0).astype(np.uint8)
# Calculate the fraction of zeros
fraction_of_zeros = np.sum(image_binary == 0) / np.prod(image_binary.shape)
print("Fraction of zeros:", fraction_of_zeros)
上述代码的输出如下:
Output of the above code is as follows −
Fraction of zeros: 0.014683837192681532