Like Zernike features, Zernike moments are also a set of mathematical values that describe the shape of objects in an image. They provide specific details about the shape, like how round or symmetrical it is, or if there are any particular patterns present.
Zernike moments have some special properties as discussed below −
-
Size Invariance − They can describe the shape regardless of its size. So, even if you have a small or large object, the Zernike moments will still capture its shape accurately.
-
Rotation Invariance − If you rotate the object in the image, the Zernike moments will remain the same.
-
Scaling Invariance − If you resize the object in the image, the Zernike moments will remain the same.
Zernike moments break down the shape into smaller pieces using special mathematical functions called Zernike polynomials.
These polynomials act like building blocks. By combining different Zernike polynomials, we can recreate and represent the unique features of the object”s shape.
Zernike Moments in Mahotas
To calculate the Zernike moments in mahotas, we can use the mahotas.features.zernike_moments() function.
In Mahotas, Zernike moments are calculated by generating a set of Zernike polynomials, which are special mathematical functions representing various shapes and contours.
These polynomials act as building blocks for analyzing the object”s shape.
After that, compute Zernike moments by projecting the shape of the object onto the Zernike polynomials. These moments capture important shape characteristics.
The mahotas.features.zernike_moments() function
The mahotas.features.zernike_moments() function takes two arguments: the image object and the maximum radius for the Zernike polynomials. The function returns a 1−D array of the Zernike moments for the image.
Syntax
Following is the basic syntax of the mahotas.features.zernike_moments() function −
mahotas.features.zernike_moments(im, radius, degree=8, cm={center_of_mass(im)})
Where,
-
im − It is the input image on which the Zernike moments will be computed.
-
radius − It defines the radius of the circular region, in pixels, over which the Zernike moments will be calculated. The area outside the circle defined by this radius, centered around the center of mass, is ignored.
-
degree (optional) − It specifies the maximum number of the Zernike moments to be calculated. By default, the degree value is 8.
-
cm (optional) − It specifies the center of mass of the image. By default, the center of mass of the image is used.
Example
Following is the basic example to calculate the Zernike moments of an image with a default degree value −
import mahotas as mh # Load images of shapes image = mh.imread(''sun.png'', as_grey=True) # Compute Zernike moments moments = mh.features.zernike_moments(image, radius=10) # Compare the moments for shape recognition print(moments)
Output
After executing the above code, we get the output as follows −
[0.31830989 0.00534998 0.00281258 0.0057374 0.01057919 0.00429721 0.00178094 0.00918145 0.02209622 0.01597089 0.00729495 0.00831211 0.00364554 0.01171028 0.02789188 0.01186194 0.02081316 0.01146935 0.01319499 0.03367388 0.01580632 0.01314671 0.02947629 0.01304526 0.00600012]
Using Custom Center of Mass
The center of mass of an image is the point in the image where the mass is evenly distributed. The custom center of mass is a point in an image that is not necessarily the center of mass of the image.
This can be useful in cases where you want to use a different center of mass for your calculations.
For example, you might want to use the custom center of mass of an object in an image to calculate the Zernike moments of the object.
To calculate the Zernike moments of an image using a custom center of mass in mahotas, we need to pass the cm parameter to the mahotas.features.zernike_moments() function.
The cm parameter takes a tuple of two numbers, which represent the coordinates of the custom center of mass.
Example
In here, we are trying to calculate the Zernike moments of an image using the custom center of mass −
import mahotas import numpy as np # Load the image image = mahotas.imread(''nature.jpeg'', as_grey = True) # Calculate the center of mass of the image center_of_mass = np.array([100, 100]) # Calculate the Zernike moments of the image, using the custom center of mass zernike_moments = mahotas.features.zernike_moments(image, radius = 5, cm=center_of_mass) # Print the Zernike moments print(zernike_moments)
Output
Following is the output of the above code −
[3.18309886e-01 3.55572603e-04 3.73132619e-02 5.98944983e-04 3.23622041e-04 1.72293481e-04 9.16757235e-02 3.35704966e-04 7.09426259e-02 1.17847972e-04 2.12625026e-04 3.06537827e-04 1.94379185e-01 1.32093249e-04 8.54616882e-02 1.83274207e-04 1.86728282e-04 3.08004108e-04 4.79437809e-04 1.97726337e-04 3.61630733e-01 5.27467687e-04 8.25534856e-02 7.75593823e-06 1.99419391e-01]
Using a Specific Order
The order of a Zernike moment is a measure of the complexity of the shape that it can represent. The higher the order, the more complex the shape that the moment can represent.
To compute the Zernike moments of an image with a specific order in mahotas, we need to pass the degree parameter to the mahotas.features.zernike_moments() function.
Example
In the following example, we are trying to compute the Zernike moments of an image with a specified order.
import mahotas import numpy as np # Load the image image = mahotas.imread(''nature.jpeg'', as_grey = True) # Calculate the Zernike moments of the image, using the specific order zernike_moments = mahotas.features.zernike_moments(image,1, 4) # Print the Zernike moments print(zernike_moments)
Output
Output of the above code is as shown below −
[0.31830989 0.17086131 0.03146824 0.1549947 0.30067136 0.5376049 0.30532715 0.33032683 0.47908119]