![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 - Sizes of Labelled Region
Sizes of labeled regions refer to the number of pixels present in different regions of a labeled image. A labeled image refers to an image in which a unique label (value) is assigned to distinct regions (a group of pixels) of an image.
Usually, an image has two primary regions − the foreground and the background.
The size of each region depends on the total number of regions present in the image. If more number of regions are present, then the size of each region will be smaller.
Conversely if less number of regions are present, then the size of each region will be bigger.
Sizes of Labeled Region in Mahotas
In Mahotas, we can use the mahotas.labeled.labeled_size() function to calculate the size of each region in a labeled image. The function works in the following way −
It first counts the number of labeled regions in the image.
Then, it traverses through all the labeled regions and calculates the total number of pixels present in each region.
Once all the regions have been traversed the size of each region is returned by the function.
The mahotas.labeled.labeled_size() function
The mahotas.labeled.labeled_size() function takes a labeled image as an input and returns a list containing the size of each region in pixels.
We can traverse through the list of values to get the size of each region.
Syntax
Following is the basic syntax of the labeled_size() function in mahotas −
mahotas.labeled.labeled_size(labeled)
where,
labeled − It is the input labeled image.
Example
In the following example, we are finding the sizes of labeled regions of an image using the mh.labeled.labeled_size() function.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread('sea.bmp') # Labeling the image labeled, num_objects = mh.label(image) # Getting the sizes of labeled regions labeled_size = mh.labeled.labeled_size(labeled) # Printing the sizes of labeled regions for i, labeled_size in enumerate(labeled_size, 1): print(f"Size of Region {i} is = {labeled_size} pixels") # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 1) # Displaying the original image axes.imshow(image) axes.set_title('Labeled Image') axes.set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
Output
Following is the output of the above code −
Size of Region 1 is = 4263 pixels Size of Region 2 is = 2234457 pixels
Following is the image obtained −
![Labeled Region](/mahotas/images/labeled_region.jpg)
Sizes in Grayscale Image
We can also possible to find the sizes of labeled regions in a grayscale image. Grayscale images refer to the image having only a single−color channel, where each pixel is represented by a single intensity value.
The intensity value of a pixel determines the shade of gray. 0 will result in black pixels, 255 will result in white pixels, while any other value will result in pixels having an intermediate shade.
In mahotas, to get the sizes of labeled regions of a grayscale image, we first convert an input RGB image to grayscale using the colors.rgb2gray() function.
Then, we label the grayscale image and traverse over each region to calculate its size.
Example
In the example mentioned below, we are finding the size of labeled regions of a grayscale image.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Loading the image image = mh.imread('sun.png') # Converting it to grayscale image image = mh.colors.rgb2gray(image) # Labeling the image labeled, num_objects = mh.label(image) # Getting the sizes of labeled regions labeled_size = mh.labeled.labeled_size(labeled) # Printing the sizes of labeled regions for i, labeled_size in enumerate(labeled_size, 1): print(f"Size of Region {i} is = {labeled_size} pixels") # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 1) # Displaying the original image axes.imshow(image, cmap='gray') axes.set_title('Original Image') axes.set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
Output
Output of the above code is as follows −
Size of Region 1 is = 8 pixels Size of Region 2 is = 1079032 pixels
The image produced is as follows −
![Sizes Grayscale Image](/mahotas/images/grayscale_image_sizes.jpg)
Sizes in a Random Boolean Image
In addition to grayscale images, we can also get the sizes of labeled regions in a random boolean image.
A random Boolean image refers to an image where each pixel has a value of either 1 or 0, where pixels with the value '1' are referred to as the foreground and pixels with the value '0' are referred to as the background.
In mahotas, we first generate a random Boolean image of a specific dimension using the np.zeros() function.
The generated random image initially has all its pixel values set to 0 (consists of only the background region). We then assign integer values to few portions of the image to create distinct regions.
Then, we label the image and traverse over each region to get its size in pixels.
Example
In here, we are getting the size of different labels of a randomly generated boolean image.
import mahotas as mh import numpy as np import matplotlib.pyplot as mtplt # Creating a random image image = np.zeros((10,10), bool) # Creating regions image[:2, :2] = 1 image[4:6, 4:6] = 1 image[8:, 8:] = 1 # Labeling the image labeled, num_objects = mh.label(image) # Getting the sizes of labeled regions labeled_size = mh.labeled.labeled_size(labeled) # Printing the sizes of labeled regions for i, labeled_size in enumerate(labeled_size, 1): print(f"Size of Region {i} is = {labeled_size} pixels") # Creating a figure and axes for subplots fig, axes = mtplt.subplots(1, 1) # Displaying the original image axes.imshow(image) axes.set_title('Original Image') axes.set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
Output
After executing the above code, we get the following output −
Size of Region 1 is = 88 pixels Size of Region 2 is = 4 pixels Size of Region 3 is = 4 pixels Size of Region 4 is = 4 pixels
The image obtained is as shown below −
![Sizes Grayscale Image1](/mahotas/images/grayscale_image_sizes1.jpg)