1. Scaling 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 scaling. If set to 0, the target image is obtained by multiplying the size of the source image by fx and fy; dsize takes precedence. The level is higher than fx and fy. If dsize is set, subsequent fx and fy settings are invalid;
fx and fy: When dsize is not set, fx and fy are used as the magnification factors of width and height respectively;
interpolation: interpolation method, default 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()
transpose() can realize the swapping of the x and y axis coordinates of the pixel subscript: dst(i,j)=src(j,i)
dst = cv2.transpose(src[, dst])
img_ret1 = cv2.transpose(img1)
#图像显示效果看,图像以对角线为轴,进行了翻转
3. Flip flip()
flip() function can realize horizontal flip, vertical flip and bidirectional flip of the image.
dst=cv2.flip(src, flipCode[, dst])
src: source image;
flipCode: flip mode, 0 means horizontal axis flip (up and down flip), greater than 0 means vertical axis flip (Flip left and right), if it is less than 0, do a two-way flip.
```bash
img_ret1 = cv2.flip(img1,0)#水平轴翻转(上下翻转)
img_ret2 = cv2.flip(img1,1)#垂直轴翻转(左右翻转)
img_ret3 = cv2.flip(img1,-1)#双向翻转```
#matplotlib显示 ,需进行通道调换
fig,ax = plt.subplots(2,2)
ax[0,0].set_title('原图')
ax[0,0].imshow(cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)) #matplotlib显示图像为rgb格式
ax[0,1].set_title('上下翻转')
ax[0,1].imshow(cv2.cvtColor(img_ret1,cv2.COLOR_BGR2RGB))
ax[1,0].set_title('左右翻转')
ax[1,0].imshow(cv2.cvtColor(img_ret2,cv2.COLOR_BGR2RGB))
ax[1,1].set_title('双向翻转')
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')#关闭坐标轴显示
plt.show()
4.Affine transformationwarpAffine()
Affine transformation
dst=cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
src: input image.
M: 2×3 2 rows and 3 columns transformation matrix.
dsize: The size of the output image.
dst: Optional, output image, size specified by dsize, type is the same as src.
flags: optional, interpolation method
borderMode: optional, border pixel mode
borderValue: optional, border fill value; default is 0
4.1 Translation
The image can be shifted by manually specifying the operator M=[[1,0,X],[0,1,Y]], where X represents the pixel value moving in the x direction (right) of the image, and Y Represents the pixel value of the image moving in the y direction (downward).
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]]) #右移100-下移50
img_ret1 = cv2.warpAffine(img,M,(cols,rows))
M = np.float32([[1,0,-100],[0,1,-50]]) #左移100-上移50
img_ret2 = cv2.warpAffine(img,M,(cols,rows))
M = np.float32([[1,0,-100],[0,1,50]]) #左移100-下移50
img_ret3 = cv2.warpAffine(img,M,(cols,rows))
fig,ax = plt.subplots(2,2)
ax[0,0].set_title('原图 by VX:桔子code')
ax[0,0].imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) #matplotlib显示图像为rgb格式
ax[0,1].set_title('右移100-下移50')
ax[0,1].imshow(cv2.cvtColor(img_ret1,cv2.COLOR_BGR2RGB))
ax[1,0].set_title('左移100-上移50')
ax[1,0].imshow(cv2.cvtColor(img_ret2,cv2.COLOR_BGR2RGB))
ax[1,1].set_title('左移100-下移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')#关闭坐标轴显示
plt.show()
4.2 Rotation Rotation requires the operator M constructed
by the method first . The interface form is:getRotationMatrix2D()
warpAffine()
getRotationMatrix2D()
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
#以图像中心旋转
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),90,1)#逆时针90度
img_ret1 = cv2.warpAffine(img,M,(cols,rows))
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),-90,1)#顺时针90度
img_ret2 = cv2.warpAffine(img,M,(cols,rows))
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),-180,1)#顺时针180度
img_ret3 = cv2.warpAffine(img,M,(cols,rows))
#显示图像
fig,ax = plt.subplots(2,2)
ax[0,0].set_title('VX:桔子code 原图')
ax[0,0].imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) #matplotlib显示图像为rgb格式
ax[0,1].set_title('逆时针90度')
ax[0,1].imshow(cv2.cvtColor(img_ret1,cv2.COLOR_BGR2RGB))
ax[1,0].set_title('顺时针90度')
ax[1,0].imshow(cv2.cvtColor(img_ret2,cv2.COLOR_BGR2RGB))
ax[1,1].set_title('顺时针180度')
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')#关闭坐标轴显示
plt.show()
4.3 CorrectiongetAffineTransform()
getAffineTransform()
to construct the M operator, the input parameters are two sets of coordinate points before and after transformation, each set of coordinate points contains 3 position parameters.
5. Rotate rotate()
cv2.rotate(src, rotateCode[, dst]) -> dst
, where src is the source image, rotateCode can choose 3 parameters:
cv2.ROTATE_90_CLOCKWISE Rotate 90 degrees clockwise
cv2.ROTATE_180 Rotate 180 degrees, no distinction is made between 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()
Perspective transformation requires finding 4 points to build the kernel. cv2.warpPerspective(src,M,dsize[,dst[,flags[,borderMode[,borderValue]]]])->dst
src: input image.
M: 3×3 3 rows and 3 columns transformation matrix.
dsize: The size of the output image.
dst: Optional, output image, the size is specified by dsize, the data type is the same as src.
flags: optional, interpolation method
borderMode: optional, border pixel mode
borderValue: optional, border fill value; default is 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) #该函数构建kernel
img_pers = cv2.warpPerspective(img_src,kernel,(300,350))
Affine transformation can realize translation, rotation and correction of images; rotation by affine transformation will cause image loss and black edges. The rotate() method can solve this problem; rotate() is actually implemented by encapsulating transpose and flip3 Rotation at one angle, rotate() does not support rotation at other angles.