Affine transformation is a function which transform an image while preserving the points, straight lines and planes i.e., the set of parallel lines remain parallel after performing affine transformation.

Image result for affine transformation"

In openCV, to obtain a transformation matrix for affine transformation we use 

cv2.getAffineTransform(src, dst) function in which we need three points from input image and their corresponding locations in output image to obtain the required transformation matrix. The function takes two arguments:

  1. src: it specifies 3×2 matrix of three points in the input image.
  2. dst: it specifies 3×2 matrix of three points in the output image.

The resultant matrix is then passed to cv2.warpAffine() function to get the output image.

Implemented code in Python:

import cv2
import numpy as np

img = cv2.imread('geometric.png',1);
rows,cols,channels = img.shape;

pts1 = np.float32([[0,50],[200,100],[50,200]]);
pts2 = np.float32([[0,100],[200,50],[100,250]]);

M = cv2.getAffineTransform(pts1,pts2);

result = cv2.warpAffine(img,M,(cols,rows));

cv2.imshow('original', img);
cv2.imshow('output', result);

#wait for 10 seconds

Thank you Rishika Gupta for this article.


Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Additional settings
Formula color
Text color
Type math using LaTeX
Nothing to preview