• Reprint please note the original sources, thank: blog.csdn.net/pentiumCM/a…

CV – Perspective (projection) transformation

One, foreword

(1) The concept of perspective transformation

  1. Preface:

  2. Affine transformation principle:

    • Reference:

      • Principle explanation: blog.csdn.net/xiaowei_cqu…
      • Principle explanation including example code: blog.csdn.net/sinat_29957…
      • The sample code: www.1zlab.com/wiki/python…
    • Definition of Perspective Transformation (Baidu Encyclopedia) : Perspective Transformation refers to a Transformation that makes the shadow plane (Perspective plane) rotate a certain Angle around the trace line (Perspective axis) according to the Perspective rotation law under the condition that the center, image point and target point are collinear, destroys the original projected ray beam, and still keeps the projected geometric figure on the shadow plane unchanged.

      In short, a Plane (image) is projected to a new Viewing Plane by a projection matrix, also known as a projection mapping.

    • Derivation of perspective transformation formula:

      • (xo, Yo) is the original image coordinate point, written in the form of augmented vector [xo, Yo, 1], (X, Y, Z) is the target point after perspective transformation (in three-dimensional space), because the image is in two-dimensional space, the target point is divided by Z, and the point transformed into two-dimensional image is (X ‘, Y ‘, Z ‘), where: x’= X/Z,y’= Y/Z, z’=1

        In the transformation matrix, A11, A12, A21 and a22 represent linear transformation, mainly used for image scaling and rotation operations a31,a32 represent translation, a13 and a23 represent perspective transformation. The fourth argument, a33, is equal to 1.

      • The affine transformation matrix has 6 parameters, so we only need 3 coordinate pairs (6 equations) to solve it, while the perspective transformation matrix has 8 parameters, so we need 4 coordinate pairs (8 equations) to solve it.

    • Conclusion:

      • Given the points of the transformation, we can solve for the transformation. Conversely, a specific transformation formula can also produce a new transformed image.
      • Affine transformation is also a special form of perspective transformation
      • The perspective transformation matrix can be obtained according to the four coordinate pairs before and after the original image and projection, and then the perspective transformation of the original image can be carried out according to the perspective transformation matrix.

Two, code implementation

(a) OpencV function description

  • References:

    • The affine transformation and perspective transformation distinction: blog.csdn.net/flyyufenfei…

1. warpPerspective

  1. def warpPerspective(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None):

    • Explanation: The perspective transformation function can keep a line in shape, but parallel lines may no longer be parallel
    • Parameters: its related parameters are similar to those of cv2.warpAffine function, except that M is a 3×3 transformation matrix, the first two lines of the matrix are used for affine transformation, and the third line is used for perspective transformation.

Example 2.

  • Sample code:

    import cv2 import numpy as np def perspective(image): Pts1 = Np.float32 ([[56, 65], [250, 52], [28, 200], [280, 290]]) Pts2 = Np.float32 ([[0, 0], [300, 0], [0, 250], [300, pts2 = NP.float32 ([[0, 0], [300, 0], [0, 250], [300, pts2 = NP.float32 ([[0, 0], [300, # 250]]) to generate the transformation matrix M = cv2 getPerspectiveTransform DST (pts1 pts2) # perspective transform = cv2. WarpPerspective (image, M, (image.shape[1], image.shape[2])) return dstCopy the code

The resources