Do you think IIT Guwahati certified course can help you in your career?
No
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.
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:
Simple thresholding
Adaptive thresholding
Adaptive mean thresholding
Gaussian thresholding
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
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
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
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
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
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
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.