ECOVI | Computer Vision

L4: Perspective projection and homography

The goal

Your goal in this exercise is to test homography estimation in practice. for this task you will use analytical solution for the 4 point matching. Given some input images with planat objects taken from different angles (with perspective distortion visible) you have to pick four corner points, and based on them (and knowledge of real object size) you have to calculate homography matrix H, that will transform the image to en face view.

Sample data

As the sample, for how the algorithm should work, take a look at the following photo of the door (forst one).

Selected reference points (i.e. door corners) were used to calculate homography, that transforms first, distorted image into second one, where the view is perpendicular to the wall (door edges are vertical and horizontal). Additional knowledge, that is used to calculate homography, is the real size of the door, which is 200x90cm. In the second image door has the size of 270x600px. Last image is the crop from the warped image with the additional border of 10px on each side.

Task advancement

Calculating the homography based on 4 pairs of points [3p]

The clue of the task is homography calculation. Given the reference points (selected on the input image by mouse) and size of the object in the output image (or another set of four points) you have to create the linear system and solve it to obtain matrix H.

Warping and croping the image [1p]

For the calculated homography you have to warp the input image to get the perspective corrected one.

After the warping you should crop the image to contain only the selected object with a small (10px) border around.

Panorama stitching [2p]

Using the homography calculation, you have to create a panorama image from two photos. Select four points that correspond on both images and calculate homography transforming both images to the perspective corrected view and merge them.

Code base

To apply calculated homography to the image, you can base on the code below:

% load sample image

I=imread('door.jpg');

% show the image and wait for the points selection (be sure to check if exactly four points were selected)

figure
imshow(I)
[x,y] = getpts

% prepare reference points

points_in = [ . . . ];
points_out = [ . . . ];

% calculate the homography

H = calculate_homography(points_in, points_out);

% prepare image reference information

Rin=imref2d(size(I));

% convert homography matrix to the Matlab projective transformation

t = projective2d(H');

% warp the image and get the output reference information

[I2, Rout]=imwarp(I, Rin, t);
imshow(I2);

% crop the output based on the reference information

I3 = crop_image(I2, Rout, points_out);

Output reference information can be used to check the coordinates of the output image and crop only the object of interest.

Files