Computes the transformed coordinates using matrix multiplication
Transformation matrix Assignment Answers
Question:
In python using OpenCV, numpy and matplotlib
𝑇1 = [ 2 0 0 0 1 0 0 0 1 ]
𝑇2 = [ √2 −√2 0 √2 √2 0 0 0 1]
How do I properly get pixel positions from an image that I chose and transform the pixel positions into new pixel positions using the matrices shown?
I am new to python and openCV so any help is greatly appreciated!!
Transformation matrix Answer and Explanation
- T1: Scaling transformation where x' = 2 * x and y' = y (no change in y).
- T2: Rotational transformation by 45 degrees (in radians) and scaling by sqrt(2).
1. Define Image Coordinates: Represent image coordinates as augmented vectors [x, y, 1], where the third element is 1 to account for translation in matrix multiplication.
2. Matrix Multiplication: Multiply each transformation matrix (`T1`, `T2`, `T3`, `T4`) with the image coordinates to get new transformed coordinates.
import numpy as np
import matplotlib.pyplot as plt
T1 = np.array([[2, 0, 0],
[0, 1, 0],
T3 = np.array([[1, 0.2, 0],
[0.2, 1, 0],
# Apply perspective transformations
def apply_transformation(image, T):
coordinates = np.vstack((x.ravel(), y.ravel(), np.ones_like(x.ravel())))
transformed_coordinates[:2, :] /= transformed_coordinates[2, :]
transformed_coordinates[0, :] = np.clip(transformed_coordinates[0, :], 0, width - 1)
transformed_coordinates[1, :] = np.clip(transformed_coordinates[1, :], 0, height - 1)
return transformed_image
transformed_images.append(transformed)
# Display results
axes[0, 0].axis('off')
for i in range(4):
plt.show()
Explanation:
This approach directly computes the transformed image coordinates using matrix operations, avoiding the need for `cv2.perspectiveTransform()` which is more suitable for projective transformations involving homography matrices.