Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 54 ยท Image Processing

Thresholding

Learn OpenCV thresholding: global binary modes, Otsu's automatic threshold and adaptive mean or Gaussian methods, with Otsu rebuilt in numpy that you can run.

Thresholding is the simplest decision a vision program can make: for every pixel, is it bright enough to count or not? The result is a binary image where each pixel is either 0 or a fixed maximum value, usually 255. Binary images are the raw material for contours, morphology, counting objects, reading scanned text and many other later steps.

This lesson covers the global modes, Otsu's method for picking the threshold automatically, and adaptive thresholding for uneven lighting. A note on honesty: cv2 cannot run on this site, so the OpenCV calls are shown in read-only blocks, and every live example uses numpy to rebuild the same idea. The numpy versions follow OpenCV's documented formulas, but they are stand-ins, not the library. If images as arrays are new to you, read Images Are NumPy Arrays first.

The global threshold

cv2.threshold takes a grayscale image, a threshold value and a maximum value, and returns two things: the threshold that was actually used and the new image.

Python
import cv2

gray = cv2.imread("page.png", cv2.IMREAD_GRAYSCALE)
used, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

With THRESH_BINARY, a pixel becomes 255 when its value is strictly greater than the threshold, and 0 otherwise. A pixel equal to 127 therefore turns black. There are five related modes, and they differ only in what they do on each side of the threshold.

Five global modes

BINARY Above the threshold becomes the max value, the rest 0.
BINARY_INV The same test, but the output is flipped.
TRUNC Values above the threshold are capped at it.

These three change values on both sides of the threshold.

1 / 2

The next example reproduces all five on one row of nine pixels so you can compare them side by side.

Python The five global modes in numpy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the pixel with value exactly 127 lands on the "not greater" side in every mode. That boundary rule matters when you compare your own code with OpenCV output.

Choosing a threshold

A fixed 127 is a guess. It works when the picture is evenly lit and the object is clearly lighter or darker than the background, and it fails otherwise. Plot the pixel values of a good candidate image and you usually see two humps, one for the background and one for the objects. The best threshold sits in the valley between them. The histograms lesson shows how to see that picture. Rather than eyeballing a valley, you can ask an algorithm to find it.

Otsu's method

Otsu's method tries every possible threshold from 0 to 255, splits the pixels into a dark class and a bright class, and keeps the threshold where the two classes are best separated. "Separated" is measured by the between-class variance: how far apart the two class means are, weighted by how many pixels each class holds. In OpenCV you add a flag, and the threshold argument you pass is ignored:

Python
used, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
print("Otsu chose", used)

That returned used value is why cv2.threshold returns a tuple. The numpy version below builds the histogram, computes the cumulative class weights and means, and takes the threshold with the largest between-class variance. The image is synthetic: a dark background with a brighter rectangle, plus Gaussian noise from a seeded generator so every run is identical.

Python Otsu's threshold, written in numpy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On this image Otsu picks 98 and gets every pixel right. The fixed 127 happens to sit inside the valley and misses one pixel, while using the image mean (81) gets 61 pixels wrong because the mean is dragged toward the dark majority. Edit the two brightness values in the script and watch the fixed threshold fail while Otsu follows the data. Otsu works best when the histogram really has two humps. With one object that covers a tiny part of the picture, or with three brightness groups, its single threshold can land in the wrong place.

The numpy version takes the first maximum, as OpenCV's C++ loop does when several thresholds tie. Treat the match as the documented idea rather than a bit-for-bit guarantee.

Adaptive thresholding

Global methods use one number for the whole picture. A photo of a page with a shadow across one side has no single good number: the shadowed paper is darker than the ink in the bright corner. Adaptive thresholding computes a separate threshold for every pixel from its local neighborhood.

Python
binary = cv2.adaptiveThreshold(
    gray, 255,
    cv2.ADAPTIVE_THRESH_GAUSSIAN_C,   # or ADAPTIVE_THRESH_MEAN_C
    cv2.THRESH_BINARY,
    11,   # blockSize: odd neighborhood size
    5,    # C: subtracted from the local mean
)

For each pixel, OpenCV computes the neighborhood mean (a plain average for MEAN_C, a Gaussian-weighted average for GAUSSIAN_C), subtracts the constant C, and then applies the binary test. A positive C makes the test stricter, so flat regions turn black instead of noisy white. blockSize must be odd and greater than 1. A block that is too small chases noise; one that is too large behaves like a global threshold.

The example paints vertical ink strokes on paper whose brightness fades from left to right, then compares a global Otsu threshold against a mean-based adaptive one that uses an integral image for the local averages.

Python Adaptive mean versus global Otsu

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The global threshold (132 here) gets 336 of 960 pixels wrong: the right side of the page falls below it and turns into one black slab, so the strokes vanish there. The adaptive version gets none wrong. It keeps the strokes everywhere, because each pixel is compared with its own surroundings. The small price is that the interior of a very wide dark shape can be misread, since its neighborhood is uniformly dark. Adaptive thresholding suits thin features such as text and line drawings.

Practical tips

  • Convert to grayscale first (see Color Spaces). Running threshold on a color image works on each channel separately, which is rarely what you want, and Otsu needs a single 8-bit channel.
  • Blur lightly before thresholding noisy images. A Gaussian blur before Otsu often cleans the histogram and gives a steadier threshold.
  • Use THRESH_BINARY_INV when the objects you care about are dark, so that they come out white. Later steps such as contours treat white as the foreground.
  • After thresholding, clean small specks and holes with morphological operations.
  • Both Otsu and the Triangle method (THRESH_TRIANGLE) only work on single-channel 8-bit images.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. With cv2.THRESH_BINARY and a threshold of 100, what happens to a pixel whose value is exactly 100?

    THRESH_BINARY sets the output to the max value only when the pixel is strictly greater than the threshold. Equal values go to 0.
  2. 2. When you use THRESH_OTSU, what happens to the threshold number you pass to cv2.threshold?

    Otsu computes its own threshold from the histogram. The value you pass is ignored, and the one actually used is the first item of the returned tuple.
  3. 3. A scan has a shadow across one corner. Which approach is the most reliable?

    A single global number cannot suit both the shadowed and the bright regions. Adaptive thresholding compares each pixel with its own neighborhood, so lighting changes matter much less.
  4. 4. What constraint applies to the blockSize argument of cv2.adaptiveThreshold?

    The neighborhood needs a center pixel, so the size must be odd, and it must be greater than 1.

Summary

  • Thresholding turns a grayscale image into a binary one by comparing every pixel with a number; "greater than" is strict.
  • cv2.threshold returns (used_threshold, image) and offers BINARY, BINARY_INV, TRUNC, TOZERO and TOZERO_INV.
  • Otsu's method picks the threshold that maximizes between-class variance and suits histograms with two humps.
  • Adaptive thresholding (mean or Gaussian) uses a per-pixel local threshold for uneven lighting; blockSize is odd and C is subtracted from the local mean.
  • Blur first, choose BINARY or BINARY_INV so objects come out white, and clean the result with morphology.

Next up: Histograms and Equalization.