๐ŸŒ
PyImageSearch
pyimagesearch.com โ€บ home โ€บ blog โ€บ adaptive thresholding with opencv ( cv2.adaptivethreshold )
Adaptive Thresholding with OpenCV ( cv2.adaptiveThreshold ) - PyImageSearch
June 9, 2023 - The fourth value to cv2.adaptiveThreshold is the threshold method, again just like the simple thresholding and Otsu thresholding methods. Here we pass in a value of cv2.THRESH_BINARY_INV to indicate that any pixel value that passes the threshold test will have an output value of 0.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-thresholding-techniques-using-opencv-set-2-adaptive-thresholding
Adaptive Thresholding using OpenCV - GeeksforGeeks
December 3, 2025 - The 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.
๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ opencv-adaptive-threshold-fae667b91984
OpenCV Adaptive Threshold | by Amit Yadav | Medium
April 18, 2025 - The heart of the function lies in the adaptiveMethod parameter, where you decide how the thresholding is calculated locally: cv2.ADAPTIVE_THRESH_MEAN_C: This method calculates the threshold based on the mean of the local neighborhood pixels.
๐ŸŒ
OpenCV
docs.opencv.org โ€บ 4.13.0 โ€บ d7 โ€บ d4d โ€บ tutorial_py_thresholding.html
OpenCV: Image Thresholding
In the previous section, we used ... in different areas. In that case, adaptive thresholding can help. Here, the algorithm determines the threshold for a pixel based on a small region around it....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ opencv โ€บ opencv_adaptive_threshold.htm
OpenCV - Adaptive Threshold
In OpenCV, you can perform Adaptive threshold operation on an image using the method adaptiveThreshold() of the Imgproc class.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ opencv-adaptive-thresholding-in-python-with-cv2adaptivethreshold
OpenCV Adaptive Thresholding in Python with cv2.adaptiveThreshold()
November 16, 2023 - In much the same way, we can apply Gaussian thresholding: mask = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 10)
๐ŸŒ
DEV Community
dev.to โ€บ catheryn โ€บ calculating-adaptive-threshold-in-opencv-1hh3
Calculating Adaptive Threshold in OpenCV - DEV Community
July 12, 2024 - Next, we specify our adaptive thresholding method: img_thresh_adp = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 7)
๐ŸŒ
Medium
medium.com โ€บ featurepreneur โ€บ dive-deeper-into-opencvs-adaptive-thresholding-3f29dda8c908
Dive deeper into OpenCVโ€™s Adaptive Thresholding | by Maximin Joshua | featurepreneur | Medium
December 1, 2021 - Then depending on the threshold type passed, either one of the following operations in the below image is performed: ... import cv2 as cv#read an image and convert it to grayscale img = cv.imread('D://medium_blogs//phone_dimmed4.jpg') gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)#apply normal and adaptive thresholding to the image ret, thresh = cv.threshold(gray, 70, 255, cv.THRESH_BINARY) adapt = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,99,3)#create resizable windows for the images to be displayed cv.namedWindow('adapt',cv.WINDOW_NORMAL) cv.namedWindow('thresh', cv.WINDOW_NORMAL)#show the image cv.imshow('thresh', thresh) cv.imshow('adapt', adapt)if cv.waitKey(0): cv.destroyAllWindows()
Find elsewhere
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ what-is-adaptive-thresholding-opencv
What is adaptive thresholding in OpenCV -
June 22, 2022 - The cv2.ADAPTIVE_THRESH_GAUSSIAN_C method calculates the threshold by taking the weighted sum of the pixel values in the neighborhood area where the weights are assigned using the gaussian window technique.
๐ŸŒ
Medium
medium.com โ€บ @manik.butex41 โ€บ an-introduction-to-thresholding-operations-in-opencv-64aff0cb2826
An Introduction to Thresholding Operations in OpenCV | by Manik Hossain | Medium
November 6, 2023 - The adaptive_method parameter is used to specify the method used to calculate the threshold value for each pixel. The cv2.ADAPTIVE_THRESH_MEAN_C method calculates the threshold value for each pixel by taking the mean of the pixelโ€™s neighbourhood.
Top answer
1 of 2
23
Python: cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) โ†’ dst

Parameters:

src โ€“ Source 8-bit single-channel image.
dst โ€“ Destination image of the same size and the same type as src .
maxValue โ€“ Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
adaptiveMethod โ€“ Adaptive thresholding algorithm to use, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C . See the details below.
thresholdType โ€“ Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV .
blockSize โ€“ Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
C โ€“ Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.

Taken from here: and it also explains the method in a lot more detail.

2 of 2
4

Add to the answer from GPPK.

The function transforms a grayscale image to a binary image according to the formulae:

  • THRESH_BINARY

  • THRESH_BINARY_INV

where T(x,y) is a threshold calculated individually for each pixel.

  • For the method ADAPTIVE_THRESH_MEAN_C , the threshold value T(x,y) is a mean of the blockSize x blockSize neighborhood of (x, y) minus C .
  • For the method ADAPTIVE_THRESH_GAUSSIAN_C , the threshold value T(x, y) is a weighted sum (cross-correlation with a Gaussian window) of the blockSize x blockSize neighborhood of (x, y) minus C . The default sigma (standard deviation) is used for the specified blockSize .
๐ŸŒ
Python Geeks
pythongeeks.org โ€บ python geeks โ€บ learn opencv โ€บ thresholding in opencv
Thresholding in OpenCV - Python Geeks
October 11, 2022 - Adaptive thresholding at pixel level yields superior results as compared to global thresholding, particularly in cases where the image has regions of varying levels of contrasts. In OpenCV, cv2.adaptiveThreshold() is used for adaptive thresholding.
๐ŸŒ
TheAILearner
theailearner.com โ€บ tag โ€บ cv2-adaptivethreshold-opencv
cv2.adaptiveThreshold opencv โ€“ TheAILearner
adaptiveMethod: This tells us how the threshold is calculated from the pixel neighborhood. This currently supports two methods: cv2.ADAPTIVE_THRESH_MEAN_C: In this, the threshold value is the mean of the neighborhood area.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 77978648 โ€บ shape-detection-with-adaptive-thresholding-in-python-opencv
Shape Detection with Adaptive Thresholding in Python/OpenCV - Stack Overflow
As fmw42 mentioned, the best answers are the simplest. cv2.adaptiveThreshold is not an easy function to use and in this case, the obvious approach should be to go with colour channels instead of dynamic thresholds
๐ŸŒ
OpenCV
opencv.org โ€บ home โ€บ news โ€บ image thresholding using opencv
Image Thresholding using OpenCV
April 24, 2025 - The initial threshold value (0 here) is ignored and OpenCV calculates the best threshold internally and returns it as the first output (_ in this case). import cv2 # Load image in grayscale img = cv2.imread("intensity_digits.png", cv2.IMREAD_GRAYSCALE) # Adaptive Mean Thresholding thresh_mean = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) # Adaptive Gaussian Thresholding thresh_gauss = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) # Otsu's Thresholding _, thresh_otsu = cv2.threshold(img, 0, 255, cv2.THRESH_BIN
๐ŸŒ
YouTube
youtube.com โ€บ watch
OpenCV - Adaptive Thresholding - YouTube
This video shows you how to use OpenCV's Adaptive Threshold function.Adaptive Thresholding Paper Link: https://people.scs.carleton.ca/~roth/iit-publications-
Published: April 4, 2020
๐ŸŒ
Medium
medium.com โ€บ @madhavarajumaddimani โ€บ exploring-thresholding-techniques-in-opencv-7e92dcb70502
Exploring Thresholding Techniques in OpenCV | by Madhavaraju | Medium
October 27, 2025 - Adaptive thresholding addresses the limitations of global thresholding by calculating the threshold for each pixel based on the local neighborhood. This method is particularly useful for images with varying lighting conditions.
Top answer
1 of 1
5

The main issue is that the result of adaptiveThreshold has gaps in the external edge, so you can't use it as input to findContours.

I think that using GaussianBlur makes things worst, because it blurs the edge between the hand and the background.

You may use the following stages:

  • Convert frame to Grayscale.
  • Apply adaptiveThreshold with large kernel size (I used size 51).
    Using a large kernel size, keeps a thick edge line without gaps (except from a small gap at the fingernail).
  • Find contours.
    Find the contour with the maximum area.
  • Draw the contour (fill with solid value of 255) on a zeros image.
    There is a problem: the inner part of the hand is not filled due to the weird shape of the contour.
  • For complete the filling:
    Find the center of the contour, and fill it using floodFill.

Here is a Python code sample:

import numpy as np
import cv2

frame = cv2.imread("hand.jpg")  # Read image from file (for testing).

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Use BGR to Gray conversion (not RGBA, because image is read from file)

# Apply adaptiveThreshold with large filter size.
thres_gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 51, 2)

# Find contours (external contours)
cnts, hier = cv2.findContours(thres_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Find contour with the maximum area
c = max(cnts, key=cv2.contourArea)

res = np.zeros_like(gray)  # Create new zeros images for storing the result.

# Fill the contour with white color - draw the filled contour on res image.
cv2.drawContours(res, [c], -1, 255, -1)

# Compute the center of the contour
# https://www.pyimagesearch.com/2016/02/01/opencv-center-of-contour/
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

# Use floodFill for filling the center of the contour
cv2.floodFill(res, None, (cX, cY), 255)

# Show images for testing
cv2.imshow('thres_gray', thres_gray)
cv2.imshow('res', res)
cv2.waitKey()
cv2.destroyAllWindows()

Results:

thres_gray:

res before floodFill:

res after floodFill:


JAVA implementation:

package myproject;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;
import org.opencv.core.Point;
import org.opencv.core.MatOfPoint;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgproc.Moments;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.List;
import java.util.ArrayList;

class Sample {
    

static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }


  
public static void main(String[] args) {
    Mat frame = Imgcodecs.imread("hand.jpg");
    
    Mat gray = new Mat();
    Mat thres_gray = new Mat();
    
    Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
    
    //Apply adaptiveThreshold with large filter size.
    Imgproc.adaptiveThreshold(gray, thres_gray, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 51, 2);    
      
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    
    //Find contours
    Imgproc.findContours(thres_gray, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    //Find contour with the maximum area
    //https://stackoverflow.com/questions/38759925/how-to-find-largest-contour-in-java-opencv
    double maxVal = 0;
    int maxValIdx = 0;
    for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
    {
        double contourArea = Imgproc.contourArea(contours.get(contourIdx));
        if (maxVal < contourArea)
        {
            maxVal = contourArea;
            maxValIdx = contourIdx;
        }
    }
       
    Mat res = Mat.zeros(gray.size(), CvType.CV_8UC1);    //Create new zeros images for storing the result.
    
    Imgproc.drawContours(res, contours, maxValIdx, new Scalar(255), -1);
    
    //Compute the center of the contour
    //https://www.pyimagesearch.com/2016/02/01/opencv-center-of-contour/
    Moments M = Imgproc.moments(contours.get(maxValIdx));
    int cX = (int)(M.get_m10() / M.get_m00());
    int cY = (int)(M.get_m01() / M.get_m00());    

    //Use floodFill for filling the center of the contour.    
    Mat mask = Mat.zeros(res.rows() + 2, res.cols() + 2, CvType.CV_8UC1);
    Imgproc.floodFill(res, mask, new Point(cX, cY), new Scalar(255));
       
    Imgcodecs.imwrite("res.png", res);
}

}
๐ŸŒ
Pseudopencv
pseudopencv.site โ€บ utilities โ€บ adaptthreshold
Adaptive Image Thresholding
The adaptive algorithm to use. Check out the Adaptive Threshold Type for more detail explanation. Type of algorithm use to calculate the threshold. Check out the documentation for further information on each type. Unlike the normal cv2.threshold(), only limited amount of threshold type is supported.