1.resize函数
函数功能:调整图片的尺寸
函数接口:
cv.resize(
src, dsize[, dst[, fx[, fy[, interpolation]]]]
)
-> dst
参数说明:
src :
input image.
dst :
output image;
it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy;
the type of dst is the same as of src.
dsize:
output image size; if it equals zero (None in Python), it is computed as:
dsize = Size(round(fx*src.cols), round(fy*src.rows))
Either dsize or both fx and fy must be non-zero.
fx:
scale factor along the horizontal axis; when it equals 0, it is computed as
(double)dsize.width/src.cols
fy:
scale factor along the vertical axis; when it equals 0, it is computed as
(double)dsize.height/src.rows
(1)缩放到原来的1/2,输出尺寸格式为(宽,高)
代码:
# 彩色照片
img_bgr = cv2.imread(r'C:\Users\Nobody\Desktop\img1.png', 1)
cv2.imshow('img_bgr',img_bgr)
x, y = img_bgr.shape[0:2]
print(f'img_bgr shape:{img_bgr.shape}')
# 缩小2倍
img_1_2=img_bgr.copy()
img_1_2=cv2.resize(img_bgr,(int(y/2),int(x/2)))
cv2.imshow('img_1_2',img_1_2)
print(f'img_1_2 shape:{img_1_2.shape}')
结果输出:
img_bgr shape:(768, 1366, 3)
img_1_2 shape:(384, 683, 3)
(2)放大到原来的2倍,输出尺寸格式为(fx,fy)
代码:
# 彩色照片
img_bgr = cv2.imread(r'C:\Users\Nobody\Desktop\img2.png', 1)
cv2.imshow('img_bgr',img_bgr)
print(f'img_bgr shape:{img_bgr.shape}')
# 放大2倍
img_2=img_bgr.copy()
img_2=cv2.resize(img_bgr,None,fx=2,fy=2)
cv2.imshow('img_2',img_2)
print(f'img_2 shape:{img_2.shape}')
结果输出:
2.flip函数
函数功能:沿水平方向/垂直方向/水平和垂直方向旋转
函数接口:
cv.flip(
src,
flipCode[, dst]
)
-> dst
参数说明:
src:
input array.
dst:
output array of the same size and type as src.
flipCode:
a flag to specify how to flip the array;
0 means flipping around the x-axis.
positive value (for example, 1) means flipping around y-axis.
Negative value (for example, -1) means flipping around both axes.
测试代码:
# 彩色照片
img_bgr = cv2.imread(r'C:\Users\Nobody\Desktop\img3.png', 1)
cv2.imshow('img_bgr',img_bgr)
# 沿X轴旋转
img_x=img_bgr.copy()
img_x=cv2.flip(img_bgr,0)
cv2.imshow('img_x', img_x)
# 沿Y轴旋转
img_y = img_bgr.copy()
img_y = cv2.flip(img_bgr, 1)
cv2.imshow('img_y', img_y)
# 同时沿X轴和Y轴旋转
img_xy = img_bgr.copy()
img_xy = cv2.flip(img_bgr, -1)
cv2.imshow('img_xy', img_xy)
结果输出: