Skip to content

Instantly share code, notes, and snippets.

@arhanjain
Created May 30, 2022 23:49
Show Gist options
  • Save arhanjain/2fbebda639388eaddaf5746af50edc3c to your computer and use it in GitHub Desktop.
Save arhanjain/2fbebda639388eaddaf5746af50edc3c to your computer and use it in GitHub Desktop.
def warp_frame(frame, playfield_corners):
"""
Warps the frame to the playfield.
Parameters
----------
frame : np.ndarray
The frame to transform.
playfield_corners : list
The coordinates of the corners of the playfield.
Returns
-------
np.ndarray
The warped frame.
"""
playfield_corners_sorted = PinballUtils.sort_coordinates(playfield_corners)
display_corners_sorted = PinballUtils.sort_coordinates([
[0, 0],
[frame.shape[1], 0],
[frame.shape[1], frame.shape[0]],
[0, frame.shape[0]]
])
# Calculate transformation matrix
transformation_matrix = cv2.getPerspectiveTransform(playfield_corners_sorted, display_corners_sorted)
return cv2.warpPerspective(frame, transformation_matrix, (frame.shape[1], frame.shape[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment