The 3*3 homography matrix has the following form
Given n pairs of points of the first image and the second image, we want to find H to minimized the following cost
Then we can use least square method to recover H
We use the inverse matrix to implement warping. By multiplying the coordinates into the original image, we can get a new warping image.
As for image rectification, we simply select 4 points of the object corners in the original image and choose 4 corners in the output image to be rectangle. Below are some example images.
We first create a large background to place the images in it. Then we make the following mask
Below are the results