- In Image Processing, Thresholding is a kind of Segmentation – it separates pixels into two or more categories.
- In its simplest form, a Thresholding operation of an Image involves classification of the pixels into two groups based on a Threshold:
- Pixels that exceed a given intensity Threshold
- Pixels that do not exceed an intensity Threshold and transforming those two kinds of pixels into two colors, say black and white, typically black for the background and the white for the object(s) identified.
- Using single threshold to separate pixels and mapping them to two colors is also called as Global Thresholding.
- Thresholding results in finding objects from the background surrounding them.
- However thresholding cannot be applied for all the images. This is because, not all images will have a clear threshold based on which pixels can be mapped to two categories.
- To decide whether thresholding will help in identifying objects vs the background of an image the Image Histogram can be used.
- An image histogram simply plots pixel values in X-axis against the count of pixels in Y-axis.
- Using an Image Histogram one or more thresholds can be identified or none at all.
- If an image has regions with uniform intensity this results in clearly separated strong peaks in the image histogram, which helps in defining a global threshold.
How to use Python Image Processing Library – Pillow for Thresholding an Image:
- The point()method of the Python Image processing library-Pillow, can be used to modify each pixel of an image.
- point() method takes a function object inside which the semantics modifying each pixel of the image can be specified.
- For a specified threshold of intensity, the Python program given here changes each pixel of an Image into either white or black thus forming the object and background.
- The Python Example Program given here does thresholding on each band of the image – Red, Green and Blue.
- Only a range of blue values are set to 1 and the remaining bands are set to 0.
- The input to the example contains a set of rings with varying colors.
- After thresholding when the blue band is displayed in the output the blue rings are displayed as white due to thresholding and all other rings that are not prominently blue are not prominently white.
- After thresholding the Red, Green and Blue bands the Python Program also merges the bands together and displays the new image formed.
- The new merged image will have only the blue rings.
# Example Python Program for thresholding
from PIL import Image
# Method to process the red band
# Method to process the blue band
# Method to process the green band
# Create an image object
imageObject = Image.open("./circles1.jpg")
# Split the red, green and blue bands from the Image
multiBands = imageObject.split()
# Apply point operations that does thresholding on each color band
redBand = multiBands.point(pixelProcRed)
greenBand = multiBands.point(pixelProcGreen)
blueBand = multiBands.point(pixelProcBlue)
# Display the individual band after thresholding
# Create a new image from the thresholded red, green and blue brands
newImage = Image.merge("RGB", (redBand, greenBand, blueBand))
# Display the merged image after thresholding
Red band of the Image after Thresholding:
Green band of the Image after Thresholding:
Blue band of the Image after Thresholding:
Merged Image after thresholding operation to have blue pixel in the foreground: