图像的几何变换
demo1
import cv2
img = cv2.imread("./cat.jpg")
dst1 = cv2.resize(img, (100, 100))
dst2 = cv2.resize(img, (400, 400))
cv2.imwrite("./cat01.png", dst1)
cv2.waitKey()
cv2.destroyAllWindows()
demo2
import cv2
img = cv2.imread("./cat.jpg")
dst3 = cv2.resize(img, None, fx=1/3, fy=1/2)
dst4 = cv2.resize(img, None, fx=2, fy=2)
cv2.imshow("img", img)
cv2.imshow("dst3", dst3)
cv2.imshow("dst4", dst4)
cv2.waitKey()
cv2.destroyAllWindows()
demo3
import cv2
img = cv2.imread("./cat01.png")
dst1 = cv2.flip(img, 0)
dst2 = cv2.flip(img, 1)
dst3 = cv2.flip(img, -1)
cv2.imshow("dst1", dst1)
cv2.imshow("dst2", dst2)
cv2.imshow("dst3", dst3)
cv2.waitKey()
cv2.destroyAllWindows()
demo4
import cv2
import numpy as np
img = cv2.imread("./cat01.png")
rows = len(img)
cols = len(img[0])
M = np.float32([[1, 0, 0], [0, 1, -50]])
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
demo5
import cv2
img = cv2.imread("./cat01.png")
# 读取像素行数
rows = len(img)
# 读取像素列数
cols = len(img[0])
center = (rows/2, cols/2)
M = cv2.getRotationMatrix2D(center, 30, 0.8)
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
demo6
import cv2
import numpy as np
img = cv2.imread("./cat01.png")
rows = len(img)
cols = len(img[0])
p1 = np.zeros((3, 2), np.float32)
p1[0] = [0, 0]
p1[1] = [cols - 1, 0]
p1[2] = [0, rows - 1]
p2 = np.zeros((3, 2), np.float32)
p2[0] = [50, 0]
p2[1] = [cols - 1, 0]
p2[2] = [0, rows - 1]
M = cv2.getAffineTransform(p1, p2)
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
demo7
import cv2
import numpy as np
img = cv2.imread("./cat01.png")
rows = len(img)
cols = len(img[0])
p1 = np.zeros((4, 2), np.float32)
p1[0] = [0, 0]
p1[1] = [cols - 1, 0]
p1[2] = [0, rows - 1]
p1[3] = [cols - 1, rows - 1]
p2 = np.zeros((4, 2), np.float32)
M = cv2.getPerspectiveTransform(p1, p2)
dst = cv2.warpPerspective(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()