import cv2
####图片缩放####
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
#1.放大和缩小 2.等比例缩放和非等比例
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
#四种方法:最近邻域插值,双线性插值,像素关系重采样,立方插值
dst = cv2.resize(img,(dstWidth,dstHeight))
cv2.imshow('image',dst)
cv2.waitKey(0)
#最近邻域插值法,双线性插值法---原理
#最近邻域插值法
#原图像src 10*20 目标图像dst 5*10
#newX = x*(原图像行/目标图像行)
#newY = y*(原图像列/目标图像列)
#双线性插值法
#A1=0.2*上+0.8*下 A2
#B1=0.3*左+0.7*右 B2
#方法一.最终点=A1*0.3+A2*0.7方法二.最终点=B1*0.2+B2*0.8
![](https://i-blog.csdnimg.cn/blog_migrate/27c55363bf6c044df10e1361312c1a6e.png)
####最近邻域插值法代码实现####
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
dstImage = np.zeros((dstHeight,dstWidth,3),np.uint8)#0-255
for i in range(0,dstHeight):#行
for j in range(0,dstWidth):#列
iNew = int(i*(height*1.0/dstHeight))
jNew = int(j*(width*1.0/dstWidth))
dstImage[i,j] = img[iNew,jNew]
cv2.imshow('image',dstImage)
cv2.waitKey(0)
#图片移位
matShift = np.float32([[1,0,100],[0,1,200]])
#矩阵为[[0,0.5,0],[0.5,0,0]]可完成缩放
dst = cv2.warpAffine(img,matShift,(height,width))#para3.最终图片的宽高信息
# [[A1,A2,B1],[A3,A4,B2]]
# [[A1,A2],[A3,A4]] [[B1],[B2]]
# newX = A1*x+A2*y+B1
# newY = A3*x+A4*y+B2
#图像仿射变换
import cv2
import numpy as np
img =cv2.imread('image0.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
#三点确定一个平面
#原始图片3个点映射到目标图片三个点上
#(左上角,左下角,右上角)
matSrc = np.float32([[0,0],[0,height-1],[width-1,0]]) #原图片三个点
matDst = np.float32([[50,50],[300,height-200],[width-300,100]])#目标图片三个点
matAffine = cv2.getAffineTransform(matSrc,matDst) #仿射变换矩阵
dst = cv2.warpAffine(img,matAffine,(width,height)) #仿射变换方法
cv2.imshow('dst',dst)
cv2.waitKey(0)
![](https://i-blog.csdnimg.cn/blog_migrate/8c98a2f7d4c10d84c5746fd1ce6fbf08.png)
#图像旋转
img =cv2.imread('image0.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
#定义旋转矩阵
matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5)#1.旋转中心点2.旋转角度3.缩放系数
dst = cv2.warpAffine(img,matRotate,(height,width))
cv2.imshow('dst',dst)
cv2.waitKey(0)
![](https://i-blog.csdnimg.cn/blog_migrate/3da0539c38b3200d016d36effac0d8cc.png)