![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 - 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