Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Image Thresholding?
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: Mar 27, 2024
Easy

Image Thresholding

Author aniket verma
0 upvote

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 which 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. It is a very common segmentation technique which is used abundantly especially in computer vision tasks. Let’s dive deep into what image thresholding is.

Also See, Image Sampling 

What is Image Thresholding?

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.  

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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)

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()

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()

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()

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()

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()

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, 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