1.图像的缩放:就是按照所给的图像将图像方法缩小
#缩放有两种:一种是绝对尺寸,一种是相对尺寸 import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像 img1 = cv.imread(‘image1.jpg‘,1) #获取长宽 rows,cols = cv.shape[:2] #第一种获取缩放矩阵 res = cv.resize(img1,(2*rows,2*cols)) plt.imshow(res) plt.show() #第一种获取缩放矩阵 res1 = cv.resize(img1,None,fx = 0.3,fy = 0.7)#None默认数据类型 plt.imshow(res1[:,:,::-1]) plt.show()
2.图像平移:在原有的图像上面进行平移
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像 img1 = cv.imread(‘image1.jpg‘,1) #获取长宽 rows,cols = cv.shape[:2] M = np.float([[1,0,50],[0,1,100]])#执行类型为浮点型 #获取平移矩阵 res = cv.warpAffine(img1,M,(rows,cols)) plt.imshow(res[:,:,::-1])#转为BGR通道 plt.show()
3.图像旋转:是将图像围绕中心点进行一定角度地旋转
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像 img1 = cv.imread(‘image1.jpg‘,1) #获取长宽 rows,cols = cv.shape[:2] #获取旋转矩阵 M = getRotationMattix2D((rows/2,cols/2),45,1)#以中心旋转45度,缩放一倍 res = cv.warpAffine(img1,M,(rows,cols)) plt.imshow(res[:,:,::-1]) plt.show()
4.图像的仿射变换:在图像的三个点对图像进行变换
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像 img1 = cv.imread(‘image1.jpg‘,1) #获取长宽 rows,cols = cv.shape[:2] #获取两个像素点 pst1 = np.float32([],[],[]) pst2 = np.float32([],[],[]) #获取旋转矩阵 M = getAffineTransform(pst1,pst2)#两个像素点转换 res = cv.warpAffine(img1,M,(rows,cols)) plt.imshow(res[:,:,::-1]) plt.show()
5.图像的透射变换,与仿射变换相似,但是这是以四个点为准,其中三个不能重复
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像 img1 = cv.imread(‘image1.jpg‘,1) #获取长宽 rows,cols = cv.shape[:2] #获取两个像素点 pst1 = np.float32([],[],[],[]) pst2 = np.float32([],[],[],[]) #获取旋转矩阵 M = getPerspectiveTransform(pst1,pst2)#两个像素点转换 res = cv.warpPerspective(img1,M,(rows,cols)) plt.imshow(res[:,:,::-1]) plt.show()
6.图像金字塔:与图像的缩放相似,一种上采样也就是放大,另外一种下采样也就是缩小
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像 img1 = cv.imread(‘image1.jpg‘,1) img_up = pycUp(img1) plt.imshow(img_up[:,:,::-1]) plt.show() img_Down = pyrDown(img1) plt.imshow(img_down[:,:,::-1]) plt.show()