Perspective rectification

This is a cool perspective rectification method that I learnt last week. It probably has also a lot of other useful applications in addition to this one.

norseland1 Figure 1.

When cataloging uncommon books at LibraryThing, one can often notice that there may be no cover image available for the correct edition of the book. It can be quite irritating if you want to keep your virtual bookshelf as close to the original as possible. Because scanning the book covers and uploading them to LibraryThing is often too troublesome (my scanner is in the other room!), it’s more convenient to search for the correct image from the Internet. Sometimes it is possible to find only photographs like the one in Figure 1 on the right,[1]Mara L. Pratt (ed.). Legends of Norseland. Educational Publishing Company, Boston, 1894. which are not suitable LT cover images as such, but need some editing in order to look nice on the book list. If this is the case and you happen to be somewhat familiar with MATLAB, you can always use a thing called homography to do a perspective rectification on the image. There is a good planar homography lecture by Peter Corke on YouTube. He probably explains the concept better than me, so I recommend to watch the video instead of trying to introduce the thing myself.

For this method, you will need MATLAB and Corke’s Machine Vision Toolbox (MVTB). I needed also the MATLAB Functions for Multiple View Geometry even though I didn’t notice it in MVTB’s installation instructions but I may have failed to install the toolbox correctly.

When you are ready, run the following script in MATLAB. (You can of course change the details like filenames and the X matrix if needed and if you know what you’re doing.) It should open a new window with the image of your book. You will need to tell MATLAB where the corners of the book are by clicking them in counterclockwise order (or depending on your X matrix) starting from the top left corner. MVTB then uses the magic that was explained in Corke’s video lecture and straightens the image’s perspective.

norseland2 Figure 2.
im = imread('input.jpg');
P = ginput(4)';
X = [min(P(1,:)), min(P(2,:)); 
     max(P(1,:)), min(P(2,:)); 
     max(P(1,:)), max(P(2,:)); 
     min(P(1,:)), max(P(2,:))]';
H = homography(P, X);
output = homwarp(H, im);
imwrite(output, 'output.jpg');

The most relevant things in these lines are the homography and homwarp functions that came with MVTB. First, homography calculates the homography matrix from P to X, and then homwarp applies the homography matrix to every pixel in the image. The output will be something like the image in Figure 2.

norseland3 Figure 3.

Now it should be easy to crop the image and do the possibly necessary width/height scaling using your favourite image editing program. For example, if you are on Windows, Paint is comfortably simple software for this step. If you don’t want to do the scaling manually, you can also modify the X matrix suitably, but that might be more impractical. The final product will be similar to Figure 3. The quality of the image is a little worse compared to scanning but it’s a good alternative if a scanner is not available, and it’s still good enough for using at LibraryThing.

References   [ + ]

1. Mara L. Pratt (ed.). Legends of Norseland. Educational Publishing Company, Boston, 1894.
Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *