from PIL import Image import cv2 import numpy as np import constants def cv2_to_pil(cv_image: np.ndarray) -> Image.Image: """ cv2.MatLike (BGR/RGB/Grayscale) → PIL.Image """ if len(cv_image.shape) == 3 and cv_image.shape[2] == 3: cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB) return Image.fromarray(cv_image) def image_to_numpy(self, image: Image.Image) -> np.ndarray: """Конвертирует PIL Image в numpy array для OpenCV""" return np.array(image) def get_normals(H: np.ndarray, R: np.ndarray, T: np.ndarray) -> np.ndarray: n = cv2.decomposeHomographyMat(H, constants.K, R, T) return n def estimate_transformation_matrix(src_pts: np.ndarray, dst_pts: np.ndarray) -> tuple[np.ndarray, float | None]: """Оценивает матрицу трансформации на основе сопоставленных точек""" # Используем RANSAC для оценки матрицы гомографии H, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 3.0, maxIters=1000) return H def calc_camera_matrix(w: float, h: float): f = constants._K_FOCUS_DISTANCE return np.array([ [f, 0, w / 2], [0, f, h / 2], [0, 0, 1] ])