Mahotas – Setting Threshold
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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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
# importing required librariesimport mahotas as mhimport mahotas.demosimport numpy as npfrom pylab import imshow, show# getting nuclear imagenuclear = mh.demos.nuclear_image()# filtering the imagenuclear = nuclear[:, :, 0]print("Image with filter")# showing the imageimshow(nuclear)show()# setting image thresholdnuclear = (nuclear < nuclear.mean())print("Image with threshold")# showing the threshold imageimshow(nuclear)show() |
Output :

Example 2 :
Python3
# importing required librariesimport numpy as npimport mahotasfrom pylab import imshow, show # loading imageimg = mahotas.imread('dog_image.png') # filtering the imageimg = img[:, :, 0] print("Image with filter")# showing the imageimshow(img)show() # setting thresholdimg = (img < img.mean()) print("Image with Threshold")# showing the threshold imageimshow(img)show() |
Output :



