vertopal.com_CVPR3
vertopal.com_CVPR3
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('nature.jpg')
rows,cols,ch = img.shape
pt1 = np.float32([[50,65],[370,52],[30,387],[390,390]])
pt2 = np.float32([[0,0],[310,0],[0,310],[310,310]])
matrix_aff = cv2.getPerspectiveTransform(pt1,pt2)
dst = cv2.warpPerspective(img,matrix_aff,(cols,rows))
plt.subplot(121),plt.imshow(img),
plt.title('Input')
plt.subplot(122),plt.imshow(dst),
plt.title('Output')
plt.show()
#FUNCTION
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
def perspective_transform(f, x1, y1, x2, y2, x3, y3, x4, y4, width,
height):
src_pts = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]],
dtype='float32')
dst_pts = np.array([[0, 0], [width - 1, 0], [width - 1, height -
1], [0, height - 1]], dtype='float32')
H, _ = cv2.findHomography(src_pts, dst_pts)
result = np.zeros((height, width, 3), dtype=np.uint8)
cv2.warpPerspective(f, H, (width, height), dst=result)
return result
#SAMPLE USE
image = cv2.imread('nature.jpg')
transformed_image = perspective_transform(image, 50, 50, 200, 50, 150,
150, 50, 150, 300, 400)
cv2.imwrite('transformed_nature.jpg', transformed_image)
cv2_imshow(transformed_image)