How it works

Both affine and perspective transformations are essentially matrix multiplication operations, where positions of elements are remapped with some warp matrix. So, to apply a transformation we need to compute such a warp matrix. For affine transformation, this can be done with the cv2.getAffineTransform function. It takes two set of points as arguments: the first one contains three points before transformation, and the second one contains three corresponding points after warp. The order of points in the sets does matter, and it should be the same for both arrays. To create the transformation matrix in the case of perspective warp, cv2.getPerspectiveTransform can be applied.

Again, it accepts two sets of points before and after ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.