Related Articles

# Mahotas – Zernike Moments

• Last Updated : 30 Jun, 2021

In this article we will see how we can get the zernike moments of the given image in mahotas. Zernike polynomials are an orthogonal basis set (a set of functions for which the integral of the product of any pair of functions is zero). In image processing, computer vision and related fields, an image moment is a certain particular weighted average of the image pixels’ intensities, or a function of such moments, usually chosen to have some attractive property or interpretation. Image moments are useful to describe objects after segmentation.
For this tutorial we will use ‘lena’ image, below is the command to load the lena image

`mahotas.demos.load('lena')`

Below is the lena image In order to do this we will use mahotas.features.zernike_moments method
Argument : It takes image object and integer as argument
Return : It returns 1-D array

Note : Input image should be filtered or should be loaded as grey
In order to filter the image we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this

`image = image[:, :, 0]`

Below is the implementation

## Python3

 `# importing required libraries``import` `mahotas``import` `mahotas.demos``from` `pylab ``import` `gray, imshow, show``import` `numpy as np``import` `matplotlib.pyplot as plt``  ` `# loading image``img ``=` `mahotas.demos.load(``'lena'``)``  ` `# filtering image``img ``=` `img.``max``(``2``)` `print``(``"Image"``)``  ` `# showing image``imshow(img)``show()` `# radius``radius ``=` `10` `# computing zernike moments``value ``=` `mahotas.features.zernike_moments(img, radius)`` `  `# printing value``print``(value)`

Output :

`Image` ```[0.31830989 0.01261485 0.00614926 0.00769591 0.0097145  0.01757332
0.00617458 0.01008905 0.01415304 0.01099679 0.02894761 0.01838737
0.0074247  0.01333135 0.01958184 0.00431827 0.00540781 0.01675913
0.03511082 0.00699177 0.00357231 0.01593838 0.01621848 0.0240565
0.0154929 ]```

Another example

## Python3

 `# importing required libraries``import` `mahotas``import` `numpy as np``from` `pylab ``import` `gray, imshow, show``import` `os``import` `matplotlib.pyplot as plt`` ` `# loading image``img ``=` `mahotas.imread(``'dog_image.png'``)`  `# filtering image``img ``=` `img[:, :, ``0``]``  ` `print``(``"Image"``)``  ` `# showing image``imshow(img)``show()` `# radius``radius ``=` `10` `# computing zernike moments``value ``=` `mahotas.features.zernike_moments(img, radius)`` `  `# printing value``print``(value)`

Output :

`Image` ```[0.31830989 0.00985427 0.00714652 0.00171408 0.00442245 0.01796711
0.00716781 0.00179965 0.0039829  0.0031081  0.02447476 0.0011686
0.009291   0.00174885 0.00357579 0.00692029 0.0043969  0.03528869
0.00264739 0.01381883 0.00750501 0.0036528  0.00867514 0.01298398
0.0129556 ]```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up