赞
踩
opencv提供了两个变换函数,cv2.warpAffine和cv2.warpPerspective,
使用这两个函数可以实现所有类型的变换。
cv2.warpAffine 接收的参数2x3的变换矩阵;
而cv2.warpPerspective 接收的3x3的变换矩阵。
img_out = cv.warpAffine(img, mat, size)
@desc:
img_out -- 输出图像
img -- 原始图像
mat -- 2×3的变换矩阵
size -- 变换后图像尺寸
变换矩阵mat不同,变换类型不同
仿射变换矩阵mat的三种设定方式:
- 人为给定
- 使用函数 cv2.getRotationMatrix2D()
- 使用函数 cv2.getAffineTransform(src, dst)
平移矩阵 mat_shift 为人为给定。
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # ----------------------------------------------------# # 读取图像 # ----------------------------------------------------# image_original = cv.imread("LenaRGB.bmp") H, W, _ = image_original.shape cv.imshow("image_original", image_original) cv.waitKey(delay=0) # ----------------------------------------------------# # 平移变换 # mat = [[1,0,dx], # [0,1,dy]] # ----------------------------------------------------# dx = 50 dy = 100 mat_shift = np.float32([[1, 0, dx], [0, 1, dy]]) # 构造平移矩阵 image_shift = cv.warpAffine(image_original, mat_shift, (W, H)) cv.imshow("image_shift", image_shift) cv.waitKey(delay=0) cv.destroyAllWindows()
旋转矩阵 mat_rotate 使用函数 cv2.getRotationMatrix2D() 获得。
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # ----------------------------------------------------# # 读取图像 # ----------------------------------------------------# image_original = cv.imread("LenaRGB.bmp") H, W, _ = image_original.shape cv.imshow("image_original", image_original) cv.waitKey(delay=0) # ----------------------------------------------------# # 旋转变换 # 对于图像的变换,OpenCV 提供了 getRotationMatrix2D() 函数来得到旋转矩阵 # 该函数包含三个参数,第一个参数是旋转中心,第二个参数是旋转角度,第三个参数是缩放比例 # mat = cv2.getRotationMatrix2D(center, angle, scale) # ----------------------------------------------------# center = (256, 256) angle = 45 scale = 1 mat_rotate = cv.getRotationMatrix2D(center, angle, scale) image_rotate = cv.warpAffine(image_original, mat_rotate, (W, H)) cv.imshow("image_rotate", image_rotate) cv.waitKey(delay=0) cv.destroyAllWindows()
仿射矩阵 mat_affine 使用函数cv2.getAffineTransform() 获得。
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # ----------------------------------------------------# # 读取图像 # ----------------------------------------------------# image_original = cv.imread("LenaRGB.bmp") H, W, _ = image_original.shape cv.imshow("image_original", image_original) cv.waitKey(delay=0) # ----------------------------------------------------# # 仿射变换 # 变换矩阵mat可通过cv.getAffineTransfrom(points1, points2)函数获得 # 变换矩阵的获取需要至少三组变换前后对应的点坐标,设取原图上的三个点组成矩阵points1,变换后的三个点组成的矩阵points2 # mat_affine = cv.getAffineTransform(points1, points2) # image_affine = cv.warpAffine(image_original, mat_affine, (image_original.shape[1], image_original.shape[0])) # ----------------------------------------------------# points1 = np.float32([[30, 30], [100, 40], [40, 100]]) points2 = np.float32([[60, 60], [200, 80], [80, 200]]) mat_affine = cv.getAffineTransform(points1, points2) image_affine = cv.warpAffine(image_original, mat_affine, (W, H)) cv.imshow("image_affine", image_affine) cv.waitKey(delay=0) cv.destroyAllWindows()
img_out = cv.warpPerspective(img, mat, size)
@desc:
img_out -- 输出图像
img -- 原始图像
mat -- 3×3的变换矩阵
size -- 变换后图像尺寸
透视变换矩阵 mat_perspective 使用函数 cv.getPerspectiveTransform() 获得。
import cv2 as cv import numpy as np import matplotlib.pyplot as plt # ----------------------------------------------------# # 读取图像 # ----------------------------------------------------# image_original = cv.imread("LenaRGB.bmp") H, W, _ = image_original.shape cv.imshow("image_original", image_original) cv.waitKey(delay=0) # ----------------------------------------------------# # 透视变换 # 变换矩阵mat可通过cv.getPerspectiveTransform()函数获得,原理和cv.getAffineTransform()相同 # 透视变换至少需要四组变换前后对应的点坐标 # 设取原图上的四个点组成矩阵points1,变换后的四个点组成的矩阵points2 # mat_perspective = cv.getPerspectiveTransform(points1, points2) # image_perspective = cv.warpPerspective(image_original, mat_perspective, (image_original.shape[1], image_original.shape[0])) # ----------------------------------------------------# points1 = np.float32([[30, 30], [10, 40], [40, 10], [5, 15]]) points2 = np.float32([[0, 0], [400, 0], [0, 400], [400, 400]]) mat_perspective = cv.getPerspectiveTransform(points1, points2) image_perspective = cv.warpPerspective(image_original, mat_perspective, (image_original.shape[1], image_original.shape[0])) cv.imshow("image_perspective", image_perspective) cv.waitKey(delay=0) cv.destroyAllWindows()
参考单应性cv.findHomography 相关的:
OpenCV中的「透视变换 / 投影变换 / 单应性」—cv.warpPerspective、cv.findHomography
单应性矩阵可以当作透视变换中的透视变换矩阵mat 。
仿射变换可以将矩形图片映射为平行四边形,
透视变换可以将矩形图片映射为任意四边形。
参考链接:
cv2.warpAffine、cv2.warpPerspective
cv2.getPerspectiveTransform()与cv2.warpPerspective()详解
python之详细图像仿射变换讲解(图像平移、旋转、缩放、翻转)
透视变换(Perspective Transformation)★
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。