Friday, November 21, 2025
HomeLanguagesMahotas – Removing Region at given position

Mahotas – Removing Region at given position

In this article we will see how we can remove the region at given position in mahotas. Labelled images are integer images where the values correspond to different regions. I.e., region 1 is all of the pixels which have value 1, region two is the pixels with value 2, and so on. By convention, region 0 is the background and often handled differently. We can create a labelled region with the help of mahotas.label method.
In order to do this we will use mahotas.labelled.remove_regions method 
 

Syntax : mahotas.labelled.remove_regions(labeled_img, i, j)
Argument : It takes labelled image and two integer representing the region
Return : It returns numpy.ndarray i.e image object 
 

Example 1: 
 

Python3




# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
import os
 
 
# loading image
img = mahotas.imread('dog_image.png')
     
 
# setting filter to the image
img = img[:, :, 0]
 
 
# setting gaussian filter
img = mahotas.gaussian_filter(img, 15)
 
# setting threshold value
img = (img> img.mean())
 
# creating a labelled image
labeled1, n_nucleus1 = mahotas.label(img)
 
# showing the labelled image
print("Labelled Image")
imshow(labelled1)
show()
 
 
 
# removing region
labelled2 = mahotas.labelled.remove_regions(labelled1, 1, 1)
 
 
# showing the labelled image
print("Labelled Image with removed region")
imshow(labelled2)
show()


Output : 
 

Example 2: 
 

Python3




# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
  
# creating region
# numpy.ndarray
regions = np.zeros((10, 10), bool)
  
# setting 1 value to the region
regions[:3, :3] = 1
regions[6:, 6:] = 1
  
# getting labelled function
labelled, nr_objects = mahotas.label(regions)
  
print("Labelled Image")
# showing the image with interpolation = 'nearest'
imshow(labelled, interpolation ='nearest')
show()
  
# removing region
labelled2 = mahotas.labelled.remove_regions(labelled, 1, 1)
 
 
# showing the labelled image
print("Labelled Image with removed region")
imshow(labelled2)
show()


Output : 
 

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11997 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS