Mahotas 简明教程
Mahotas - Getting Image Moments
图像矩是用于描述图像各种属性的统计度量。它提供有关形状、位置、对象方向和图像内对象强度的信息。
Image moment is the statistical measure that is used to describe various properties of an image. It provides information about the shape, location, orientation of the objects and intensity of objects within an image.
通常,图像矩通过将像素强度及其相应空间坐标的乘积求和来计算。这些矩可用于导出有用的特征,如图像中对象的质心(平均位置)、面积、方向和比例。高阶矩可以捕获更复杂形状特征。
In general, image moments are calculated by summing up the product of pixel intensities and their corresponding spatial coordinates. These moments can be used to derive useful features such as centroid (mean position), area, orientation, and scale of objects in an image. Higher−order moments can capture more complex shape characteristics.
Getting Image moments in Mahotas
在 Mahotas 的上下文中,图像矩使用 mahotas.moments() 函数计算。此函数将图像作为输入并返回一组描述图像的矩。
In the context of Mahotas, image moments are calculated using the mahotas.moments() function. This function takes an image as input and returns a set of moments that characterize the image.
Mahotas 提供各种类型的矩,包括原始矩、中心矩、归一化矩和 Hu 矩。这些矩对于对象识别、图像对齐和形状分析等任务非常有用。
Mahotas provides various types of moments, including raw moments, central moments, normalized moments, and Hu moments. These moments can be useful for tasks such as object recognition, image alignment, and shape analysis.
Using the mahotas.moments() Function
mahotas.moments() 函数用于获取图像或感兴趣区域 (ROI) 的矩。该函数将图像对象作为输入并返回包含计算矩的 numpy 数组。
The mahotas.moments() function is used to get the moments of an image or a region of interest (ROI). This function takes an image object as input and returns a numpy array containing the computed moments.
NumPy 数组就像一个数据表格,其中每个值都排列在一个网格中,您可以轻松地对整个网格或其特定部分执行计算。
A NumPy array is like a table of data where each value is arranged in a grid, and you can perform calculations on the entire grid or specific parts of it easily.
以下是 Mahotas 中 moments() 函数的基本语法:
Following is the basic syntax of the moments() function in Mahotas −
mahotas.moments(image, p0, p1, cm=(0, 0), convert_to_float=True)
其中,
where,
-
p0 − It is the power of the first dimension (float)
-
p1 − It is the power of the second dimension (float)
-
image − It is the input image and it should be a 2−D array
-
cm − It is the center of mass and (o,o)is taken as default.
在下面给出的示例中,我们使用 moments() 函数获取图像矩 −
In the example given below, we are using the moments() function to get image moments −
import mahotas as mh
import numpy as np
from pylab import imshow, show
image = mh.imread('sun.png')
# extracting the first channel (0th index) of the image array using slicing
cimage=image[:,:,0]
p0=5.5
p1=5.5
moment = mh.moments(cimage,p0,p1)
print(moment)
以下是上面代码的输出: -
Following is the output of the above code −
2.971238276705602e+39
Getting Image Moments by Specifying Center of Mass
质心是对像素平均位置的度量,根据像素强度进行加权。通过指定质心,我们可以获得相对该特定位置的矩。
The center of mass is a measure of the average position of the pixels, weighted by their intensities. By specifying the center of mass, we can obtain moments that are relative to that particular location.
我们可以指定质心,通过将 'cm' 参数传递给 moments() 函数来获得图像矩。通过设置质心,我们可以平移坐标系统,并获得相对于图像内特定位置的矩。
We can specify the center of mass to get image moments by passing the 'cm' parameter to the moments() function. By setting the center of mass, we can shift the coordinate system and obtain moments that are relative to a specific position within the image.
Example
在此处,我们通过指定质心配合度来获取图像矩 −
In here, we are getting the image moment by specifying the center of mass co-ordinates −
import mahotas as mh
image = mh.imread('nature.jpeg', as_grey = True)
moments = mh.moments(image, p0=1, p1=0, cm=(100, 100))
print(moments)
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
40074427849.0
Getting Image moments by Disabling Float Conversion
通过禁用浮点转换,我们在矩形计算过程中保留输入图像的原始数据类型。这在处理特定图像格式时非常有用,例如灰度或二进制图像,其中像素值已为整数格式。
By disabling float conversion, we preserve the original data type of the input image during the moment calculation. This is helpful when we work with specific image formats, such as grayscale or binary images, where the pixel values are already in integer format.
我们可以通过将 'convert_to_float' 参数传递给 moments() 函数,在 mahotas 中图像矩计算期间禁用将输入图像转换为浮点表示。
We can disable the conversion of the input image to a floating−point representation during the calculation of image moments in mahotas, by passing the 'convert_to_float' parameter to the moments() function.
convert_to_float 参数被明确设置为 False。这确保输入图像在矩形计算期间不会转换为浮点表示。
The convert_to_float parameter is explicitly set to False. This ensures that the input image is not converted to a floating−point representation during the moment calculation.
Example
在以下示例中,我们通过禁用浮点转换来获取图像矩 −
In the following example, we are getting image moments by disabling float conversion −
import mahotas as mh
image = mh.imread('tree.tiff', as_grey = True)
moments = mh.moments(image, p0=2, p1=0, cm=(0, 0), convert_to_float=False)
print(moments)
上述代码的输出如下:
Output of the above code is as follows −
11029976739711.432
Getting Higher Order Image Moments
高阶图像矩提供了图像像素分布、对称性和形状特征的更详细信息。这些矩帮助我们捕捉低阶矩可能忽略的复杂模式和变化。
Higher−order image moments provide more detailed information of the image’s pixel distribution, symmetry, and shape characteristics. These moments help us to capture complex patterns and variations that lower−order moments might overlook.
我们可以通过使用 'p0' 和 'p1' 参数指定所需矩阶数来在 mahotas 中获取高阶图像矩,其中较高值表示高阶。
We can get the higher order image moments in mahotas by specifying the desired order of moments using the 'p0' and 'p1' parameters, where higher values represent higher orders.
Example
此处,我们尝试获取高阶图像矩 −
Here, we are trying to get the higher order image moments −
import mahotas as mh
image = mh.imread('sea.bmp', as_grey = True)
moments = mh.moments(image, p0=3, p1=3, cm=(10, 10))
print(moments)
以上代码的输出如下所示 -
The output of the above code is as follows −
2.3690172519584466e+24