# 5 Best Ways to Perform Matrix Transformation in OpenCV Python

π‘ Problem Formulation: When working with images, you may need to transform them to achieve certain effects or to standardize their dimensions. This could include operations like scaling, rotating, or translating the image within the pixel grid. For example, you might have an image that needs to be rotated 45 degrees and scaled by 150%. OpenCV provides powerful functions to handle these transformations using matrices, and here, weβll explore several methods to do just that using Python.

## Method 1: Scaling

Scaling operations in OpenCV are performed using the `cv2.resize()` function. This method allows altering the size of an image to any desired proportion by specifying a scaling factor or the desired size in pixels. Interpolation options are available to maintain image quality.

Here’s an example:

```import cv2

# Resize the image by specifying the scaling factors
resized_img = cv2.resize(img, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_LINEAR)

# Save or display the output
cv2.imwrite('scaled.jpg', resized_img)```

Output: An image saved as ‘scaled.jpg’, 1.5 times larger than the original ‘input.jpg’.

The code snippet loads an image from the filesystem, applies a scaling transformation with scaling factors for x and y set to 1.5, and uses linear interpolation to avoid aliasing effects. The resulting image is saved back to the filesystem.

## Method 2: Rotation

Rotating images in OpenCV is sleek since it offers the `cv2.getRotationMatrix2D()` to calculate the rotation matrix, after which the `cv2.warpAffine()` function applies the matrix transformation. The process allows rotation by any degree about any point in the image, usually the center.

Here’s an example:

```import cv2

# Get the image center
center = (img.shape[1]//2, img.shape[0]//2)

# Calculate the rotation matrix
rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1.0) # Rotate 45 degrees

# Rotate the image using the rotation matrix
rotated_img = cv2.warpAffine(img, rotation_matrix, (img.shape[1], img.shape[0]))

# Save or display the output
cv2.imwrite('rotated.jpg', rotated_img)```

Output: An image saved as ‘rotated.jpg’, rotated by 45 degrees around its center.

This code snippet calculates a rotation matrix for a 45-degree clockwise rotation with respect to the image center and then applies this matrix to get a rotated image. The affined image is then saved to the filesystem.

## Method 3: Translation

Translation, or shifting an image within its own frame, can be carried out in OpenCV using an affine transformation. You specify the shift in pixels along the x and y axes in a transformation matrix, which you then apply to the image with `cv2.warpAffine()`.

Here’s an example:

```import cv2
import numpy as np

# Define the translation matrix
translation_matrix = np.float32([[1, 0, 100], [0, 1, 50]]) # Shift 100 pixels right and 50 pixels down

# Apply the translation
translated_img = cv2.warpAffine(img, translation_matrix, (img.shape[1], img.shape[0]))

# Save or display the output
cv2.imwrite('translated.jpg', translated_img)```

Output: An image saved as ‘translated.jpg’, shifted 100 pixels to the right and 50 pixels down.

The above code creates a translation matrix to shift the image 100 pixels right and 50 pixels down, then translates the image using the `cv2.warpAffine()` function. The result is a translated image which is then saved.

## Method 4: Affine Transformation

Affine transformations maintain collinearity and relative distances between points. In OpenCV, an imageβs affine transformation can be done using two functions: `cv2.getAffineTransform()` to create the transformation matrix and `cv2.warpAffine()` to apply it. To perform the transformation, you need three points from the input image and their corresponding locations in the output image.

Here’s an example:

```import cv2
import numpy as np

# Coordinates of triangle vertices in the input and output images
input_pts = np.float32([[50, 50], [200, 50], [50, 200]])
output_pts = np.float32([[10, 100], [200, 50], [100, 250]])

# Compute the affine transform matrix
affine_matrix = cv2.getAffineTransform(input_pts, output_pts)

# Apply the affine transformation
affine_img = cv2.warpAffine(img, affine_matrix, (img.shape[1], img.shape[0]))

# Save or display the output
cv2.imwrite('affine_transformed.jpg', affine_img)```

Output: An image saved as ‘affine_transformed.jpg’, showing an affine transformation based on the predefined points.

The code snippet defines sets of points in the source and destination images, computes the affine transformation matrix based on these points, and then applies it using `cv2.warpAffine()`. The image is modified according to the defined correspondences in the point sets.

## Bonus One-Liner Method 5: Perspective Transformation

Perspective transformation or homography allows mapping an image onto a new view like performing a birdβs-eye view for a planar object. In OpenCV, you can do this using four points on the input image and their corresponding points on the output image with `cv2.getPerspectiveTransform()` followed by `cv2.warpPerspective()`.

Here’s an example:

```import cv2
import numpy as np

# Corners of the original and destination points
src_points = np.float32([[0, 0], [img.shape[1], 0], [0, img.shape[0]], [img.shape[1], img.shape[0]]])
dst_points = np.float32([[0, 0], [img.shape[1], 0], [img.shape[1]*0.3, img.shape[0]], [img.shape[1]*0.7, img.shape[0]]])

# Compute the perspective matrix
perspective_matrix = cv2.getPerspectiveTransform(src_points, dst_points)

# Apply the perspective transformation
warped_img = cv2.warpPerspective(img, perspective_matrix, (img.shape[1], img.shape[0]))

# Save or display the output
cv2.imwrite('perspective_transformed.jpg', warped_img)```

Output: An image saved as ‘perspective_transformed.jpg’, transformed to a new perspective as defined by the point mappings.

With the original and destination points defined, the perspective matrix is computed, which maps the source points to the destination points. This matrix is then used to perform the perspective warp, resulting in the output image.

## Summary/Discussion

• Method 1: Scaling. Easily changes image dimensions. Good for zooming. Can cause quality loss on upscaling.
• Method 2: Rotation. Great for aligning images or correcting orientation. May introduce blank areas when rotating by non-orthogonal angles.
• Method 3: Translation. Useful for shifting objects within an image. Simple to implement. Cropping can occur if the translation is not handled properly.
• Method 4: Affine Transformation. Versatile for combining multiple transformations. Precise control over image shape change. Requires careful selection of reference points.
• Method 5: Perspective Transformation. Powerful for creating 3D effects and viewpoint changes. More complex and requires solving for a matrix.