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.

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

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
cv2.waitKey(10000);
cv2.destroyAllWindows();

$${}$$