DIP+CV · Programming Exercise

EP03_06 — 🌫️ N×N Mean Filter Kernel

3.12.6 EP03_06 🌫️ N×N Mean Filter Kernel

In autonomous vehicle cameras, images captured under rain or fog exhibit Gaussian noise. The mean filter is widely used for real-time noise reduction, being implemented directly in the ISP (Image Signal Processor) of CMOS (Complementary Metal-Oxide-Semiconductor) sensors.

CMOS sensors are the image sensors used in most modern cameras (smartphones, webcams, automotive cameras, etc.). They convert light into electrical signals, and the ISP processes these signals in real time — applying operations such as noise reduction, white balance, and other image adjustments.

See the simulation of this EP in Figure 3.31.

3.12.6.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows), \(C\) (columns), and \(N\) (kernel size, always odd).
  2. Data: Read the pixel matrix \(f\).
  3. Mean Filter: For each internal pixel \((i,j)\) (without borders), compute:

\[g(i,j) = \text{round}\left(\frac{1}{N^2} \sum_{s=-(r)}^{r} \sum_{t=-(r)}^{r} f(i+s,\, j+t)\right), \quad r = \lfloor N/2 \rfloor\]

  1. Border Handling: Pixels at the border (where the \(N \times N\) window exceeds the limits) must be copied directly from the original without modification.
  2. Output: Display the resulting \(L \times C\) matrix.

3.12.6.2 📌 Computational Constraints

  • Radius: \(r = \lfloor N/2 \rfloor\) (half of the kernel, integer).
  • Internal pixels: \((i,j)\) with \(r \le i < L-r\) and \(r \le j < C-r\).
  • Rounding: Use mathematical rounding before converting to integer.
  • No clipping: The average of values \(\in [0,255]\) remains in \([0,255]\).

3.12.6.3 🧠 Theoretical Background

Size \(N\) Coefficient Pixels in the window Effect
3 \(1/9 \approx 0.111\) 9 Smooth
5 \(1/25 = 0.04\) 25 Medium
7 \(1/49 \approx 0.020\) 49 Strong

3.12.6.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Integer \(N\) (odd, \(N \ge 3\)).
  • Following lines: Elements of the original matrix.

Output:

  • Filtered \(L \times C\) matrix.

3.12.6.5 📌 Examples

Input Output Observation
3
3
3
10 20 30
40 50 60
70 80 90
10 20 30
40 50 60
70 80 90
Only border (3×3 = all border)
5
5
3
0 0 0 0 0
0 0 0 0 0
0 0 100 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 11 11 11 0
0 11 11 11 0
0 11 11 11 0
0 0 0 0 0
Isolated pixel: all 9 internal pixels whose 3×3 window includes the value 100 receive round(100/9)=11
🔲 Simulator EP03_06: Mean Filter with N×N Kernel g = Mean(Neighbors)

Select the kernel size and hover over the result pixels to inspect the neighborhood and the arithmetic mean calculation.

Kernel size:
Original Image f (7×7) With salt-and-pepper noise
Result g (Smoothed Filter) Hover to inspect
Legend:
Kernel Window
Border (Copied)
Inspected Pixel
Hover over an inner pixel of the result to see the mean calculation.
Figure 3.31: Simulator EP03_06: Average Filter with N×N Kernel
%%writefile EP03_06.cpp
// your solution
Overwriting EP03_06.cpp
TestSuite("EP03_06.cpp").run()
✔️ EP03_06.cases already exists in casos/
📋 5 case(s) loaded from casos/EP03_06.cases

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