DIP+CV · Programming Exercise

EP02_02 — 🔬 Spatial Subsampling

2.17.2 EP02_02 🔬 Spatial Subsampling

In this activity, you must implement the reduction of the spatial resolution of an image through the subsampling process.

  • Read two integers L and C, representing the dimensions of the original matrix.
  • Read an integer value \(f\) (\(f \ge 1\)), which represents the sampling factor.
  • Read the integer values of the original matrix.
  • The new image must be constructed by selecting the pixel at position \((f \cdot i, f \cdot j)\) of the original image.
  • Print the resulting matrix with the new dimensions.
  • See a simulation of this EP in Figure 2.13.

📌 Important:

  • Final Dimensions: The sampled image will have dimensions \(\lceil L/f \rceil \times \lceil C/f \rceil\). In programming terms, this is equivalent to the resulting size of a slice with step \(f\).
  • Implementation: Do not use ready-made functions from image processing libraries (such as OpenCV or PIL) for resizing. Implement the pixel selection logic manually or via matrix slicing.
  • Aliasing: Note that this process may cause the aliasing effect (jagged edges), where fine details are lost or unwanted patterns appear.

2.17.2.1 🧠 Discretization of Space

Subsampling reduces the spatial resolution of an image by selecting only one pixel every \(f\) pixels in each direction. It is the inverse process of interpolation:

Parameter Function Effect
Factor \(f\) Sampling step Defines the selection interval. A factor of \(2\) reduces the width and height by half.
Resolution Pixel density Decreases the total amount of spatial information in the image.
Aliasing Side effect Emergence of staircase patterns or blocks due to the loss of fine details.

2.17.2.2 📋 Task (specification for VPL)

Input:

The first line contains L.

The second line contains C.

The third line contains the factor f.

The following lines contain the elements of the \(L \times C\) matrix.

Output:

The reduced matrix with dimensions corresponding to the slicing by f.

2.17.2.3 📌 Examples

Input Output Observation
2
4
2
10 20 30 40
50 60 70 80
10 30 Factor 2 selects pixels (0,0) and (0,2) from the first row. The second row is ignored.
🔽 Simulator EP02_02: Spatial Image Subsampling p'(i, j) = p(i·f, j·f)

Adjust the subsampling factor (f) to observe the reduction in the spatial dimension of the matrix and the skip-sampling of the top-left pixels of each f × f block.

1
f = 1 → Original Resolution (4×4)  |  f = 2 → Half (2×2)  |  f = 3 or 4 → Single Sample (1×1)
Original (4×4)
Subsampled (Variable Size)
Factor f = 1 → keeps all original pixels (4×4)
Figure 2.13: EP02_02 Simulator: Spatial Subsampling (Resolution Reduction by f-Step)
%%writefile EP02_02.cpp
// your solution
Overwriting EP02_02.cpp
TestSuite("EP02_02.cpp").run()
✔️ EP02_02.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_02.cases

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