
- 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 - Labeled Image Functions
Image labeling is a data labeling process that involves identifying specific features or objects in an image, adding meaningful information to select and classify those objects.
- It is commonly used to generate training data for machine learning models, particularly in the field of computer vision.
- Image labeling is used in a wide range of applications, including object detection, image classification, scene understanding, autonomous driving, medical imaging, and more.
- It allows machine learning algorithms to learn from labeled data and make accurate predictions or identifications based on the provided annotations.
Functions for Labeling Images
Following are the different functions used to label images in mahotas −
S.No | Function & Description |
---|---|
1 |
label() This function performs connected component labeling on a binary image, assigning unique labels to connected regions in one line. |
2 |
labeled.label() This function assigns consecutive labels starting from 1 to different regions of an image. |
3 |
labeled.filter_labeled() This function applies filters to the selected regions of an image while leaving other regions unchanged. |
Now, lets us see examples of some of these functions.
The label() Function
The mahotas.label() function is used to label the array, which is interpreted as a binary array. This is also known as connected component labeled, where the connectivity is defined by the structuring element.
Example
Following is the basic example to label an image using the label() function −
import mahotas as mh import numpy as np from pylab import imshow, show # Create a binary image image = np.array([[0, 0, 1, 1, 0], [0, 1, 1, 0, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1], [0, 1, 1, 1, 1]], dtype=np.uint8) # Perform connected component labeling labeled_image, num_labels = mh.label(image) # Print the labeled image and number of labels print("Labeled Image:") print(labeled_image) print("Number of labels:", num_labels) imshow(labeled_image) show()
Output
After executing the above code, we get the following output −
Labeled Image: [[0 0 1 1 0] [0 1 1 0 0] [0 0 0 2 2] [0 0 0 0 2] [0 2 2 2 2]] Number of labels: 2
The image obtained is as shown below −

The labeled.label() Function
The mahotas.labeled.label() function is used to update the label values to be in sequential order. The resulting sequential labels will be a new labeled image with labels assigned consecutively starting from 1.
In this example, we start with a labeled image represented by a NumPy array where the labels are non−sequential.
Example
Following is the basic example to label an image using the labeled.label() function −
import mahotas as mh import numpy as np from pylab import imshow, show # Create a labeled image with non-sequential labels labeled_image = np.array([[0, 0, 1, 1, 0], [0, 2, 2, 0, 0], [0, 0, 0, 3, 3], [0, 0, 0, 0, 4], [0, 5, 5, 5, 5]], dtype=np.uint8) # Update label values to be sequential sequential_labels, num_labels = mh.labeled.label(labeled_image) # Print the updated labeled image print("Sequential Labels:") print(sequential_labels) imshow(sequential_labels) show()
Output
The output obtained is as follows −
Sequential Labels: [[0 0 1 1 0] [0 1 1 0 0] [0 0 0 2 2] [0 0 0 0 2] [0 2 2 2 2]]
Following is the image produced −

We have discussed these functions in detail in the remaining chapters of this section.