Thresholding Of An Image Using Python And Pillow

Overview:

  • 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:

# Example Python Program for thresholding

from PIL import Image

 

# Method to process the red band

def pixelProcRed(intensity):

    return 0

 

# Method to process the blue band

def pixelProcBlue(intensity):

    return intensity

 

# Method to process the green band

def pixelProcGreen(intensity):

    return 0

 

# 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[0].point(pixelProcRed)

greenBand    = multiBands[1].point(pixelProcGreen)

blueBand     = multiBands[2].point(pixelProcBlue)

 

# Display the individual band after thresholding

redBand.show()

greenBand.show()

blueBand.show()

 

# 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

newImage.show()

Input:

Original Image:

Original Image before thresholding

Courtesy: gregoryfollon

Output:

Red band of the Image after Thresholding:

Red Band of the image After Thresholding

Green band of the Image after Thresholding:

Green Band of an Image after thresholding

Blue band of the Image after Thresholding:

Blue band of an image after thresholding

Merged Image after thresholding operation to have blue pixel in the foreground:

Merged Bands of an image after thresholding to retain only blue in the foreground


Copyright 2018 © pythontic.com