DIP+CV · Programming Exercise

EP05_01 — 🟢 Ideal Low-Pass Filter by Distance in the Spectrum

5.13.1 EP05_01 🟢 Ideal Low-Pass Filter by Distance in the Spectrum

In an old document scanner, the sensor captures crumpled paper and fiber texture along with the text — high-frequency noise that “pollutes” the spectrum at the edges. The maintenance technician has no access to the original image, only to the magnitude spectrum already computed by the scanner’s software. Their job is simple and surgical: keep only the central circle of low frequencies (the global structure of the document) and erase everything outside the radius \(D_0\), eliminating the fine texture without even needing to touch the spatial image.

This is the Ideal Low-Pass Filter (LPFI): the most direct spectral operation in the chapter, but also the one that best reveals the anatomy of a centered spectrum.

5.13.1.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows) and \(C\) (columns) from the magnitude spectrum — already provided centered (equivalent to the output of np.fft.fftshift).
  2. Cutoff frequency: Read the integer \(D_0\).
  3. Data: Read the integer values of the magnitude matrix, row by row.
  4. Spectrum center: Compute \((c_y, c_x) = (L \mathbin{//} 2,\; C \mathbin{//} 2)\).
  5. Distance: For each position \((u,v)\), compute \[ D(u,v) = \sqrt{(u-c_y)^2 + (v-c_x)^2} \]
  6. Ideal mask: Apply \[ H(u,v) = \begin{cases} 1, & D(u,v) \le D_0 \\ 0, & D(u,v) > D_0 \end{cases} \]
  7. Filtering: The output value is \(\text{mag}'(u,v) = \text{mag}(u,v) \cdot H(u,v)\).
  8. Output: Display the filtered matrix with dimensions \(L \times C\).

5.13.1.2 📌 Computational Constraints

  • Non-strict comparison: the criterion uses \(D(u,v) \le D_0\) (the boundary belongs to the filter, i.e., it is kept).
  • Type: all input and output values are integers; the distance is computed in floating point only internally.
  • No magnitude rounding: since the input is already integer and the mask is binary (0 or 1), the output never requires rounding.

5.13.1.3 🧠 Theoretical Background

Region Distance to center Filter effect
Center (\(D \le D_0\)) Low frequencies Preserved — global structure maintained
Edges (\(D > D_0\)) High frequencies Zeroed — texture and noise removed
Small \(D_0\) — Reconstructed image would be very blurry
Large \(D_0\) — Little filtering; almost all energy preserved

5.13.1.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Integer \(D_0\).
  • Following lines: Integer elements of the magnitude matrix (centered).

Output:

  • Filtered matrix with \(L\) rows and \(C\) columns, separated by spaces.

5.13.1.5 📌 Examples

Input Output Observation
3
3
1
10 20 30
40 50 60
70 80 90
0 20 0
40 50 60
0 80 0
Center \((1,1)\). Corners have \(D=\sqrt{2}\approx1.41 > 1\), hence they are zeroed; orthogonal neighbors have \(D=1 \le 1\) and are kept.
1
3
0
5 9 7
0 9 0 \(L=1, C=3\): center at \((0,1)\). Only the central position itself (\(D=0\)) survives \(D_0=0\).
🎮 Simulator EP05_01: Ideal Low-Pass Filter H = (D ≤ D₀) ? 1 : 0
Adjust D₀ and observe which positions of the 5×5 spectrum survive the filter.
Original Spectrum (Magnitude)
Filtered Result
–
Figure 5.32: EP05_01 Simulator: Ideal Low-Pass Filter in the Spectrum
%%writefile EP05_01.cpp
// your solution
Overwriting EP05_01.cpp
TestSuite("EP05_01.cpp").run()
✔️ EP05_01.cases already exists in casos/
📋 5 case(s) loaded from casos/EP05_01.cases

🔍 Testing C++: EP05_01.cpp
⚠️ EP05_01.cpp: Empty file (fewer than 3 lines). Tests skipped.