how to get Homography matrix using OpenCV (Stitching 2 images)

12,780

Solution 1

First you should be aware that two point correspondences, which you illustrate in your image, are not sufficient for homography estimation. A homography has 7 free parameters - there are 8 matrix elements, but a homography is a homogeneous quantity. This means you can scale the matrix as you like, which reduces the free parameters by one. To estimate 7 parameters you need at least four corresponding points.

I think understanding what you do is more important than blindly calling OpenCv methods, so please read about the real algorithms. The simplest (but non-optimal) method for homography computation is DLT, which amounts te rearranging the equation y = Hx for four or more points in order to solve for the components of H in a least squares sense. This is a nice explanation of the details: https://engineering.purdue.edu/kak/courses-i-teach/ECE661.08/solution/hw4_s1.pdf The principal academic reference is Multiple View Geometry for Computer Vision by Heartley and Zisserman.

As pointed out in the comments, the OpenCv docs are here: http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#findhomography

Solution 2

To establish an homography between two images you need at least 4 points. Each one has two coordinates, giving you two equations / constraints. An homography has 8 degrees of freedom (it's a 3x3 full-rank matrix with 9 elements, -1 d.o.f. because of scale since it operates on homogeneous coordinates).

Note that only images of planar surfaces, or images taken by a camera rotating around its center can be related by an homography (in this latter case, the planar surface is the plane at infinity).

This page shows how to compute an homography with feature extraction and RANSAC.

Share:
12,780
AHmedRef
Author by

AHmedRef

I'm a Multi-developper.

Updated on June 14, 2022

Comments

  • AHmedRef
    AHmedRef almost 2 years

    I'm working on OpenCV in visual studio 2012 , i tried to get Homography matrix to Stitch 2 images in opencv so really i don't know the steps to follow to do'it :

    enter image description here enter image description here