Learn Mahotas – Euler Number of an Image work project make money

Mahotas – Euler Number of an Image



Imazine you have a drawing with different shapes on it. The Euler number allows us to count how many holes are in those shapes and how many separate parts they can be divided into (connected components). This can help in analyzing and characterizing the structure of the image.

Mathematically, it can be defined as −

E = C - H

where, E is the Euler number, C is the number of connected components, and H is the number of holes in the image.

Euler Number of an Image in Mahotas

In Mahotas, you can calculate the Euler number using the mahotas.euler() function. This function takes a binary image as input, where the objects of interest are represented by white pixels and the background is represented by black pixels. It then calculates the Euler number based on the connectivity and holes in the objects present in the image.

Using the mahotas.euler() Function

The mahotas.euler() function takes a binary image as input and returns the Euler characteristic value as an integer.

The Euler characteristic is a topological measure that describes the connectivity and shape of objects in an image. It is defined as the difference between the number of connected components and the number of holes in the image.

Following is the basic syntax of euler() function in mahotas −

mahotas.euler(f, n=8)

Where, ”f” is a 2−D binary image and ”n” is the connected component in integer which is either 4 or 8 (default is 8).

Example

In the following example, we are computing the Euler number of a binary image ”nature.jpeg” by loading it as a grayscale image and then thresholding it to create a binary image −

import mahotas as mh
import numpy as np
# Load binary image as a NumPy array
binary_image = mh.imread(''nature.jpeg'', as_grey=True) > 0
# Compute Euler number
euler_number = mh.euler(binary_image)
# Print result
print("Eu
ler Number:", euler_number)
Output

Following is the output of the above code −

Euler Number: -2.75

Euler Number with Different Connectivity

We can also calculate the Euler number with different connectivity in Mahotas using the euler() function. The connectivity parameter determines which neighboring pixels are considered in the calculation.

For example, using connectivity−4 considers only the immediate horizontal and vertical neighbors, while connectivity−8 includes diagonal neighbors as well.

Example

Here, we are calculating the Euler number with different connectivity for the “nature.jpeg” image −

import mahotas as mh
import numpy as np
# Load the image as a grayscale image
image = mh.imread(''sun.png'', as_grey=True)
# Threshold the image to create a binary image
thresholded_image = image > 0
# Compute the Euler number with 4-connectivity
euler_number_4conn = mh.euler(thresholded_image, 4)
# Compute the Euler number with 8-connectivity
euler_number_8conn = mh.euler(thresholded_image, 8)
# Print the results
print("Euler Number (4-connectivity):", euler_number_4conn)
print("Euler Number (8-connectivity):", euler_number_8conn)

Output

Output of the above code is as follows −

Euler Number (4-connectivity): -4.75
Euler Number (8-connectivity): -4.75

Euler Number Calculation with Labelled Image

A labeled image assigns unique integer labels to connected components in a binary image.

In Mahotas, the euler function takes a labelled image as input and returns the Euler number for the entire image. The calculation takes into account the number of objects, holes, and tunnels between objects.

Example

In here, we are calculating the Euler number of a labeled image derived from the
“sea.bmp” image −

import mahotas as mh
import numpy as np
# Load the image as a grayscale image
image = mh.imread(''sea.bmp'', as_grey=True)
# Threshold the image to create a binary image
thresholded_image = image > 0
# Label the connected components in the binary image
labeled_image, num_labels = mh.label(thresholded_image)
# Compute the Euler number of the labeled image
euler_number = mh.euler(labeled_image)
# Print the result
print("Euler Number:", euler_number)

Output

After executing the above code, we get the following output −

Euler Number: -44.75

Euler Number Calculation with Binary Image

In Mahotas, the Euler number of a binary image can be calculated using the euler() function. By loading the binary image and converting it to a boolean format, the euler function takes the image as input and returns the Euler number as an integer.

Example

In the example given below, we are calculating the Euler number of a binary image created from the “nature.jpeg” image using Mahotas −

import mahotas as mh
# load binary image and convert to boolean format
image = mh.imread(''sun.png'', as_grey= True)
image = image.astype(bool)
# calculate the Euler number
euler_number = mh.euler(image)
# print the result
print("Euler number of the binary image is:", euler_number)

Output

The result obtained is as follows −

Euler number of the binary image is: -4.75

Leave a Reply

Your email address will not be published. Required fields are marked *