Skip to content

Instantly share code, notes, and snippets.

@baterflyrity
baterflyrity / direct_inverse_cv2_transforms.py
Created February 20, 2021 17:54
Example of direct and inverse transformations with cv2 perspective. See https://docs.opencv.org/3.1.0/d1/de0/tutorial_py_feature_homography.html
def transform_coordinates(x: np.float32, y: np.float32, matrix: np.ndarray) -> np.ndarray:
"""
Calculate coordinates transformation.
:param x: x coordinate.
:param y: y coordinate.
:param matrix: transformation matrix. Can be obtained from cv2.findHomography().
:return: transformed coordinates.
"""
return cv2.perspectiveTransform(np.array([[[x, y]]], dtype=np.float32), matrix).reshape(2)