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
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
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.
Following is the basic syntax of center_of_mass() function in mahotas −
mahotas.center_of_mass(image)
Were, image refers to the input image for which you want to find the center of mass.
Example
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)
Output
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
The NumPy functions are built-in tools in the NumPy library that let you perform array operations and mathematical computations with ease in Python.
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.
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)
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
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)
Output
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.
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
Output of the above code is as follows −
[29.50213372 22.13203391]