OpenCV-Python Development Guide (12) --- Affine of Geometric Transformation

table of Contents

Affine

In OpenCV, affine transformation refers to a series of geometric transformations to achieve translation, rotation and other operations. This transformation can maintain the straightness and parallelism of the image. Straightness means that after the image undergoes affine transformation, the straight line is still a straight line; parallelism means that the image is still parallel after the affine transformation is completed.

In OpenCV, the affine function it provides us is cv2.warpAffine(), which is implemented by a transformation matrix M. If you don’t know much about matrix operations, you can remember what will be explained later, or you can learn discrete mathematics or linear algebra. , Both have explained matrix operations.

The definition of the affine function is as follows:

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

src: represents the original image to be affine

M: Represents a 2*3 transformation matrix. Using different transformation matrices, different affine transformations can be realized.

dszie: represents the size of the output image.

dst: represents 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 an inverse transformation type, which implements the inverse transformation from the target image dst to the original image src. Detailed parameters, the previous blog post table is.

borderMode: represents the edge type, the default is BORDER_CONSTANT. When the value is BORDER_TRANSPARENT, it means that the values ​​in the target image will not be changed, and these values ​​correspond to the abnormal values ​​in the original image.

borderValue: represents the border value, the default is 0.

In summary, our commonly used parameters are: src, M, dsize.

Pan

The known affine formula is:

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

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

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

Obtain the value of each element in M:

M11 = 1

M12=0

M13 = 50

M21 = 0

M22 = 1

M23 = 100

In summary, the transformation matrix for shifting right by 50 pixels and shifting down by 100 pixels is:

M

Knowing the transformation matrix and the original image, we can complete the translation operation of the image very simply. 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()

After running, the effect is as follows:
image

Spin

When using the function cv2.warpAffine() to rotate the image, the conversion matrix can be obtained through the function cv2.getRotationMatrix2D(). The syntax format of this function is:

def getRotationMatrix2D(center, angle, scale):

center: is the center of rotation

angle: the angle of rotation, a positive number means counterclockwise rotation, and a negative number means clockwise rotation

scale: is the transform size (that is, the zoom size mentioned above)

Next, let's rotate the above picture by 45 degrees, the specific code is as follows:

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()

Just change the M transformation matrix, where (w / 2, h / 2) is the center coordinate of the image, 45 is a positive number, that is, rotate 45 degrees counterclockwise, and 0.6 is to zoom the image by 0.6 times.

After running, the effect is as follows:
image

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113802561