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

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.