Learn Sizes of Labelled Region work project make money

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

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

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

Leave a Reply

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