Image thresholding is a form of image segmentation technique used for changing the pixels of an image such that the image can be processed easily and it becomes easier to interpret the information and analyze the image. Image thresholding is also done in order to separate or differentiate the main object or the foreground pixels of the image from the background pixels to aid in image interpretation. There are multiple types of image thresholding methods, out of which we will discuss adaptive thresholding in this blog.
Adaptive thresholding is a form of image thresholding technique in which rather than specifying the threshold value manually or using any restrictions, the threshold value is adjusted and selected automatically according to the image pixels and layout for converting the image pixels to grayscale or a binary image. Essentially, this method supports the automatic selection of the threshold value for segregating the main object from its background in conditions where there are different lighting conditions, colors or contrast in the image.
Threshold value for an image is the intensity of the pixel at which the pixels corresponding to foreground and background are separated. This means that image pixels with higher intensities than the threshold value will be separated from the image pixels with lower intensities than the threshold value. If we implement adaptive thresholding then, instead of manually providing a single value of the threshold through basic effort, we will be able to differentiate the foreground and background of the image more efficiently using a much more accurate threshold value.
An example of adaptive thresholding is given in the following figure.
What are the major advantages of adaptive thresholding?
The major two advantages of using adaptive thresholding are mentioned as follows:
We can obtain better segmentation using adaptive thresholding because the threshold value is selected by analyzing the intensities of all the image pixels and then selecting the most suitable threshold value which can efficiently separate the foreground and background.
This technique is time-saving and can segregate the main object from the rest of the image quickly.
Mathematical inference for adaptive thresholding
The objective of using adaptive thresholding is to statistically examine the local regions of the image and find an optimal value of threshold for each region through which we can easily separate the image regions. We can use either arithmetic or gaussian mean of the pixel intensities in each region to compute the threshold value T for each region.
When considering arithmetic mean, each pixel in the neighborhood has equal contribution towards the threshold value and in the gaussian mean, image pixel values which are farther away from the (x, y)-coordinate center of the region make less contribution to the overall calculation of T.
Therefore, the general formula for computing T is -
T = mean(IL) - C
Where, the mean is either arithmetic or gaussian, IL is the local sub-region of the image I and C is some constant which we can use to fine tune the threshold value T.
Implementation of adaptive thresholding using OpenCV and Python
The following code demonstrates the implementation of adaptive thresholding using OpenCV and Python.
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('text 1.jpg',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv.THRESH_BINARY,11,2)
titles = ['Original Image', ' Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', ' Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
You can also try this code with Online Python Compiler
Is adaptive thresholding better than the global thresholding technique?
No, the global thresholding technique is better than adaptive thresholding because it determines the threshold value based on the histogram of the overall pixel intensity distribution of the image whereas adaptive thresholding determines value for each fractional region.
What is the block size in adaptive thresholding?
Block size determines the number of neighboring pixels which are included in the individual images while calculating the threshold value for the pixel.
Where is adaptive thresholding significantly used?
Adaptive thresholding can be used in preparing medical images, images for training various datasets and enhancing the images used for forecasting.
Conclusion
In this blog, we discussed adaptive thresholding and also implemented it using OpenCV and Python.