Mahotas 简明教程
Mahotas - Centre of Mass of an Image
质心是指物体的质量的平均位置。它是物体总质量集中的一个点。简单地说,它表示物体的平衡点。如果物体是均匀且对称的,则质心将在其几何中心,否则不在。
The center of mass refers to the average position of the mass of an object. It is a point where the total mass of the object is concentrated. In simple terms, it represents the balancing point of an object. If the object is uniform and symmetric, the center of mass will be at its geometric center, otherwise not.
Center of Mass of an Image in Mahotas
Mahotas 中的质心是通过为对象的每个像素分配一个质量值,然后计算这些质量值的平均位置来确定的。这产生了质心的坐标,它指示了对象质心在图像内的位置。
The center of mass in Mahotas is determined by assigning a mass value to each pixel of the object, and then calculating the average position of these mass values. This results in the coordinates of the center of mass, which indicate the location of the object’s centroid within the image.
Using mahotas.center_of_mass() Function
mahotas.center_of_mass() 函数用于查找图像的质心。它计算图像中像素强度的平均位置(作为坐标组),提供图像“中心”所在位置的度量。
The mahotas.center_of_mass() function is used to find the center of mass of an image. It calculates the average position (as a tuple of coordinates) of the pixel intensities in the image, providing a measure of where the "center" of the image is located.
以下是 mahotas 中 center_of_mass() 函数的基本语法 −
Following is the basic syntax of center_of_mass() function in mahotas −
mahotas.center_of_mass(image)
其中, image 指示您要查找其质心的输入图像。
Were, image refers to the input image for which you want to find the center of mass.
在以下示例中,我们正在计算图像 'nature.jpeg' 的质心 −
In the following example we are calculating the center of mass of the image 'nature.jpeg' −
import mahotas as ms
import numpy as np
# Loading the image
image = ms.imread('nature.jpeg')
# Calculating the center of mass
com = ms.center_of_mass(image)
# Printing the center of mass
print("Center of mass of the image is:", com)
质心以 3D 坐标的形式表示为 [x, y, z],如下面输出所示 −
The center of mass is represented by a 3D coordinate in the form [x, y, z] as shown in the output below −
Center of mass of the image is: [474.10456551 290.26772015 0.93327202]
Center of Mass Using Numpy Functions
NumPy 函数是 NumPy 库中内置的工具,可让您轻松地执行数组操作和 Python 中的数学计算。
The NumPy functions are built-in tools in the NumPy library that let you perform array operations and mathematical computations with ease in Python.
要使用 Mahotas 中的 NumPy 函数计算质心,我们需要确定图像“权重”的平均位置。这是通过将每个像素的 x 和 y 坐标与其强度值相乘,对这些加权坐标求和,然后将结果除以强度总和来完成的。
To calculate the center of mass using NumPy functions in Mahotas, we need to determine the average position of the "weight" of an image. This is achieved by multiplying the x and y coordinates of each pixel by their intensity values, summing these weighted coordinates, and then dividing the result by the total sum of intensities.
以下是使用 numpy 函数计算图像质心的基本语法 −
Following is the basic syntax to calculate center of mass of an image using numpy functions −
com = np.array([np.sum(X * Y), np.sum(A * B)]) / np.sum(C)
其中 'X' 和 'Y' 表示坐标数组, 'A' 和 'B' 表示与坐标相关的值或强度的数组, 'C' 表示数组表示的值或强度的总和。
Where, 'X' and 'Y' represent the coordinate arrays, 'A' and 'B' represent the arrays corresponding to the values or intensities associated with the coordinates, and 'C' refers to the array representing the overall sum of the values or intensities.
Example
在这里,我们使用创建单个坐标数组。coords 的第一维表示 y 坐标,第二维表示 x 坐标。然后我们在计算加权和时访问 coords[1] 获取 x 坐标,访问 coords[0] 获取 y 坐标 −
In here, we are creating a single coordinate array using. The first dimension of coords represents the y−coordinates and the second dimension represents the x−coordinates. We then access coords[1] to get the x−coordinates and coords[0] to get the y−coordinates when calculating the weighted sum −
import mahotas as mh
import numpy as np
# Loading the image
image = mh.imread('tree.tiff')
# Creating a single coordinate array
coords = np.indices(image.shape)
# Calculating the weighted sum of x and y coordinates
com = np.array([np.sum(coords[1] * image), np.sum(coords[0] * image)]) /
np.sum(image)
# Printing the center of mass
print("Center of mass:", com)
以下是上面代码的输出: -
Following is the output of the above code −
Center of mass: [ 7.35650493 -3.83720823]
Center of Mass of a Specific Region
特定区域的质心是图像中感兴趣的区域(面积)。这可以是特定区域,例如边框框或所选区域。
The center of mass of a specific region is the region (area) of interest within the image. This could be a specific area, such as a bounding box or a selected region.
特定区域的质心是通过计算感兴趣区域 (ROI) 中每个像素的 x 和 y 坐标的加权平均值来计算的,其中权重是像素强度。质心作为两个值的对返回,分别表示 x 和 y 坐标。
The center of mass of a specific region is calculated by taking the weighted average of the x and y coordinates of each pixel in the ROI (Region of Interest), where the weights are the pixel intensities. The center of mass is returned as a tuple of two values representing the x and y coordinates, respectively.
Example
以下是计算灰度图像感兴趣区域的质心的示例 −
Following is example to calculate the center of mass of a region of interest of a grayscale image −
import mahotas as mh
import numpy as np
# Load a grayscale image
image = mh.imread('nature.jpeg', as_grey=True)
# Defining a region of interest
roi = image[30:90, 40:85]
# Calculating the center of mass of the ROI
center = mh.center_of_mass(roi)
print(center)
上述代码的输出如下:
Output of the above code is as follows −
[29.50213372 22.13203391]