DIP+CV · Programming Exercise

EP02_06 — 🔄 Image Rotation

2.17.6 EP02_06 🔄 Image Rotation

In this activity, you must implement the rotation of an image around its geometric center. This operation requires coordinate mapping and the use of interpolation techniques to determine the new pixel values.

  • Read two integers L and C, representing the dimensions of the matrix.
  • Read a real value \(\theta\) (angle in degrees) and a string representing the interpolation method (nearest or bilinear).
  • Read the integer values of the original matrix.
  • Perform the rotation around the image center \((L/2, C/2)\).
  • Print the resulting matrix with the same dimensions as the original.
  • See Figure 2.17 for a simulation of this EP.

📌 Important:

  • Inverse Mapping: To avoid “holes” in the final image, iterate over each pixel \((x', y')\) of the destination image and compute its corresponding position \((x, y)\) in the original image using the inverse rotation matrix.

  • Interpolation:

  • nearest: Assigns the value of the pixel closest to the computed coordinate.

  • bilinear: Computes a weighted average based on the 4 nearest neighbors.

  • Borders: Pixels whose origin \((x, y)\) falls outside the bounds of the original image must be filled with 0.

2.17.6.1 🧠 Angle Transformation

The rotation of a point \((x, y)\) relative to the origin by an angle \(\theta\) is given by the transformation matrix. To rotate around a center \((x_c, y_c)\), we first translate the center to the origin, rotate, and translate back:

\[\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} \cos\theta & -\sin\theta & x_c \\ \sin\theta & \cos\theta & y_c \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x-x_c \\ y-y_c \\ 1 \end{bmatrix}\]

Tip: Use inverse mapping to ensure that all pixels of the output image are correctly filled.

2.17.6.2 📋 Task (specification for VPL)

Input:

The first line contains L.

The second line contains C.

The third line contains the angle theta (in degrees) and the method interp (nearest or bilinear).

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

Output:

The rotated matrix with L rows and C columns.

2.17.6.3 📌 Examples

Input Output Observation
2
2
90 nearest
1 2
3 4
3 1
4 2
90° clockwise rotation: column 0 becomes row 0 (from bottom to top). \((0,0)=1→(1,0)\), \((1,0)=3→(0,0)\), \((0,1)=2→(1,1)\), \((1,1)=4→(0,1)\).
3
3
45 bilinear
0 0 0
0 255 0
0 0 0
0 180 0
180 255 180
0 180 0
45° rotation: the central pixel remains \(255\); the direct neighbors receive an interpolated value \(\approx 180\) via bilinear; the corners remain \(0\).
🔄 Simulator EP02_06: 2D Geometric Rotation x' = x·cosθ − y·sinθ | y' = x·sinθ + y·cosθ

Adjust the rotation angle (θ) via slider or quick shortcuts to observe the trigonometric transformation of coordinates around the image center.

0°

● Green square with orange marker (top-right corner) – rotation around the center.

θ = 0° → cos = 1.000, sin = 0.000 → Identity Matrix
Figure 2.17: EP02_06 Simulator: Image Rotation Around Origin by Angle θ
%%writefile EP02_06.cpp
// your solution
Overwriting EP02_06.cpp
TestSuite("EP02_06.cpp").run()
✔️ EP02_06.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_06.cases

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