Translation is the movement of the matrix. Usually, we need to define a transformation matrix , which is a matrix with 2 rows and 3 columns
The tx
and ty
in the matrix represent the translation distance in the x
and y
directions, respectively
The translation is implemented using the radial transformation function cv2.warpAffine
, whose function prototype is
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
in
-
src
input image -
M
2 rows and 3 columns transformation matrix -
dsize
the size of the output image -
dst
optional parameter, output image, can also be used as return value -
flags
optional parameter, interpolation method -
borderMode
optional parameter, border mode -
borderValue
optional parameter, border fill value, default is 0
Example
import cv2 import numpy as np from sqlalchemy import column image = cv2.imread("lenna.png") cv2.imshow("original image", image) column, row, channel = image.shape # 变换矩阵,x放心移动100,y方向移动50 M = np.float32([[1, 0, 100], [0, 1, 50]]) dst=cv2.warpAffine(image, M, (column, row)) cv2.imshow('dst', dst) cv2.waitKey(0) cv2.destroyAllWindows()
This article is transferred from https://xugaoxiang.com/2022/07/09/opencv-translation/
This site is for inclusion only, and the copyright belongs to the original author.