|
| 1 | +import os |
| 2 | +import numpy as np |
| 3 | +import cv2 |
| 4 | + |
| 5 | +from . import param_settings as settings |
| 6 | + |
| 7 | + |
| 8 | +class FisheyeCameraModel(object): |
| 9 | + |
| 10 | + """ |
| 11 | + Fisheye camera model, for undistorting, projecting and flipping camera frames. |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, camera_param_file, camera_name): |
| 15 | + if not os.path.isfile(camera_param_file): |
| 16 | + raise ValueError("Cannot find camera param file") |
| 17 | + |
| 18 | + if not camera_name in settings.camera_names: |
| 19 | + raise ValueError("Unknown camera name: {}".format(camera_name)) |
| 20 | + |
| 21 | + self.camera_file = camera_param_file |
| 22 | + self.camera_name = camera_name |
| 23 | + self.scale_xy = (1.0, 1.0) |
| 24 | + self.shift_xy = (0, 0) |
| 25 | + self.undistort_maps = None |
| 26 | + self.project_matrix = None |
| 27 | + self.project_shape = settings.project_shapes[self.camera_name] |
| 28 | + self.load_camera_params() |
| 29 | + |
| 30 | + def load_camera_params(self): |
| 31 | + fs = cv2.FileStorage(self.camera_file, cv2.FILE_STORAGE_READ) |
| 32 | + self.camera_matrix = fs.getNode("camera_matrix").mat() |
| 33 | + self.dist_coeffs = fs.getNode("dist_coeffs").mat() |
| 34 | + self.resolution = fs.getNode("resolution").mat() |
| 35 | + |
| 36 | + scale_xy = fs.getNode("scale_xy").mat() |
| 37 | + if scale_xy is not None: |
| 38 | + self.scale_xy = scale_xy |
| 39 | + |
| 40 | + shift_xy = fs.getNode("shift_xy").mat() |
| 41 | + if shift_xy is not None: |
| 42 | + self.shift_xy = shift_xy |
| 43 | + |
| 44 | + project_matrix = fs.getNode("project_matrix").mat() |
| 45 | + if project_matrix is not None: |
| 46 | + self.project_matrix = project_matrix |
| 47 | + |
| 48 | + fs.release() |
| 49 | + self.update_undistort_maps() |
| 50 | + |
| 51 | + def update_undistort_maps(self): |
| 52 | + new_matrix = self.camera_matrix.copy() |
| 53 | + new_matrix[0, 0] *= self.scale_xy[0] |
| 54 | + new_matrix[1, 1] *= self.scale_xy[1] |
| 55 | + new_matrix[0, 2] += self.shift_xy[0] |
| 56 | + new_matrix[1, 2] += self.shift_xy[1] |
| 57 | + width, height = self.resolution |
| 58 | + |
| 59 | + self.undistort_maps = cv2.fisheye.initUndistortRectifyMap( |
| 60 | + self.camera_matrix, |
| 61 | + self.dist_coeffs, |
| 62 | + np.eye(3), |
| 63 | + new_matrix, |
| 64 | + (width, height), |
| 65 | + cv2.CV_16SC2 |
| 66 | + ) |
| 67 | + return self |
| 68 | + |
| 69 | + def set_scale_and_shift(self, scale_xy=(1.0, 1.0), shift_xy=(0, 0)): |
| 70 | + self.scale_xy = scale_xy |
| 71 | + self.shift_xy = shift_xy |
| 72 | + return self |
| 73 | + |
| 74 | + def undistort(self, image): |
| 75 | + result = cv2.remap( |
| 76 | + image, |
| 77 | + *self.undistort_maps, |
| 78 | + interpolation=cv2.INTER_LINEAR, |
| 79 | + borderMode=cv2.BORDER_CONSTANT |
| 80 | + ) |
| 81 | + return result |
| 82 | + |
| 83 | + def project(self, image): |
| 84 | + result = cv2.warpPerspective( |
| 85 | + image, |
| 86 | + self.project_matrix, |
| 87 | + self.project_shape |
| 88 | + ) |
| 89 | + return result |
| 90 | + |
| 91 | + def flip(self, image): |
| 92 | + if self.camera_name == "front": |
| 93 | + return image.copy() |
| 94 | + |
| 95 | + elif self.camera_name == "back": |
| 96 | + return image.copy()[::-1, ::-1, :] |
| 97 | + |
| 98 | + elif self.camera_name == "left": |
| 99 | + return cv2.transpose(image)[::-1] |
| 100 | + |
| 101 | + else: |
| 102 | + return np.flip(cv2.transpose(image), 1) |
0 commit comments