Binarize an Image
Binarize any image to black and white using global threshold, Otsu's method, adaptive mean, or Floyd-Steinberg dithering. Download the result as PNG.
About
Image binarization converts a continuous-tone photograph into a strict two-level (black and white) representation. The process hinges on mapping each pixel's luminance L to either 0 or 255 based on a decision boundary T. Getting that boundary wrong destroys information: set T too low and fine detail drowns in white; set it too high and shadow regions collapse into solid black. This tool implements four production-grade methods. Global thresholding applies a single user-defined T. Otsu's method computes the statistically optimal T by maximizing inter-class variance across the full histogram. Adaptive mean thresholding calculates a local T per pixel using a sliding window, critical for unevenly lit documents. Floyd-Steinberg dithering diffuses quantization error to neighboring pixels, preserving perceived tonal gradation without halftone screens.
Luminance is derived via the ITU-R BT.601 standard: L = 0.299R + 0.587G + 0.114B. This weighting reflects human photopic sensitivity, not a naive average. The tool approximates ideal results under uniform illumination. For documents with strong lighting gradients (e.g., phone-captured receipts), adaptive thresholding will outperform global methods. Note: processing occurs entirely in-browser via the Canvas API. No image data leaves your device.
Formulas
The luminance of each pixel is computed using ITU-R BT.601 luma coefficients:
For global thresholding, the output pixel P′ is:
Otsu's method selects T by maximizing inter-class variance σB2:
where ω0, ω1 are the class probabilities (fraction of pixels below/above T) and μ0, μ1 are the class means.
For adaptive mean thresholding, the local threshold at pixel (x, y) is:
where N is the W × W neighborhood and C is a constant offset (typically 2 - 15). The sum is computed in O(1) per pixel using an integral image (summed-area table).
Floyd-Steinberg dithering distributes quantization error e = L − P′ to four neighbors:
where R is the red channel, G green, B blue, L is computed luminance, T is the threshold value, P′ is the binarized output pixel, e is quantization error, W is the adaptive window size (odd integer), and C is the adaptive offset constant.
Reference Data
| Method | Type | Best For | Parameters | Time Complexity | Edge Handling |
|---|---|---|---|---|---|
| Global Threshold | Fixed | High-contrast images, logos | T ∈ [0, 255] | O(n) | None |
| Otsu's Method | Automatic | Bimodal histograms, scanned docs | None (auto) | O(n + 256) | None |
| Adaptive Mean | Local | Uneven lighting, photos of text | Window W, Offset C | O(n) | Mirror padding |
| Floyd-Steinberg | Dithering | Photographs, gradients, art | T ∈ [0, 255] | O(n) | Boundary clamp |
| Niblack | Local | Degraded documents | W, k | O(n) | Mirror padding |
| Sauvola | Local | Stained/aged paper | W, k, R | O(n) | Mirror padding |
| Bernsen | Local | Variable contrast regions | W | O(n ⋅ W2) | Zero padding |
| Mean (Simple) | Fixed | Quick preview | None (auto) | O(n) | None |
| Median | Fixed | Symmetric histograms | None (auto) | O(n) | None |
| Triangle | Automatic | Unimodal histograms | None (auto) | O(n + 256) | None |
| Ordered Dither (Bayer) | Dithering | Retro/pixel art | Matrix size | O(n) | Tile wrap |
| Atkinson | Dithering | Classic Mac aesthetics | T | O(n) | Boundary clamp |
| Stucki | Dithering | Smoother gradients | T | O(n) | Boundary clamp |