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

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 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

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.

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.

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.

Example

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)
Output

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.

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)

Output

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.

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.

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

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.

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)

Output

The output of the above code is as follows −

2.3690172519584466e+24
Advertisements