Sub topic 13
Adaptive Thresholding for Uneven Lighting
Prerequisites and the problem
This chapter assumes basic Python, NumPy array indexing, grayscale images, and an OpenCV installation that can read images. You should also know that an image is an array of pixel intensities. In an 8-bit grayscale image, each pixel usually ranges from 0 for black to 255 for white.
Thresholding creates a binary image. Pixels become either foreground, usually 255, or background, usually 0. A global threshold uses one value for every pixel. That works when lighting is fairly even. It fails when one side of a document is darker than the other, because a threshold suitable for the bright side may remove details from the dark side.
Adaptive thresholding calculates a different threshold for each pixel from a local neighborhood.
| Method | Threshold source | Useful when |
|---|---|---|
| Adaptive Gaussian | Weighted local neighborhood | Lighting changes across the image |
How adaptive thresholding works
For each pixel, OpenCV examines a square neighborhood around it. The neighborhood size is controlled by blockSize. With cv2.ADAPTIVE_THRESH_GAUSSIAN_C, nearby pixels receive more weight than distant pixels.
OpenCV then subtracts the constant C:
T(x,y) = \text{local weighted mean}(x,y) - CThe output depends on the threshold type:
cv2.THRESH_BINARYmakes pixels above the local threshold white.cv2.THRESH_BINARY_INVmakes pixels below the local threshold white.
blockSize must be an odd integer greater than 1, such as 11, 21, or 31. A small block follows fine lighting changes but can also follow noise. A large block is more stable but may fail when illumination changes quickly. The constant C shifts the local threshold. Increasing it usually makes more pixels white with THRESH_BINARY_INV.
import cv2
gray = cv2.imread("document.jpg", cv2.IMREAD_GRAYSCALE)
if gray is None:
raise FileNotFoundError("document.jpg could not be read")
adaptive = cv2.adaptiveThreshold(
gray,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
31,
10,
)
cv2.imwrite("document_thresholded.png", adaptive)The input must be a single-channel 8-bit image. If the result has the opposite foreground/background polarity from what you need, change THRESH_BINARY to THRESH_BINARY_INV.
Choosing parameters
Start with a neighborhood several times wider than the smallest object detail that must remain visible. Then inspect the result rather than relying on one setting.
- Increase
blockSizewhen the mask contains small, scattered lighting artifacts. - Decrease
blockSizewhen the illumination changes over short distances. - Increase
Cwhen faint unwanted pixels become foreground. - Decrease
Cwhen thin, dim foreground features disappear. - Use an odd
blockSize; even values are rejected by OpenCV.
Adaptive thresholding is especially useful for scanned pages, receipts, and signs photographed under uneven light. It is less suitable when foreground and background have similar local intensity or when the image contains strong repeating texture.
Exercise
A document has dark text on a page whose left side is shaded. Which parameter should you try changing first if the thresholded result contains many small noisy regions: decrease blockSize or increase it?
Show answer
Increase blockSize. A larger neighborhood averages over more of the shading and usually makes the local threshold less sensitive to tiny intensity changes.