DIP+CV · Programming Exercise

EP01_07 — ⚫ Manual Thresholding: Binary Image

1.18.10 EP01_07 ⚫ Manual Thresholding: Binary Image

In this activity, you must write a program that performs image segmentation through thresholding.

  • Read two integers L and C, representing the dimensions of the matrix.
  • Read an integer T, which will be the threshold (cutoff) value.
  • Read the integer values of the matrix.
  • For each pixel \(p\), apply the following binarization rule:

\[\text{result} = \begin{cases} 255 & \text{if } p > T \\ 0 & \text{if } p \le T \end{cases}\]

  • Print the resulting matrix containing only the values 0 or 255.

📌 Important:

  • Pay attention to the operator: the pixel only becomes white (255) if it is strictly greater than \(T\).
  • The output must preserve the matrix structure (L rows and C columns).
  • See an interactive simulator for this problem at Figure 1.17 (adjust the \(T\) slider to observe how objects are isolated from the background).

1.18.10.1 🧠 What is Segmentation?

Thresholding is the simplest method for separating objects of interest from the image background. By converting grayscale tones into pure black and white, we create a binary map that facilitates object counting or shape identification:

Pixel Value (\(p\)) Condition Final Result
Dark (\(p \le T\)) Background/Noise 0 (Black)
Light (\(p > T\)) Object/Highlight 255 (White)

1.18.10.2 📋 Task (specification for VPL)

Input:

The first line contains the integer L.

The second line contains the integer C.

The third line contains the integer T (threshold).

The following lines contain the elements of the matrix.

Output:

The binarized matrix (0 or 255) with L rows and C columns.

1.18.10.3 📌 Examples

Input Output Observation
2
4
128
0 100 128 200
50 129 255 64
0 0 0 255
0 255 255 0
Note that the value 128 became 0 (since \(128 \le 128\))
🎛️ EP01_07 Simulator: Interactive Image Thresholding Binary: 0 or 255
128
Input (Grayscale)
Output (Binary Mask)
Pixels with intensity greater than 128 (p > 128) become white (255); otherwise, they become black (0).
Figure 1.17: Simulator EP01_07: Interactive Global Thresholding (Binarization p > T)
# your solution
TestSuite("EP01_07.py").run()
✔️ EP01_07.cases already exists in casos/
📋 7 case(s) loaded from casos/EP01_07.cases
💥 File EP01_07.py not found.