Perspective Transformation – A Manual Way
In last few posts we deal with Affine transforms, which we constructed our own transformation matrices to perform the transformation.
Today, we will look into a more useful transformation –> Perspective Transform, which used to transform the 3d world into 2d image.
Let say we have an image taken by camera as below:
Notice that the image was taken from the left angle and the Sudoku puzzle box was slanted. Our purpose today is to make the slanted image flat to our view in 2D, so that we could used it in our coming post, including solving the puzzle by Scilab. 🙂
Let’s do it in manual way today.
// Import image S=imread('sudoku1.jpg'); // Select 4 points from the figure src = warpmatselect(S,4); |
At this stage, a figure with the image above will be shown and waiting for the user to click on the image to select 4 meaningful points as the source. Select the points in proper sequences as below:
Next, we will choose the location which we want our image to map to:
tgt = warpmatselect(S,4); |
Do note that you need to choose the points in the same orientation with the sources points in order to get the desired result.
The perspective transform will map the source’s point 1 to target’s point 1, and so on.
// Find the transformation matrix and perform the transformation mat = imgettransform(src,tgt,'perspective'); Snew = imtransform(S,mat,'perspective'); imshow(Snew); |
Still not satisfied with the result? You could always make it better by defining the target points manually:
sz = size(S); tgt = [0 0;0 sz(1);sz(1) sz(1);sz(1) 0]; mat = imgettransform(src,tgt,'perspective'); Snew2 = imtransform(S,mat,'perspective'); Snew3 = imcrop(Snew2,[1 1 sz(1) sz(1)]); imshow(Snew3); |
How about now?
Please take note that the image coordinates are different with Cartesian Coordinates, where the origin of the image is on the upper left corner while the Cartesian space origin located at the bottom left.