In this article we will see how we can set threshold to the images in mahotas. For this we are going to use the fluorescent microscopy image from a nuclear segmentation benchmark. We can get the image with the help of command given below
mahotas.demos.nuclear_image()
Image thresholding is a simple form of image segmentation. It is a way to create a binary image from a grayscale or full-color image. This is typically done in order to separate “object” or foreground pixels from background pixels to aid in image processing.
Below is the nuclear_image

In order to set threshold to the image we will take the image object which is numpy.ndarray and will divide the array with the threshold value, here threshold value is the means value, below is the command to do this
img = (img < img.mean())]
Example 1 :
Python3
import mahotas as mh
import mahotas.demos
import numpy as np
from pylab import imshow, show
nuclear = mh.demos.nuclear_image()
nuclear = nuclear[:, :, 0 ]
print ( "Image with filter" )
imshow(nuclear)
show()
nuclear = (nuclear < nuclear.mean())
print ( "Image with threshold" )
imshow(nuclear)
show()
|
Output :

Example 2 :
Python3
import numpy as np
import mahotas
from pylab import imshow, show
img = mahotas.imread( 'dog_image.png' )
img = img[:, :, 0 ]
print ( "Image with filter" )
imshow(img)
show()
img = (img < img.mean())
print ( "Image with Threshold" )
imshow(img)
show()
|
Output :

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Apr, 2021
Like Article
Save Article