In [1]:
import cv2
import numpy as np
In [2]:
img = cv2.imread('data/src/lena.jpg')
In [3]:
h, w, c = img.shape
In [4]:
src_pts = np.array([[0, 0], [0, h], [w, h], [w, 0]], dtype=np.float32)
dst_pts = np.array([[20, 50], [50, 175], [300, 205], [380, 20]], dtype=np.float32)
In [5]:
mat = cv2.getPerspectiveTransform(src_pts, dst_pts)
print(mat)
In [6]:
perspective_img = cv2.warpPerspective(img, mat, (w, h))
cv2.imwrite('data/dst/opencv_perspective_dst.jpg', perspective_img)
Out[6]:
In [7]:
mat_i = cv2.getPerspectiveTransform(dst_pts, src_pts)
print(mat_i)
In [8]:
perspective_img_i = cv2.warpPerspective(perspective_img, mat_i, (w, h))
cv2.imwrite('data/dst/opencv_perspective_dst_inverse.jpg', perspective_img_i)
Out[8]: