Rectify Image Perspective

Adjust the perspective of an image into a front-facing angle

proj_perspective thumbnail

Introduction

The goal of the project is to align the perspective of an image into a front view. Here, we use a four-point rectification method to correct the perspective. That is, we select the four points that will be the vertices of a rectangle when we face the rectangle orthogonally.

Implementation

Step 1: find the four points

In this case, we would like to transform the book cover to a front view. The four points are the vertices of a book cover. The yellow circles indicate the selected points.

Book Image before perspective rectification

Step 2: calculate the homography matrix

The homography matrix (a 3x3 matrix) is a linear mapping from the points in the first image to the points in the second image. In this example, the second image will be a front view of the book cover. We use the coordinates of the four points to calculate the homography matrix. I implemented in C++, and you could refer to my source code for more details. (In OpenCV, you could simply call the function, findHomography(), to compute the homography matrix. In my source code, I use the OpenCV function to verify my result.)

Step 3: project points on an empty image

After finding the homography matrix, we perform a linear transform and map all points in the first image to the front-view image. We use the OpenCV function, wrapPerspective(), to fulfill the mapping computation. After the perspective rectification, the image below is the front view of the book cover.

Book Image after perspective rectification

Application

We often use the perspective rectification to correct the distortion of wide-angle camera lens. Here is the example. We would like to move the perspective to the four points on the wall. The yellow circles indicates the selected four points. It will look like that we are facing directly to the front side of the concrete wall. The result shows as below.

Compare by SIFT

Before the perspective rectification.

Compare by SIFT

After the perspective rectification.


How to compile c++ file that includes OpenCV library?

g++ -ggdb perspective_wall.cpp -o perspective_wall `pkg-config --cflags --libs opencv`


Development Tools

C++
OpenCV
MultipleView Geometry
Author: Patrick Yu

© All rights reserved.