Python OpenCV - Affine Transformation
Last Updated :
28 Apr, 2025
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it integrated with various libraries, such as Numpy, Python is capable of processing the OpenCV array structure for analysis. Note: For more information, refer to OpenCV Python Tutorial
Affine Transformation
In Affine transformation, all parallel lines in the original image will still be parallel in the output image. To find the transformation matrix, we need three points from input image and their corresponding locations in the output image. Then cv2.getAffineTransform will create a 2x3 matrix which is to be passed to cv2.warpAffine. cv2.getAffineTransform method:
Syntax: cv2.getPerspectiveTransform(src, dst) Parameters: src: Coordinates of quadrangle vertices in the source image. dst: Coordinates of the corresponding quadrangle vertices in the destination image.
cv2.warpAffine method:
Syntax: cv2.warpAffine(src, M, dsize, dst, flags, borderMode, borderValue) Parameters: src: input image. dst: output image that has the size dsize and the same type as src. M: transformation matrix. dsize: size of the output image. flags: combination of interpolation methods (see resize() ) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation (dst->src). borderMode: pixel extrapolation method; when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function. borderValue: value used in case of a constant border; by default, it is 0.
Example 1:
Python3
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('food.jpeg')
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50],
[200, 50],
[50, 200]])
pts2 = np.float32([[10, 100],
[200, 50],
[100, 250]])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))
plt.subplot(121)
plt.imshow(img)
plt.title('Input')
plt.subplot(122)
plt.imshow(dst)
plt.title('Output')
plt.show()
# Displaying the image
while(1):
cv2.imshow('image', img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
Output:
Example 2:
Python3
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('food.jpeg')
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50],
[200, 50],
[50, 200]])
pts2 = np.float32([[10, 100],
[200, 50],
[100, 250]])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))
plt.subplot(121)
plt.imshow(img)
plt.title('Input')
plt.subplot(122)
plt.imshow(dst)
plt.title('Output')
plt.show()
# Displaying the image
while(1):
cv2.imshow('image', img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
Output: 
Similar Reads
Image Transformations using OpenCV in Python In this tutorial, we are going to learn Image Transformation using the OpenCV module in Python. What is Image Transformation? Image Transformation involves the transformation of image data in order to retrieve information from the image or preprocess the image for further usage. In this tutorial we
5 min read
Perspective Transformation - Python OpenCV In Perspective Transformation, we can change the perspective of a given image or video for getting better insights into the required information. In Perspective Transformation, we need to provide the points on the image from which want to gather information by changing the perspective. We also need
2 min read
Image Translation using OpenCV | Python Translation refers to the rectilinear shift of an object i.e. an image from one location to another. If we know the amount of shift in horizontal and the vertical direction, say (tx, ty) then we can make a transformation matrix e.g. \begin{bmatrix} 1 & 0 & tx \\ 0 & 1 & ty \end{bmatr
2 min read
OpenCV Tutorial in Python OpenCV, short for Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. Originally developed by Intel, it is now maintained by a community of developers under the OpenCV Foundation.OpenCVis a huge open-source library for computer vision, machin
3 min read
Negative transformation of an image using Python and OpenCV Image is also known as a set of pixels. When we store an image in computers or digitally, itâs corresponding pixel values are stored. So, when we read an image to a variable using OpenCV in Python, the variable stores the pixel values of the image. When we try to negatively transform an image, the b
4 min read