opencv image transformation image shifting principle Detailed image rotation Image scaling

Common 2D image is converted from affine transformation principle including 2 × 3 matrix and the 3 × 3 matrix based on the perspective transformation.

Affine transformation

principle

Basic image transformation is a two-dimensional coordinate transformation: from one two-dimensional coordinates (x, y) to another two-dimensional coordinate (u, v) of the linear transformation:
Affine transformation formula
If written in matrix form is:
Affine transformation matrix representation
defined as follows:
Each portion represents the affine transformation matrix is ​​described
matrix T (2 × 3) called affine transformation is a transformation matrix, R is the linear transformation matrix, t is the translation matrix, simply, an affine transformation is a linear transformation + translation. After the conversion is still straight straight, parallel lines are still parallel lines, the relative positional relationship between the straight lines unchanged, thus corresponding to three non-collinear points can be determined only one of the affine transformation, linear transformation four degrees of freedom translational + 2 degrees of freedom affine transformation → 6 degrees of freedom.

opencv achieve affine transformation

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('drawing.jpg')
rows, cols = img.shape[:2]

# 变换前的三个点
pts1 = np.float32([[50, 65], [150, 65], [210, 210]])
# 变换后的三个点
pts2 = np.float32([[50, 100], [150, 65], [100, 250]])

# 生成变换矩阵
M = cv.getAffineTransform(pts1, pts2)
# 第三个参数为dst的大小
dst = cv.warpAffine(img, M, (cols, rows))

plt.subplot(121), plt.imshow(img), plt.title('input')
plt.subplot(122), plt.imshow(dst), plt.title('output')
plt.show()

Experimental results

Comparison before and after the affine transformation in FIG.

Application opencv in ready-made image translation, rotation, scaling, flip

Please refer to opencv to achieve image geometric transformation

Translation

Graphic translation
Is a direct translation movement in the x and y directions, up and down / left movement, a degree of freedom of 2, the transformation matrix can be expressed as:
Translation transformation matrix representation

Rotation

Graphic rotation
Rao is the rotation axis direction of the origin of a certain rotational angle [theta], 1 degree of freedom, a translation does not contain, as clockwise rotation may be expressed as:
Rotation transformation matrix representation

Overturn

Flip is negated on the x or y direction or all in one direction, two degrees of freedom, such as vertically inverted here as an example:
Vertical flip transform matrix represents

Rigid transformation

Rotation + translation, also known as rigid transformation (Rigid Transform), that is to say if the before and after image transformation distance between two points remain the same, then this change is called rigid transformation. Body transformation include translation, rotation and flip, 3 degrees of freedom. Since only the rotation and translation, the rigid transformation remains constant length between the straight lines, it is also known as European transformation (change before and after holding Euclidean distance). Transformation matrix can be expressed as:
Rigid transformation matrix representation

Scaling

Graphic zoomingScaling is a measure of (multiple) converting the x and y directions, zoom ratio and the like are also referred to as non-stretched in the some of the information / pressing, a scaling degrees of freedom, non-scaling 2 degrees of freedom, the matrix can Expressed as:
Scaling transformation matrix representation

Similarity transformation

Known as rotary scale similarity transformation, similarity transformation comprises rotation, scaling and translation transformation, 4 degrees of freedom. In OpenCV, the rotation is achieved by similarity transformation:
if the scale is Scale, the rotation angle [theta], is the center of rotation (centerx, centery), the affine transformation can be expressed as:
Represents a similarity transformation matrix
where:
In the above formula α and β calculated
a similarity transformation as compared to a rigid transformation plus scale, it will not keep the Euclidean distance unchanged, but the angle between the straight line remains the same.

Perspective transformation

After the affine transformation is still in front of a parallelogram, and can not do any conversion.
Various perspective transform

principle

Perspective transformation (Perspective Transformation) is a two-dimensional image projected onto a three-dimensional view plane, and then converted to the two-dimensional coordinates, it is also referred to as a projection map (Projective Mapping). It is simply a process of three-dimensional → → two-dimensional.
Perspective transformation formula:
Perspective transformation formula
a perspective transformation matrix represents:
Represents a perspective transformation matrix
an affine transformation is a perspective transformation subset. Next, the Z-axis and then dividing by the conversion into two-dimensional coordinates:
The three-dimensional perspective transformation -> two-dimensional
perspective transformation is more flexible compared to the affine transformation, after transformation produces a new quadrilateral, but not necessarily a parallelogram, so it is necessary to four non-collinear points the only determined after the original linear transformation is still straight. Because quadrilateral parallelogram comprising all, so all of the perspective transformation includes the affine transformation.

opencv achieve perspective transformation

OpenCV The first four points before and after the transformation to generate 3 × 3 matrix transformation with cv.getPerspectiveTransform (), then with cv.warpPerspective () for a perspective conversion.

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('card.jpg')

# 原图中卡片的四个角点
pts1 = np.float32([[148, 80], [437, 114], [94, 247], [423, 288]])
# 变换后分别在左上、右上、左下、右下四个点
pts2 = np.float32([[0, 0], [320, 0], [0, 178], [320, 178]])

# 生成透视变换矩阵
M = cv.getPerspectiveTransform(pts1, pts2)
# 进行透视变换,参数3是目标图像大小
dst = cv.warpPerspective(img, M, (320, 178))

plt.subplot(121), plt.imshow(img[:, :, ::-1]), plt.title('input')
plt.subplot(122), plt.imshow(dst[:, :, ::-1]), plt.title('output')
plt.show()

Experimental results

Revised results Pictures

to sum up

The image illustrates various changes
The image illustrates various changes
Affine transformation and perspective transformation properties are summarized

Guess you like

Origin www.cnblogs.com/wojianxin/p/12591410.html