What are warpAffine and warpPerspective in OpenCV

This recipe explains what are warpAffine and warpPerspective in OpenCV

Recipe Objective: What are warpAffine() and warpPerspective() in OpenCV?

The objective of this recipe is to explain two main functions of OpenCV which are v2.warpAffine() and cv2.warpPerspective()

Step 1: Import the necessary libraries and read the image

Let us first import the required libraries and read the images. The image that we are using here is the one shown below. Let us also store the shape of the image using the .shape() function.

INPUT IMAGE

import cv2
import numpy as np
image = cv2.imread('project.jpg')
rows,cols,channels = image.shape

Step 2: Image Translation

Image translation is nothing but shifting the location of the image in the (x,y) direction. To do that, we have to create a matrix in the following format

matrix.jpg

where tx and ty are the values to which the image is relocated. This can be implemented using the cv2.warpAffine() function, which takes the following parameters

  • src: The image which is to be translated
  • M: The translation matrix
  • dsize: Output size

M = np.float32([[1,0,50],[0,1,20]])
result = cv2.warpAffine(image,M,(cols,rows))

cv2.imshow('Image after Translation',result)
cv2.waitKey(0)

Output:

output1.jpg

Step 3: Perspective Transformation

A 3X3 matrix is required to perform Perspective Transformation. This transformation matrix can be formed by selecting four points on the input and the output image. Out of these 4 points, at least 3 of them should not be collinear. This transformation matrix can be obtained by cv2.getPerspectiveTransform function.

Once we have successfully obtained the transformation matrix, we can then use the cv2.warpPerspective() function, which takes the following parameters to perform Perspective Transformation

  • src: The image which is to be transformed
  • M: The translation matrix
  • dsize: Output size

pts1 = np.float32([[26,25],[168,22],[28,187],[189,190]])
pts2 = np.float32([[0,0],[200,0],[0,200],[200,200]])
M = cv2.getPerspectiveTransform(pts1,pts2)
result_2 = cv2.warpPerspective(image,M,(400,100))

cv2.imshow('Image after Perspective Transformation',result_2)
cv2.waitKey(0)

Output:

output2.jpg

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.