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

CV – Data enhancement: affine transformation

One, foreword

(1) Concept of affine transformation

  1. In the data enhancement of deep learning, we often need to carry out various image enhancement operations, such as translation, flip, Scale, Rotation, Shear, etc., which are actually affine transformations of images. To confirm: contrast, color jitter, noise

  2. Affine transformation principle:

    • An Affine transformation, also known as an Affine mapping, is a linear transformation of one vector space followed by a translation to transform it into another vector space in geometry.

      In simple terms, affine transformation is: linear transformation + translation.

    • Example:

      Suppose we have a vector space k: k = (x, y), and a vector space j: j = (x’, y’), and we can transform j into K by affine transformation.

      The steps are as follows:

      • Let: j = w * k + b, decomposition: x’ = w00 * x + W01 * y + b0, y’ = W10 * x + w11 * y + b1
      • Convert to matrix multiplication:throughParameter matrix MI can convert between two vector Spaces.
    • Conclusion: When carrying out affine transformation, we only need a matrix M to achieve translation, scaling, rotation and flip transformation.

      Affine transformation is a two-dimensional linear transformation between coordinate to the two-dimensional coordinate, it keeps the 2 d graphics “laymen” ping (straight after transformation is still a straight line) and “parallel” (2 d graphics remains the same, the relative position of the relationship between parallel lines remain parallel lines, and the position of the line on the order unchanged). Any affine transformation can be expressed in terms of multiplying by a matrix (linear transformation) plus a vector (translation).

(2) Affine transformation matrix

  1. Translation:

    # y = [[1, 0, dx], [0, 1, dy]] # y = [1, 0, dx], [0, 1, dy]Copy the code

  1. Flip (flip) :

    [[# flip horizontal M = 1, 0, w], [0, 1, 0]] # flip vertical M = [[1, 0, 0], [0, 1, h]]Copy the code

  2. Scale:

    M = [[fx, 0, 0], [0, fy, 0]]Copy the code

  1. Rotation:

    D = PI / 180 M = [[cosd, -sind, 0], [sind, cosd, 0]]Copy the code

  2. Shear:

    # a, b: To convert to radians, Angle * PI / 180 # x coordinate shear only M = [[1, tan (a), 0] to [0, 1, 0]] # y only shear M = [[1, 0, 0], [tan (b), 1, 0]] # x and y coordinates and shear M = [[1, tan (a), 0], [tan (b), 1, 0]]Copy the code

Two, code implementation

(a) OpencV function description

  • References:

    • Affine and opencv use: blog.csdn.net/zhuguiqin1/…
    • The affine transformation and perspective transformation distinction: blog.csdn.net/flyyufenfei…

1. warpAffine

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

    • Affine transformation function, can achieve rotation, translation, scaling; The parallel lines are still parallel

    • Parameters:

      SRC: input image DST: output image M: 2×3 transformation matrix Dsize: output image size after transformation Flag: interpolation method borderMode: boundary pixel expansion mode borderValue: boundary pixel interpolation, filled with 0 by default

2. 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.

(2) Implementation code

  • Complete code:

    • Complete code link: affine transform – OpencV

    • Take the translation of an affine transformation as an example. The other transformations simply replace the matrix of the affine transformation:

      Import cv2 import numpy as np def translation_img(image, pad_color=(114, 114, 114)): 2. Use warpAffine function in OpenCV :return: "" # define an image translation matrix, matrix M: 100 pixels # M = [[1, 0, dx], # [0, 1, dy]] M = np.array([[1, 0, - 100], [0, 1, 100]]. Dsize = image.shape[:2][::-1] # use gray to fill the boundary image = cv2.warpaffine (image, M, image) dsize, borderValue=pad_color) return image if __name__ == '__main__': image = cv2.imread('000007.jpg') cv2.imshow('org_img', image) image_dst = translation_img(image) cv2.imshow('image_dst', image_dst) cv2.waitKey(0) cv2.destroyAllWindows()Copy the code
  • The effect is as follows:

    The image on the left is the original image, and the image on the right is the image after translation affine transformation.

\