DIP+CV · Programming Exercise

EP02_09 — 🧩 Generic Affine Transformation

2.17.9 EP02_09 🧩 Generic Affine Transformation

In this activity, you must implement an arbitrary affine transformation on an image. This operation is the generalization of all linear transformations (scaling, rotation, shearing) combined with translation, allowing complex geometric manipulations through a single matrix.

  • Read two integers L and C, representing the dimensions of the matrix.
  • Read six real values (\(a, b, t_x, c, d, t_y\)) that compose the \(2 \times 3\) affine transformation matrix.
  • Read a string representing the interpolation method (nearest or bilinear).
  • Read the integer values of the original matrix.
  • Apply the transformation while maintaining the original size \(L \times C\).
  • Print the resulting matrix.
  • See Figure 2.20 for a simulation of this EP.

📌 Important:

  • Inverse Mapping: To compute the value of each pixel in the destination image, you must use the inverse of the provided affine transformation matrix to find the corresponding coordinate in the original image.
  • Filling: Computed coordinates that fall outside the bounds \([0, L-1]\) and \([0, C-1]\) of the original image must result in a pixel with value 0.
  • Flexibility: This implementation must be able to perform any of the previous tasks (translation, rotation, etc.) simply by changing the matrix parameters.

Hint:

flags = cv2.INTER_NEAREST if interp == 'nearest' else \
        cv2.INTER_CUBIC   if interp == 'bicubic'  else \
        cv2.INTER_LANCZOS4 if interp == 'lanczos' else \
        cv2.INTER_LINEAR

r = cv2.warpAffine(img, M, (C, L), flags=flags)

2.17.9.1 🧠 Combining Operations

The affine transformation preserves points, lines, and planes. In image processing, it maps the position \((x, y)\) to \((x', y')\) following the system:

\[\begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix}\]

Or, compactly in homogeneous coordinates:

\[\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}\]

2.17.9.2 📋 Task (specification for VPL)

Input:

The first line contains L.

The second line contains C.

The third line contains six floats: a b tx c d ty.

The fourth line contains the method interp (nearest or bilinear).

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

Output:

The transformed matrix with the original dimensions \(L \times C\).

2.17.9.3 📌 Examples

Input Output Observation
2
2
1.0 0.0 0.5 0.0 1.0 0.5
bilinear
10 20
30 40
15 20
25 30
Fractional translation \((t_x=0.5, t_y=0.5)\): each output pixel \((i,j)\) samples the position \((i+0.5,\, j+0.5)\) from the input via bilinear interpolation. E.g., \((0,0)\) interpolates the four neighbors \(→15\).
3
3
2.0 0.0 0.0 0.0 2.0 0.0
nearest
1 2 3
4 5 6
7 8 9
1 1 2
1 1 2
4 4 5
\(2\times\) scaling via the affine matrix \((a=2, d=2)\): each output pixel \((i,j)\) samples the position \((2i, 2j)\) from the input with nearest. E.g., \((0,2)→(0,4)\) outside the image \(→\) nearest clips to \((0,2)=3\)… awaiting confirmation of the border logic.
📐 Simulator EP02_09: 2D Affine Transformation [x'] = [a b tx]·[x y 1]ᵀ

Adjust the parameters of the 2×3 affine matrix (rotation, scale, shear, and translation) and observe the effect applied to the reference figure.

2×3 affine matrix
a b tx
c d ty

● Orange arrow (triangular tip) + black rectangular body. The affine transformation is applied to the entire figure.

Matrix = [[1.0, 0.0, 0], [0.0, 1.0, 0]] → identity transformation.
Figure 2.20: Simulator EP02_09: Affine Transformation 2D (2×3 Matrix)
%%writefile EP02_09.cpp
// your solution
Overwriting EP02_09.cpp
TestSuite("EP02_09.cpp").run()
✔️ EP02_09.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_09.cases

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