Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Transformationmatrix: affine vs. perspective warping

Hi I'm working on image transformation at the moment, but there is one thing I don't understand regarding warping. Namely whats the difference between the warpAffine() and warpPerspective()?

By that I don't mean whats the differences in transformation at all. My question is why I can use the same transformation matrix (3x3) in both of the above mentioned functions? And is the output the same (if run with the same transformation matrix)

Or when to use the one, when the other?

like image 711
flor1an Avatar asked Aug 11 '17 14:08

flor1an


1 Answers

Affine transformations can be thought of as a subset of all possible perspective transformations, aka homographies.

The main functional difference between them is affine transformations always map parallel lines to parallel lines, while homographies can map parallel lines to intersecting lines, or vice-versa.

Different transformations

Starting with a regular square, you can see that translational and Euclidean transformations (rotation, uniform scaling, and translation) keep the aspect ratio; the result on applying is still a square. However, affine transformations can squash the square into a rectangle in either direction, and it can also provide a shear/skew to the square. But notice that the shape after the affine transformation is applied is a parallelogram---the sides are still parallel. With a homography, this need not be the case. The parallel lines could be warped so that they intersect. So the result of a rectangle transformed by an homography is a general quadrilateral, while the result of a rectangle transformed by an affine transformation is always a parallelogram.


The functions warpAffine() and warpPerspective() don't necessarily need to be two distinct functions. Any affine transformation written as a 3x3 matrix could be passed into warpPerspective() and transformed all the same; in other words, a function like warpPerspective could have been made to take 2x3 and 3x3 matrices. However, perspective transformations apply extra steps because of the additional row, so it's not as efficient to do that. Also, warpPerspective performs a division, so there could be larger floating-point errors introduced in the operations, and that's just unnecessary for affine transformations. Lastly, inverting an affine warp can be done without creating a 3x3 square matrix and inverting that. So all in all, it just makes sense to keep them as separate functions.

like image 162
alkasm Avatar answered Jan 01 '23 12:01

alkasm