一、图像的几何变换,缩放,平移,旋转
#图像的几何变换,缩放,平移,旋转、仿射、透视
import cv2 as cv
import numpy as np
img1 = cv.imread("E:\\python opencv\\demo2\\a.jpg")
cv.imshow("image", img1)
#缩放 cv.resize()
res = cv.resize(img1, None, fx=1, fy=2, interpolation=cv.INTER_CUBIC)
res1 = cv.resize(img1, None, fx=0.5, fy=1, interpolation=cv.INTER_AREA)
res2 = cv.resize(img1, None, fx=0.5, fy=0.5, interpolation=cv.INTER_LINEAR)
#或者
height, width = img1.shape[:2]
res3 = cv.resize(img1, (2*width, 2*height), interpolation=cv.INTER_LINEAR)
cv.imshow("res", res)
cv.imshow("res1", res1)
cv.imshow("res2", res2)
cv.imshow("res3", res3)
#平移 np.float32 cv.waroAffine
rows, cols = img1.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv.warpAffine(img1, M, (cols, rows))
cv.imshow('dst', dst)
#旋转 (),旋转角度,比例
M1 = cv.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 90, 0.5)
dst1 = cv.warpAffine(img1, M1, (cols, rows))
cv.imshow('dst1', dst1)
cv.waitKey(0)
cv.destroyAllWindows()
二、仿射和透视
#仿射和透视,在输入图像和输出图像中找三个对应的点
#创建一个2*3转换矩阵,然后旋转
#透视就是找4个对应的点,生成3*3转换矩阵,然后旋转
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img=cv.imread("E:\\python opencv\\demo2\\a.jpg")
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
plt.subplot(121), plt.imshow(img), plt.title('Input')
plt.subplot(122), plt.imshow(dst), plt.title('Output')
plt.show()
cv.waitKey()
cv.destroyAllWindows()