from pathlib import Path import cv2 as cv import numpy as np input_path = Path("input/scene.png") output_path = Path("output/scene-perspective.png") image = cv.imread(str(input_path)) if image is None: raise SystemExit(f"Could not read {input_path}") source_points = np.float32([ [125, 82], [600, 62], [670, 398], [35, 360], ]) output_size = (640, 360) target_points = np.float32([ [0, 0], [output_size[0] - 1, 0], [output_size[0] - 1, output_size[1] - 1], [0, output_size[1] - 1], ]) matrix = cv.getPerspectiveTransform(source_points, target_points) warped = cv.warpPerspective( image, matrix, output_size, flags=cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT, borderValue=(245, 245, 245), ) output_path.parent.mkdir(parents=True, exist_ok=True) if not cv.imwrite(str(output_path), warped): raise SystemExit(f"Could not write {output_path}") print(f"source shape: {image.shape}") print("matrix:") print(np.array2string(matrix, precision=6, suppress_small=True)) print(f"output size: {output_size[0]}x{output_size[1]}") print(f"output shape: {warped.shape}") print(f"wrote: {output_path}")