一.图像仿射变换
import cv2
import numpy as np
import matplotlib.pyplot as plt
src = cv2.imread('C:/Users/31035/Desktop/yifei/01.jpg')
rows, cols = src.shape[:2]
pos1 = np.float32([[50,50], [200,50], [50,200]])
pos2 = np.float32([[10,100], [200,50], [100,250]])
M = cv2.getAffineTransform(pos1, pos2)
result = cv2.warpAffine(src, M, (cols, rows))
cv2.imshow("original", src)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d71875dd262227a42a226e959a09109d.png)
二.图像透视变换
rows, cols = src.shape[:2]
pos1 = np.float32([[114, 82], [287, 156], [8, 322], [216, 333]])
pos2 = np.float32([[0, 0], [188, 0], [0, 188], [188, 188]])
M = cv2.getPerspectiveTransform(pos1, pos2)
result = cv2.warpPerspective(src, M,(190,272))
cv2.imshow("original", src)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
三.基于图像透视变换的图像校正
四.图像几何变换总结
img = cv2.imread('C:/Users/31035/Desktop/yifei/01.jpg')
image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
M = np.float32([[1, 0, 80], [0, 1, 30]])
rows, cols = image.shape[:2]
img1 = cv2.warpAffine(image, M, (cols, rows))
img2 = cv2.resize(image, (200,100))
img3 = cv2.resize(image, None, fx=1.5, fy=1.5)
rows, cols, channel = image.shape
M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)
img4 = cv2.warpAffine(image, M, (cols, rows))
img5 = cv2.flip(image, 0)
img6 = cv2.flip(image, 1)
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
img7 = cv2.warpAffine(image, M, (rows,cols))
pts1 = np.float32([[56,65],[238,52],[28,237],[239,240]])
pts2 = np.float32([[0,0],[200,0],[0,200],[200,200]])
M = cv2.getPerspectiveTransform(pts1,pts2)
img8 = cv2.warpPerspective(image,M,(200,200))
titles = [ 'source', 'shift', 'reduction', 'enlarge', 'rotation', 'flipX', 'flipY', 'affine', 'transmission']
images = [image, img1, img2, img3, img4, img5, img6, img7, img8]
for i in range(9):
plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9b9c2b1b9915154349ec8f546ace178a.png)