DIP+CV · Programming Exercise

EP04_01 — 🎚️ Global Thresholding with a Fixed Threshold

4.9.1 EP04_01 🎚️ Global Thresholding with a Fixed Threshold

In document scanners and barcode reading systems, the first processing step is always to separate what is “object” (ink, text, bars) from what is “background” (paper, packaging). Global thresholding does exactly this: it compares each pixel to a single threshold \(T\) and decides, in real time, whether it belongs to the light class or the dark class. It is the simplest segmentation operator—and yet, it underlies a large portion of industrial visual inspection pipelines. See Figure 4.30 for a simulation of this EP.

4.9.1.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows) and \(C\) (columns).
  2. Threshold: Read the integer \(T\) (decision threshold).
  3. Data: Read the integer values of the original matrix row by row.
  4. Mapping: For each pixel \(p\), compute the new value using the equation:

\[ p' = \begin{cases} 255, & \text{if } p > T \\ 0, & \text{if } p \le T \end{cases} \] 5. Output: Display the binarized matrix with dimensions \(L \times C\).

4.9.1.2 📌 Computational Constraints

  • Binarization: The output contains only the values \(0\) or \(255\).
  • Strict comparison: The criterion uses \(> T\) (pixels equal to \(T\) become background).
  • Type: The final result must be an integer.
  • Note: This EP follows the OpenCV convention (cv2.THRESL_BINARY): only pixels with value greater than \(T\) become white (255); pixels with value equal to \(T\) remain black (0).

4.9.1.3 🧠 Theoretical Foundation

Parameter Type Visual Impact
\(T\) small Integer Most pixels become white
\(T\) large Integer Most pixels become black
\(T\) well-chosen Integer Clearly separates object and background

4.9.1.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Integer \(T\).
  • Following lines: Integer elements of the original matrix.

Output:

  • Binarized matrix with \(L\) rows and \(C\) columns, values \(0\) or \(255\) separated by spaces.

4.9.1.5 📌 Examples

Input Output Observation
2
4
100
0 99 100 180
255 30 120 80
0 0 0 255
255 0 255 0
\(T=100\): only pixels with value greater than 100 become white;
therefore, 99 and 100 become black.
1
3
0
0 50 255
0 255 255 \(T=0\): only pixels with value strictly greater than 0 become white.
🎚️ Simulator EP04_01: Global Thresholding p' = (p > T) ? 255 : 0

👆 Click on a cell in the Original Input to darken the pixel (−30) and right-click to lighten (+30). Adjust the threshold T for binarization.

128
Original Input (Clickable)
Binarized Result (p')
Formula applied: (p > 128) ? 255 : 0
Figure 4.30: EP04_01 Simulator: Global Thresholding by Fixed Threshold (p’ = (p > T) ? 255 : 0)
%%writefile EP04_01.py
# Python code
Overwriting EP04_01.py
TestSuite("EP04_01.py").run()
✔️ EP04_01.cases already exists in casos/
📋 7 case(s) loaded from casos/EP04_01.cases

🔍 Testing Python: EP04_01.py
⚠️ EP04_01.py: Empty file (fewer than 3 lines). Tests skipped.