affine

In OpenCV, affine transformation refers to the image through a series of geometric transformation to achieve translation, rotation and other operations. The transformation can keep the flatness and parallelism of the image. Flatness refers to the image after affine transformation, the line is still a straight line; Parallelism means that the image is still parallel after affine transformation.

In OpenCV, it provides us with the affine function cv2.warpaffine (), which is realized by a transformation matrix M. For those who are not familiar with matrix operations, you can remember the following explanation, or you can learn discrete mathematics or linear algebra, both of which explain matrix operations.

Affine functions are defined as follows:

def warpAffine(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None) :
Copy the code

SRC: represents the original image to affine

M: represents a 2*3 transformation matrix. Different transformation matrices can be used to achieve different affine transformations.

Dszie: indicates the size of the output image.

DST: the output image after affine

Flags: represents the interpolation method. The default is INTER_LINEAR. When the value is WARP_INVERSE_MAP, it means that M is the invert type, implementing the invert from the target image DST to the original image SRC. Detailed parameters, as shown in the table in the previous post.

BorderMode: Represents the edge type, which defaults to BORDER_CONSTANT. When this value is BORDER_TRANSPARENT, it means that the values in the target image do not change, and these values correspond to the outliers in the original image.

BorderValue: Represents the boundary value. The default is 0.

In summary, the commonly used parameters are SRC,M and dsize.

translation

The affine formula is known as:

Suppose we now want to shift the image 50 pixels to the right and 100 pixels to the bottom, then the formula is replaced as follows:

dst(x,y)=src(x+50,y+100)

DST (x, y) = SRC (+ 0 / x, y + 50, 0 x + y + 1 100).

The values of each element in M are:

M11=1

M12=0

M13=50

M21=0

M22=1

M23=100

To sum up, the transformation matrix of 50 pixels right and 100 pixels down is:

Given the transformation matrix and the original image, it is very simple for us to complete the image translation operation, the specific code is as follows:

import cv2
import numpy as np

img = cv2.imread("4.jpg")
h, w = img.shape[:2]
x = 50
y = 100
M = np.float32([[1.0, x], [0.1, y]])
move_img = cv2.warpAffine(img, M, (w, h))
cv2.imshow("img", img)
cv2.imshow("move_img", move_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result should look like this:

rotating

When the image is rotated using the function cv2.warpaffine (), the transformation matrix can be obtained using the function cv2.getrotationMatrix2d (). The syntax for this function is:

def getRotationMatrix2D(center, angle, scale):

Center: indicates the center of rotation

Angle: indicates the rotation Angle. A positive number indicates counterclockwise rotation, and a negative number indicates clockwise rotation

Scale: indicates the scale of the transformation.

Now, let’s rotate the figure 45 degrees, as shown below:

import cv2

img = cv2.imread("4.jpg")
h, w = img.shape[:2]
M = cv2.getRotationMatrix2D((w / 2, h / 2), 45.0.6)
move_img = cv2.warpAffine(img, M, (w, h))
cv2.imshow("img", img)
cv2.imshow("move_img", move_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

Change the M transformation matrix, where (W / 2, h / 2) is the central coordinate of the image, 45 is a positive number, that is to rotate 45 degrees counterclockwise, 0.6 is to scale the image 0.6 times.

After running, the result should look like this: