Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Understanding Homography Theoretically
2.1.
Calculating Homography using point correspondences
3.
Implementation in Python 
4.
Frequently Asked Questions
4.1.
What is Homography used for?
4.2.
What is image homography?
4.3.
Why do we need 4 points for Homography?
4.4.
How many degrees of freedom does Homography have?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Homography

Introduction

Homography is a projective transformation in which projections are used to connect two images. Homographies were initially introduced to study shifts in perspective, and they have enabled people to understand better how images change when we look at them from a different perspective.

Homography describes the projective geometry of two cameras and a world plane. Homography is a technique for mapping images of locations on a world plane from one camera view to another. Because it is based solely on the intersection of planes and lines, it is a projective connection. As depicted in the diagram below:

source 

There are two only cases for which Homography applies (both cases assume that the world view can be modelled by plane):

  1. Images are captured by the same camera but at a different angle (the world is now essentially a plane)
  2. Two cameras are viewing the same plane from a different location. 

Understanding Homography Theoretically

Homography (also known as planar Homography) is a change that takes place between two planes. In other terms, it is a transformation of a picture between two planar projections. In a homogeneous coordinates space, it is represented by a 3x3 transformation matrix. The homography matrix is expressed mathematically as:

So Homography is nothing but a function that converts one planar projection of an image to some other plane. 

Let's suppose we want to paste some logo to some video or another image; there, we will need this Homography as we are going to map the image points to a new image. 

So let's see more details about how we calculate the homography matrix. 

Calculating Homography using point correspondences

Let's go over some of the attributes of a homography matrix before we get into how to calculate Homography using point correspondences. Homography connects points in the first perspective to points in the second view, and because neither view has any constraints, it is a full rank (=3) matrix. Homography is also defined up to a scale (c in the preceding equation), which means it can be altered by a non-zero constant without affecting projective transformation. Homography has 8 degrees of freedom despite the fact that it comprises nine elements (3x3 matrix), i.e. there are eight unknowns to solve. We only need 4 points in the football goal post example because each point includes x and y coordinates, and there are a total of 8 unknowns to solve H for a unique solution. These four points could be the goalpost's four corners.

To make the solution to equation Ah=0 more stable in the presence of noise and to prevent divergence from the right answer, Hartley and Zisserman recommended a normalisation step before calculating h. The goal is to find a transformation matrix T and T' that transforms x and x' so that the centroid of these points is the coordinate origin and the average distance from it is fixed. Using the given equation, we can recover the original homography matrix after calculating H' from the altered points.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation in Python 

Implementation in python is very easy; we can just use the open cv library and easily find out the homography matrix using at least four corresponding points that map with each other in the plane. Once the homography matrix is found, we can find the corresponding points in another plane for every point by matrix multiplication. Here is the basic example where we are first calculating the homography matrix, and then we find the transformation of the image as a whole. 

'''
pts_src and pts_dst are NumPy arrays of points
in source and destination images. We need at least
4 corresponding points.
'''
h, status = cv2.findHomography(pts_src, pts_dst)
 
'''
The calculated Homography can be used to warp
the source image to the destination. Size is the
size (width, height) of im_dst.
'''
 
im_dst = cv2.warpPerspective(im_src, h, size)

 

Also see, Sampling and Quantization

Frequently Asked Questions

What is Homography used for?

Homography lets us relate two cameras viewing the same planar surface; Both the cameras and the surface that they view (generate images of) are located in the world-view coordinates. In other words, two 2D images are related by a homography H if both view the same plane from a different angle.

What is image homography?

QuillBot's paraphraser takes your sentences and alters them, allowing you to swiftly revise and rewrite your text!

Why do we need 4 points for Homography?

Homography is the relationship between two planes, and because the degree of freedom in the homography transform is 7, you'll need at least four corresponding points. 4 points equals 4 pairs of (x,y), allowing you to calculate 7 variables.

How many degrees of freedom does Homography have?

The homography matrix is a 3x3 matrix but with 8 DoF (degrees of freedom) as it is estimated up to a scale.

Conclusion

In a nutshell, Homography is a technique where we can project the image which is taken with one kind of camera position to some other kind of camera position so that more than one image can be overlaid over each other. 
Hey Ninjas! Don't stop here; check out Coding Ninjas for Machine Learning, more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. 

Happy Learning!

Next article
Object Detection Using Deep Learning
Live masterclass