Adaptive Thresholding using OpenCV Last Updated : 03 Dec, 2025 Comments Improve Suggest changes 14 Likes Like Report Adaptive thresholding addresses one of the main limitations of simple (global) thresholding its inability to handle images with varying lighting conditions. Instead of using a single global threshold value for the whole image, adaptive thresholding calculates the threshold for small regions around each pixel. This approach provides better results for images where illumination changes across different parts.Instead of a constant threshold for all pixels, the image is divided into smaller blocks and a threshold is computed for each region.Helps segment images effectively even if lighting is not uniform, making it ideal for documents or scenes with shadows and bright spots.Step-by-Step ImplementationStep 1: Import libraries and Image PreparationSample image can be downloaded from here.Let's import the required libraries and load our image on which we will perform the operations,cv2: Handles image reading, processing and applies thresholding techniques.matplotlib.pyplot: Displays images and results in Colab notebooks. Python import cv2 import matplotlib.pyplot as plt image = cv2.imread('input1.jpg') gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Step 2: Helper FunctionDefine the helper function which helps in displaying the images, Python def show_image(img, title): plt.imshow(img, cmap='gray') plt.title(title) plt.axis('off') plt.show() Step 3: Display Original Image Python show_image(gray_image, "Original Grayscale Image") Output:Original Grayscale ImageStep 4: Adaptive Mean ThresholdingThe threshold for each pixel is computed as the mean value of the surrounding block (199×199 neighborhood, here), minus the constant (5).Best for images with fairly consistent noise or lighting within local regions. Python thresh_mean = cv2.adaptiveThreshold( gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 199, 5 ) show_image(thresh_mean, "Adaptive Mean Thresholding") Output:Adaptive Mean ThresholdingStep 5: Adaptive Gaussian ThreosholdingThe threshold for each pixel comes from a weighted sum of surrounding pixels (Gaussian window), minus the constant (5).More effective than mean in regions with gradual intensity variations. Python thresh_gauss = cv2.adaptiveThreshold( gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 199, 5 ) show_image(thresh_gauss, "Adaptive Gaussian Thresholding") Output:Adaptive Gaussian Thresholding Create Quiz Comment R rishabhsingh1304 Follow 14 Improve R rishabhsingh1304 Follow 14 Improve Article Tags : Technical Scripter Python Image-Processing OpenCV Python-OpenCV +1 More Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like