Opencv geometric space transformation: resize(), transpose(), flip(), warpAffine(), rotate(), warpPerspective()

1. Zoom resize()
resize() can reduce or enlarge the image size
dst=cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
src: source image;
dsize: The size of the target image after zooming. If it is set to 0, the target image will be obtained by multiplying the size of the source image by fx and fy; dsize has a higher priority than fx and fy. If dsize is set, the following settings of fx and fy will be invalid ;
fx and fy: When dsize is not set, use fx and fy as the magnification of width and height respectively;
interpolation: interpolation method, the default is to use bilinear interpolation cv2.INTER_LINEAR;

img_ret1 = cv2.resize(img1,(800,800))
img_ret2 = cv2.resize(img1,None,fx=0.5,fy=0.3)

2. transpose()
transpose() can realize the exchange of x and y axis coordinates of pixel subscript: dst(i,j)=src(j,i)
dst = cv2.transpose(src[, dst])

img_ret1 = cv2.transpose(img1)
#Image display effect, the image is flipped with the diagonal as the axis

3. Flip flip()
The flip() function can realize the horizontal flip, vertical flip and two-way flip of the image.
dst=cv2. flip(src, flipCode[, dst])
src: source image;
flipCode: Flip mode, 0 means flip on the horizontal axis (flip up and down), greater than 0 means flip on the vertical axis (flip left and right), and less than 0 means flip in both directions.

```bash
img_ret1 = cv2.flip(img1,0)#Horizontal axis flip (flip up and down)
img_ret2 = cv2.flip(img1,1)#Vertical axis flip (left and right flip)
img_ret3 = cv2.flip(img1,-1)#Bidirectional flip```
#matplotlib shows that the channel needs to be swapped
fig,ax = plt.subplots(2,2)
ax[0,0].set_title('Original image')
ax[0,0].imshow(cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)) #matplotlib display image in rgb format
ax[0,1].set_title('flip up and down')
ax[0,1].imshow(cv2.cvtColor(img_ret1,cv2.COLOR_BGR2RGB))
ax[1,0].set_title('Flip left and right')
ax[1,0].imshow(cv2.cvtColor(img_ret2,cv2.COLOR_BGR2RGB))
ax[1,1].set_title('two-way flip')
ax[1,1].imshow(cv2.cvtColor(img_ret3,cv2.COLOR_BGR2RGB))
ax[0,0].axis('off');ax[0,1].axis('off');ax[1,0].axis('off');ax[ 1,1].axis('off')#Close the axis display
plt. show()

4. Affine transformation warpAffine()

affine transformation
dst=cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
src: input image.
M: 2×3 transformation matrix with 2 rows and 3 columns.
dsize: The size of the output image.
dst: optional, the output image, the size is specified by dsize, and the type is the same as src.
flags: optional, interpolation method
borderMode: optional, border pixel mode
borderValue: optional, border padding value; default is 0

4.1 Panning
The shift of the image can be realized by manually specifying the operator M=[[1,0,X],[0,1,Y]], where X represents the pixel value moving to the image x direction (to the right), and Y represents the image The pixel value to move in the y direction (down).

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

plt.rc('font', family='Youyuan', size='9')
plt.rc('axes',unicode_minus='False')

img = cv2.imread('..\messi5.jpg')
rows,cols,_ = img.shape

M = np.float32([[1,0,100],[0,1,50]]) #Move right 100-down 50
img_ret1 = cv2.warpAffine(img,M,(cols,rows))
M = np.float32([[1,0,-100],[0,1,-50]]) #move left 100-up 50
img_ret2 = cv2.warpAffine(img,M,(cols,rows))
M = np.float32([[1,0,-100],[0,1,50]]) #move left 100-move down 50
img_ret3 = cv2.warpAffine(img,M,(cols,rows))

fig,ax = plt.subplots(2,2)
ax[0,0].set_title('Original image by VX: orange code')
ax[0,0].imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) #matplotlib display image in rgb format
ax[0,1].set_title('Move right 100-move down 50')
ax[0,1].imshow(cv2.cvtColor(img_ret1,cv2.COLOR_BGR2RGB))
ax[1,0].set_title('move left 100-move up 50')
ax[1,0].imshow(cv2.cvtColor(img_ret2,cv2.COLOR_BGR2RGB))
ax[1,1].set_title('move left 100-move down 50')
ax[1,1].imshow(cv2.cvtColor(img_ret3,cv2.COLOR_BGR2RGB))
#ax[0,0].axis('off');ax[0,1].axis('off');ax[1,0].axis('off');ax [1,1].axis('off')#Close the axis display
plt. show()

4.2 Rotation
Rotation needs to use getRotationMatrix2D() method to construct operator M of warpAffine(), getRotationMatrix2D() interface form:

retval=cv2.getRotationMatrix2D(center, angle, scale)

center: Rotation center position
angle: rotation angle
scale: scaling ratio, 1 when not scaling

import matplotlib.pyplot as plt
import numpy as np
import cv2
plt.rc('font', family='Youyuan', size='9')
plt.rc('axes',unicode_minus='False')

img = cv2.imread('mess.jpg')
rows,cols,_ = img.shape

#rotate around image center
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),90,1)#counterclockwise 90 degrees
img_ret1 = cv2.warpAffine(img,M,(cols,rows))
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),-90,1)# clockwise 90 degrees
img_ret2 = cv2.warpAffine(img,M,(cols,rows))
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),-180,1)# clockwise 180 degrees
img_ret3 = cv2.warpAffine(img,M,(cols,rows))

#display image
fig,ax = plt.subplots(2,2)
ax[0,0].set_title('VX: orange code original image')
ax[0,0].imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) #matplotlib display image in rgb format
ax[0,1].set_title('counterclockwise 90 degrees')
ax[0,1].imshow(cv2.cvtColor(img_ret1,cv2.COLOR_BGR2RGB))
ax[1,0].set_title('clockwise 90 degrees')
ax[1,0].imshow(cv2.cvtColor(img_ret2,cv2.COLOR_BGR2RGB))
ax[1,1].set_title('clockwise 180 degrees')
ax[1,1].imshow(cv2.cvtColor(img_ret3,cv2.COLOR_BGR2RGB))
ax[0,0].axis('off');ax[0,1].axis('off');ax[1,0].axis('off');ax[ 1,1].axis('off')#Close the axis display
plt. show()

4.3 CorrectiongetAffineTransform()
getAffineTransform() constructs the M operator, and the input parameters are two sets of coordinate points before and after transformation, and each set of coordinate points contains three position parameters.

5. Rotate rotate()

cv2.rotate(src, rotateCode[, dst]) -> dst, where src is the source image, and rotateCode can choose 3 parameters:
cv2.ROTATE_90_CLOCKWISE Rotate 90 degrees clockwise
cv2.ROTATE_180 rotates 180 degrees, does not distinguish clockwise or counterclockwise, the effect is the same
cv2.ROTATE_90_COUNTERCLOCKWISE Rotate 90 degrees counterclockwise, which is equivalent to 270 degrees clockwise

img_ret1 = cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)
img_ret2 = cv2.rotate(img,cv2.ROTATE_180)
img_ret3 = cv2.rotate(img,cv2.ROTATE_90_COUNTERCLOCKWISE)

6. Perspective transformation warpPerspective()

The perspective transformation needs to find 4 points to build the kernel. cv2.warpPerspective(src,M,dsize[,dst[,flags[,borderMode[,borderValue]]]])->dst
src: input image.
M: 3×3 transformation matrix with 3 rows and 3 columns.
dsize: The size of the output image.
dst: optional, the output image, the size specified by dsize, the data type is the same as src.
flags: optional, interpolation method
borderMode: optional, border pixel mode
borderValue: Optional, border padding value; defaults to 0.

pts1 = np.float32([[192,40],[610,122],[216,363],[465,415]])
pts2 = np.float32([[0,0],[300,0],[0,350],[300,350]])
kernel = cv2.getPerspectiveTransform(pts1,pts2) #This function builds the kernel
img_pers = cv2.warpPerspective(img_src,kernel,(300,350))

Affine transformation can realize the translation, rotation and correction of the image; the rotation of the affine transformation will cause the image to be lost and black edges will appear, and the rotate() method can solve this problem; rotate() actually encapsulates transpose and flip To achieve three angles of rotation, rotate() does not support rotation at other angles.