- 对图像进行各种几何变换,移动、旋转、仿射变换等
- 学习的函数
cv2.resize
缩放扩展,cv2.getRotationMatrix2D
旋转,cv2.getAffineTransform(pts1,pts2)
构造2x3矩阵和cv2.warpAffine
仿射变换,cv2.getPerspectiveTransform(pts1,pts2)
构造3x3矩阵和cv2.warpPerspective
透视变换
导入包
import cv2
import numpy as np
扩展缩放
- 缩放 推荐使用
cv2.INTER_AREA
,基于局部像素的重采样, - 扩展 推荐使用
v2.INTER_CUBIC
,基于4x4像素邻域的3次插值法 和v2.INTER_LINEAR
,双线性插值法(默认)。
img=cv2.imread('test.jpg')#读取图片
print('img.shape:',img.shape)
res=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)# None 本应该是输出图像的尺寸,
# 但是因为后边我们设置了缩放因子,fx 和fy分别是水平轴和垂直轴上的比例因子,都扩大2倍,
print(res.shape)
# height,width=img.shape[:2]# dsize的形状是(w,h),而opencv读取出来的图像的形状是(h,w)
# res=cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)
while(1):
cv2.imshow('img',img)
cv2.imshow('res',res)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
输出:
img.shape: