Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Adaptive Thresholding?
3.
What are the major advantages of adaptive thresholding?
4.
Mathematical inference for adaptive thresholding
5.
Implementation of adaptive thresholding using OpenCV and Python 
6.
Frequently Asked Questions
6.1.
Is adaptive thresholding better than the global thresholding technique?
6.2.
What is the block size in adaptive thresholding?
6.3.
Where is adaptive thresholding significantly used?
7.
Conclusion
Last Updated: Mar 27, 2024

Adaptive thresholding

Introduction

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.

Also See, Image Sampling 

What is Adaptive Thresholding?

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.

Source: Link

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

What are the major advantages of adaptive thresholding?

The major two advantages of using adaptive thresholding are mentioned as follows:

  1. 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.
  2. 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()

 

The link for the image is given by - text 1.

The output of the above program is - 

Let us consider another image and the link for the image is given by - landscape.

The output of the above program for the second image is -

Also read, Sampling and Quantization

Frequently Asked Questions

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.

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must have a look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Bitwise Operations on Images
Next article
Image Segmentation Techniques
Live masterclass