Table of contents
1.
Introduction
2.
What is Thresholding in Image Processing?
3.
Simple Image thresholding
3.1.
cv2.THRESH_BINARY
3.2.
cv2.THRESH_BINARY_INV
3.3.
cv2.THRESH_TRUNC
3.4.
cv2.THRESH_TOZERO
3.5.
cv2.THRESH_TOZERO_INV
4.
Limitations of Image Thresholding
5.
Frequently Asked Questions
5.1.
Why is Image thresholding used?
5.2.
Mention different types of Image thresholding techniques.
6.
Conclusion
Last Updated: Sep 9, 2024
Easy

Thresholding in Image Processing

Author aniket verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Look at the picture below and ask yourself a question if you have encountered such a filter that produces a similar output when you click a selfie.

  Source: link

Well if you haven’t encountered such a filter that produces a similar output try clicking selfies of yours in different contrast modes using your device. In this blog, we will discuss one such computer vision technique that produces such an output called Image thresholding or Thresholding in Image Processing. It is a very common segmentation technique that is used abundantly, especially in computer vision tasks. Let’s dive deep into what image thresholding is.

Also See, Image Sampling 

What is Thresholding in Image Processing?

Image thresholding is a very common image segmentation technique in computer vision that is used to detect objects and separate foreground and background in a given image. As the name suggests a threshold pixel value is set and then all pixels lower than this value are set to small value say 0 and those pixels with greater value than the threshold value are assigned a large value say 255. Generally image thresholding is applied on grayscale images.  There are various types of image thresholding and they are as follows:

  1. Simple thresholding
  2. Adaptive thresholding
  3. Adaptive mean thresholding
  4. Gaussian thresholding
  5. Otsu’s thresholding

 

We will discuss Simple Image thresholding in this blog.  

Simple Image thresholding

Simple Image thresholding is a classical method of image thresholding in which pixels above and below a set threshold are assigned new values. This technique is used when there is high contrast between the background and the foreground in the given image.

Note: the shown methods will be used on grayscale images.

Let’s look at different types of simple image thresholding techniques provided in the cv2 library. Any method/technique can be represented using a mathematical formulation generally we can say that:

T <- threshold

MaxIntensityValue <- the highest intensity value by which the pixel value can be replaced.

MinIntensityValue <- the lowest intensity value by which the pixel value can be replaced. 

I(x, y) denotes the intensity value of a pixel at coordinate x, y.

The operation can be formulated as 

If I(x, y) < T:

      I(x, y) <- MinIntensityValue

else:

      I(x, y) <- MaxIntensityValue

We will use the leaf image which you can download from here

import cv2 
import numpy as np
import matplotlib.pyplot as plt
# reading image from the dataset folder and
# converting it to a grayscale image
leaf_img = cv2.imread('../input/leafdataset/leaf.png', 0)
You can also try this code with Online Python Compiler
Run Code

cv2.THRESH_BINARY

This method in cv2 library sets the values below the threshold to 0 and and above threshold to 255.

Mathematically, 

 If I(x, y) < T:

      I(x, y) <- 0

else:

      I(x, y) <- 255

#threshold value is set as 128 and max value is given as 255
ret, thresh1 = cv2.threshold(leaf_img, 128, 255, cv2.THRESH_BINARY)
plt.imshow(thresh1)
plt.show()
You can also try this code with Online Python Compiler
Run Code

cv2.THRESH_BINARY_INV

This method in cv2 library works the opposite way of cv2.THRESH_BINARY works.

Mathematically, 

 If I(x, y) < T:

      I(x, y) <- 255

else:

      I(x, y) <- 0

#threshold value is set as 128 and max value is given as 255
ret, thresh2 = cv2.threshold(leaf_img, 128, 255, cv2.THRESH_BINARY_INV)
plt.imshow(thresh2)
plt.show()
You can also try this code with Online Python Compiler
Run Code

cv2.THRESH_TRUNC

This method in the cv2 library truncates all values above the threshold value to the threshold value without changing the rest of the pixel values.

Mathematically, 

 If I(x, y) < T:

      I(x, y) <- I(x, y)

else:

      I(x, y) <- T

#threshold value is set as 128 and max value is given as 255
ret, thresh3 = cv2.threshold(leaf_img, 128, 255, cv2.THRESH_TRUNC)
plt.imshow(thresh3)
plt.show()
You can also try this code with Online Python Compiler
Run Code

cv2.THRESH_TOZERO

This method in the cv2 library sets the values below the threshold to 0. 

Mathematically, 

 If I(x, y) < T:

      I(x, y) <- 0

else:

      I(x, y) <- I(x, y)

#threshold value is set as 128 and max value is given as 255
ret, thresh4 = cv2.threshold(leaf_img, 128, 255, cv2.THRESH_TOZERO)
plt.imshow(thresh4)
plt.show()
You can also try this code with Online Python Compiler
Run Code

cv2.THRESH_TOZERO_INV

This method in cv2 library works the opposite way of cv2.THRESH_TOZERO works.

Mathematically, 

 If I(x, y) < T:

     I(x, y) <- I(x, y)

else:

     I(x, y) <- 0

#threshold value is set as 128 and max value is given as 255
ret, thresh5 = cv2.threshold(leaf_img, 128, 255, cv2.THRESH_TOZERO_INV)
plt.imshow(thresh5)
plt.show()
You can also try this code with Online Python Compiler
Run Code

Limitations of Image Thresholding

Image thresholding is used very commonly, but it has some limitations which one should be aware of.

  1. It gives good results in case of images with less noise generally.
  2. When there is clear contrast between the foreground and background in the given image.
  3. homogeneous lighting, etc.
    Also read, Sampling and Quantization

Frequently Asked Questions

Why is Image thresholding used?

Image thresholding is used to perform image segmentation, and bifurcate the background and foreground in an image. It’s also used for object detection. 
 

Mention different types of Image thresholding techniques.

There are different types of image thresholding techniques. Some of them are Simple thresholding, Adaptive thresholding, Adaptive mean thresholding, Gaussian thresholding, Otsu’s thresholding.

Conclusion

In this blog, we extensively discussed what Image thresholding is and focussed on one of the types of image thresholding, i.e, Simple Image thresholding. We then looked at a mathematical formulation of image thresholding. We discussed different techniques under simple image thresholding present in the cv2 library of python along with their implementations. At last we looked at some limitations of image thresholding. We hope you liked this blog and to explore more and dive deep into computer vision and machine learning you can visit our blogs on the Coding Ninjas Studio Library.    

Happy Learning!!

Live masterclass