OpenCV python 图片透视变换
原图片:[source2.jpg]
代码:
import cv2
import numpy as np
def main():
# 1.导入图片
img_src = cv2.imread("source2.jpg")
height, width = img_src.shape[:2]
print("img width:%d height:%d" % (width, height))
# 2.创建原图与目标图的对应点
src_point = np.float32([[width * 0.15, 0], [width - 1, 0],
[0, height - 1], [width * 0.85, height - 1]])
dst_point = np.float32([[0, 0], [width - 1, 0],
[0, height - 1], [width - 1, height - 1]])
# 3.获取透视变换矩阵
perspective_matrix = cv2.getPerspectiveTransform(src_point, dst_point)
# 4.执行透视变换
img_dst = cv2.warpPerspective(img_src, perspective_matrix, (width, height))
# 5.显示结果
cv2.imshow("img_src", img_src)
cv2.imshow("img_dst", img_dst)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
结果图片: