The roundness of an image refers to the measure of how closely an object or a region in the image resembles a perfect circle. It is a metric used to quantify the degree of circularity or deviation from circularity.
The roundness value is calculated by comparing the object”s shape to that of a circle.
A perfectly round object would have a roundness value close to 1, while objects that are more elongated or irregular in shape would have roundness values closer to 0.
Roundness of Image in Mahotas
In Mahotas, we can calculate the roundness of an object using the mahotas.features.roundness() function. This function takes a binary image as input.
A binary image is an image where each pixel is either classified as foreground (object of interest) or background. Generally, the foreground pixels are represented by white (pixel value = 1), and the background pixels are represented by black (pixel value = 0).
The input binary image should be in a boolean format or represented as a NumPy array with boolean values.
The mahotas.features.roundness() function
The ”mahotas.features.roundness()” function accepts a binary image as input and returns a float value between 0 and 1. The closer the value is to 1.0, the closer the shape is to a perfect circle.
Syntax
Following is the basic syntax of the roundness() function in mahotas −
mahotas.features.roundness(image)
Where, ”image” is the Boolean image input.
Example
In the following example, we are finding the roundness of an image using the roundness() function −
import mahotas as mh import numpy as np image = mh.imread(''sun.png'', as_grey = True) roundness = mh.features.roundness(image) print("Roundness of the image= ", roundness)
Output
The output obtained is as follows −
Roundness of the image= 4.98542867728303e-05
Blob Roundness in Binary Image
Blob roundness refers to a measure of how closely a blob resembles a perfect circle. A roundness value close to 1 indicates a more circular blob, while a value significantly lower than 1 indicates a more elongated or irregular shape.
To compute blob roundness in a binary image using Mahotas, we need to take an image having a clear separation between the foreground (blobs) and the background. Then, label the blobs in the binary image to assign a unique identifier (index) to each blob.
It helps in distinguishing individual blobs. Thereafter, compute the roundness of each labeled blob.
Example
In here, we are trying to compute the blob roundness in a binary image −
import mahotas as mh import numpy as np image = mh.imread(tree.tiff'', as_grey=True) # Labelling the blobs in the image labeled, _ = mh.label(image) # Computing the roundness of each blob roundness = mh.features.roundness(labeled) print("Blob Roundness:", roundness)
Output
Output of the above code is as follows −
Blob Roundness: 2.0659091361803767
Using zernike_moment
Zernike moments are mathematical representations of the shape of an object in an image.
It captures the roundness and other shape attributes of an image by analyzing the distribution of intensity or color variations within the object.
To determine the roundness of an image using Zernike Moments, we start by specifying a radius value. This radius determines the size of the circular region in which the moments will be computed.
Choosing a smaller radius is ideal for analyzing smaller objects, while a larger radius is more suitable for larger objects.
Once we have the Zernike Moments calculated, the first moment becomes particularly important when assessing image roundness. It serves as a representative measure of the overall roundness of the object within the image.
By extracting the first element from the Zernike Moments list, we obtain a specific value that quantifies the roundness of the object accurately.
Example
Here, we are trying to find an image roundness with Zernike Moments −
import mahotas as mh image = mh.imread(''nature.jpeg'', as_grey = True) # Setting the radius for calculating Zernike moments radius = 10 # Calculating the Zernike moments moments = mh.features.zernike_moments(image, radius=radius) # The first Zernike moment represents roundness roundness = moments[0] print(roundness)
Output
After executing the above code, we get the output as shown below −
0.3183098861837907