👆 Left click darkens (−25) and right click lightens (+25) the input pixels. Watch the optimal threshold T* adjust dynamically to the histogram.
T* = −
Manually choosing the threshold \(T\) works when illumination is stable, but in digital microscopy and blood smear inspection, each sample has different contrast — a fixed threshold would fail from image to image. Otsu’s method solves this by autonomously finding the threshold that maximizes the statistical separation between the two pixel classes, making segmentation automatic and adaptive. See Figure 4.31 for a simulation of this EP.
Dimensions: Read the integers \(L\) (rows) and \(C\) (columns).
Data: Read the integer values of the original matrix row by row.
Histogram: Build the histogram \(h[i]\), \(i=0,\dots,255\), counting how many pixels have value \(i\).
Threshold search: For each candidate \(T\) from \(1\) to \(255\), compute the between-class variance: \[ \sigma_B^2(T) = \frac{n_0 \cdot n_1}{N^2}\,(m_0 - m_1)^2 \] where \(n_0,n_1\) are the numbers of pixels with values \(<T\) and \(\geq T\), \(m_0,m_1\) are their means, and \(N=L\times C\).
Selection: The optimal threshold \(T^*\) is the one that maximizes \(\sigma_B^2(T)\) (in case of a tie, keep the first one found).
Application: Binarize the image using T*, applying: \[ p' = \begin{cases} 255, & \text{if } p > T^* \\ 0, & \text{if } p \le T^* \end{cases} \]
cv2.THRESL_BINARY; pixels with value exactly equal to \(T^*\) become black.| Concept | Meaning | Impact |
|---|---|---|
| High \(\sigma_B^2(T)\) | Classes well separated at \(T\) | \(T\) is a good threshold candidate |
| Bimodal histogram | Two distinct “peaks” | Otsu finds the valley between them |
| Unimodal histogram | A single “peak” | Otsu still chooses some \(T\), but the segmentation is unreliable |
Input:
Output:
| Input | Output | Observation |
|---|---|---|
| 4 4 12 12 12 200 12 12 200 200 12 200 200 200 200 200 200 200 |
0 0 0 255 0 0 255 255 0 255 255 255 255 255 255 255 |
Clear bimodal histogram: 12 and 200 |
| 1 2 10 250 |
0 250 | Only two values: \(T^*\) falls on the largest one |
👆 Left click darkens (−25) and right click lightens (+25) the input pixels. Watch the optimal threshold T* adjust dynamically to the histogram.
T* = −
%%writefile EP04_02.cpp
// your solutionOverwriting EP04_02.cpp
TestSuite("EP04_02.cpp").run()✔️ EP04_02.cases already exists in casos/
📋 5 case(s) loaded from casos/EP04_02.cases
🔍 Testing C++: EP04_02.cpp
⚠️ EP04_02.cpp: Empty file (fewer than 3 lines). Tests skipped.