This an affine transform that simply shifts the position of an image.
We use cv2.warpAffine to implement these transformations.
OpenCV provides two transformation functions, cv.warpAffine
and cv.warpPerspective
, with which you can perform all kinds of transformations. cv.warpAffine takes a 2x3 transformation matrix
while cv.warpPerspective takes a 3x3 transformation matrix
as input.
https://docs.opencv.org/4.x/da/d6e/tutorial_py_geometric_transformations.html
A transformation that can be expressed in the form of a matrix multiplication (linear transformation) followed by a vector addition (translation).
From the above, we can use an Affine Transformation to express:
Rotations (linear transformation)
Translations (vector addition)
Scale operations (linear transformation)
you can see that, in essence, an Affine Transformation represents a relation between two images.
https://docs.opencv.org/4.6.0/d4/d61/tutorial_warp_affine.html
Translation is the shifting of an object's location. If you know the shift in the (x,y) direction and let it be (tx,ty), you can create the transformation matrix M as follows:
$$M= \begin{bmatrix} 1 & 0 & t_{x} \\ 0 & 1 & t_{y} \end{bmatrix}$$import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('images/input.jpg')
height, width, _ = image.shape
# | 1 0 Tx |
# T = | 0 1 Ty |
# T is our translation matrix
T = np.float32([[1, 0, 100], [0, 1,30]])
# We use warpAffine to transform the image using the matrix, T
img_translation = cv2.warpAffine(image, T, (width+100, height+30))
plt.imshow(img_translation[...,::-1])
<matplotlib.image.AxesImage at 0x1d4fbf3d490>
# Let's take a look at T
print (T)
[[ 1. 0. 100.] [ 0. 1. 30.]]