AI Notes / Image Binarization
Common color format: RGB.
To reduce the amount of data and speed up processing when doing machine learning and similar tasks, we need to binarize image data.
Feature extraction is an irreversible, staged process, so we should keep as much of the genuinely useful information as possible during extraction.
How binarization works
Manual thresholding
A binarized image has only two colors: black and white. So we can set a threshold: if a pixel is greater than the threshold, treat it as black; otherwise, treat it as white.
But this method has drawbacks: for an image that is bright overall, the RGB values of its pixels may all fall below the threshold, producing a large area of pure white; the same applies to an image that is dark overall.
Improving manual thresholding: the mean method
To fix the drawbacks of manual thresholding, we can take the average of the RGB values across the whole image as the threshold.
But this method also has drawbacks, for example, when part of the image is too dark or too bright.
Adaptive thresholding
Adaptive thresholding works like this: split a large image into multiple small regions, then compute the mean or median for each region and use it as the threshold for binarization.
The downside is that if the split cuts right through an object, the object may end up with uneven colors.
Otsu's method
Otsu's method is a more complex way to choose a binarization threshold.
First, compute its grayscale histogram. The histogram is usually bimodal, with some separation between the two peaks.
Normalization
Next, normalize the colors, scaling values of 255 or higher down to the range [0,1].
Formula:
x' = \frac{x - min(x)}{max(x) - min(x)}
where x is the original data, max(x) and min(x) are the maximum and minimum values across all data respectively, and x' is the normalized data.
Between-class variance
Next, for every possible threshold, compute the between-class variance.
Between-class variance is the variance between the means of the two classes after binarization (the pixels above and below the threshold).
Given a grayscale image, denoted h(r_k), k=0,1,2,...,L-1, where r_k is the pixel value and L-1 is the gray level (for an 8-bit image, L=256). Let the threshold be t; the class of pixels with values below the threshold is C_0, and the class with values above the threshold is C_1.
First compute the probabilities of the two classes, p_0(t) and p_1(t):
p_0(t) = \sum\limits^{t-1}_{i=0}h(r_i)
p_1(t) = \sum\limits^{L-1}_{i=t}h(r_i)
Then compute the mean gray levels of the two classes, \mu_0(t) and \mu_1(t):
\mu_0(t) = \sum\limits^{t-1}_{i=0}\frac{i\cdot h(r_i)}{p_0(t)}
\mu_1(t) = \sum\limits^{L-1}_{i=t}\frac{i\cdot h(r_i)}{p_1(t)}
Compute the overall mean gray level \mu_{T}:
\mu_{T} = \sum\limits^{L-1}_{i=0}i \cdot h(r_i)
The between-class variance \sigma^{2}_{B}(t) is defined as:
\sigma^{2}{B}(t) = p_0(t) \cdot (\mu_0(t) - \mu{T})^{2} + p_1(t) \cdot (\mu_1(t) - \mu_{T})^{2}
Otsu's goal is to find a threshold t that maximizes the between-class variance \sigma^{2}_{B}(t) (which means the clearest separation between black and white).
Drawback: the computation is too heavy for high-resolution images.
Code implementation
Using Python.
import numpy as np
best_threshold = t
After the loop finishes, we have the best threshold.
Image example
Original image:

After processing:

