![Mahotas](/mahotas/images/mahotas-mini-logo.jpg)
- Mahotas Tutorial
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
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