Skip to content

Instantly share code, notes, and snippets.

@ivder
Created May 13, 2019 03:49
Show Gist options
  • Save ivder/1b12e9a916b7809775d6c95c95f16ba9 to your computer and use it in GitHub Desktop.
Save ivder/1b12e9a916b7809775d6c95c95f16ba9 to your computer and use it in GitHub Desktop.
Perspective transform on video
import cv2
import numpy as np
cap = cv2.VideoCapture("test.MP4")
out = cv2.VideoWriter('out.mp4', 0x7634706d, 20.0, (960,540))
if (cap.isOpened()==False):
print("Error reading video")
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
pts1 = np.float32([[469, 517], [1317, 517], [1, 676], [1701, 676]])
pts2 = np.float32([[0, 0], [960, 0], [0, 540], [960, 540]])
matrix = cv2.getPerspectiveTransform(pts1, pts2)
result = cv2.warpPerspective(frame, matrix, (960, 540))
#cv2.imshow("Frame", frame)
cv2.imshow("Perspective transformation", result)
out.write(result)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment