Adjust the horizontal (tx) and vertical (ty) shifts to observe the reverse coordinate mapping and zero-filling (black) for pixels outside the original image boundaries.
2.17.5 EP02_05 ➡️ Image Translation
In this activity, you must implement the spatial displacement of an image. Translation moves each pixel of the original image to a new position based on a displacement vector.
- Read two integers L and C, representing the dimensions of the matrix.
- Read two integers \(t_x\) (horizontal displacement) and \(t_y\) (vertical displacement).
- Read the integer values of the original matrix.
- Calculate the new position \((x', y')\) for each original pixel \((x, y)\).
- Print the resulting matrix with the same dimensions as the original.
- See a simulation of this EP in Figure 2.16.
📌 Important:
- Filling: Pixels that “enter” the image due to displacement and have no corresponding pixel in the original must be filled with 0 (black).
- Discarding: Pixels that, after translation, fall outside the matrix boundaries (\(0 \dots L-1\) or \(0 \dots C-1\)) must be ignored.
- Coordinates: Consider \(x\) as the row index and \(y\) as the column index.
2.17.5.1 🧠 Spatial Displacement
Translating an image means moving all its points by a fixed distance in specified directions. Mathematically, using homogeneous coordinates, the operation is described as:
\[\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}\]
Which results in the simple equations:
- \(x' = x + t_x\)
- \(y' = y + t_y\)
2.17.5.2 📋 Task (specification for VPL)
Input:
The first line contains L.
The second line contains C.
The third line contains the integers tx and ty.
The following lines contain the elements of the \(L \times C\) matrix.
Output:
The resulting matrix with the same dimensions \(L \times C\) after displacement.
2.17.5.3 📌 Examples
| Input | Output | Observation |
|---|---|---|
| 2 2 1 1 10 20 30 40 |
0 0 0 10 |
Displacement (\(t_x=1, t_y=1\)): Each pixel moves one position to the right (horizontal) and one position down (vertical). Pixel \((0,0)=10\) moves to destination \((1,1)\) (bottom-right corner). Empty positions are filled with \(0\). |
| 3 3 -1 0 1 2 3 4 5 6 7 8 9 |
2 3 0 5 6 0 8 9 0 |
Displacement (\(t_x=-1, t_y=0\)): Each pixel moves one position to the left (horizontal). The original first column (1, 4, 7) is discarded, the remaining columns move to the left, and the last resulting column is filled with zeros (\(0\)). |
%%writefile EP02_05.cpp
// your solutionOverwriting EP02_05.cpp
TestSuite("EP02_05.cpp").run()✔️ EP02_05.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_05.cases
🔍 Testing C++: EP02_05.cpp
⚠️ EP02_05.cpp: Empty file (fewer than 3 lines). Tests skipped.