예제 #1
0
 def intrinsics(self):
     if self._intrinsics is None or self._intrinsics.resolution != self.frame_size:
         if self.projection_matrix is None:
             distortion = [[0.0, 0.0, 0.0, 0.0, 0.0]]
             self._intrinsics = Radial_Dist_Camera(
                 self.projection_matrix, distortion, self.frame_size, self.name
             )
         else:
             self._intrinsics = Dummy_Camera(self.frame_size, self.name)
     return self._intrinsics
예제 #2
0
 def intrinsics(self):
     if self._intrinsics is None or self._intrinsics.resolution != self.frame_size:
         lens_params = roypycy.get_lens_parameters(self.camera)
         c_x, c_y = lens_params["principalPoint"]
         f_x, f_y = lens_params["focalLength"]
         p_1, p_2 = lens_params["distortionTangential"]
         k_1, k_2, *k_other = lens_params["distortionRadial"]
         K = [[f_x, 0.0, c_x], [0.0, f_y, c_y], [0.0, 0.0, 1.0]]
         D = k_1, k_2, p_1, p_2, *k_other
         self._intrinsics = Radial_Dist_Camera(K, D, self.frame_size, self.name)
     return self._intrinsics
예제 #3
0
    def calculate(self):
        self.calculated = True
        self.count = 10
        img_shape = self.g_pool.capture.frame_size

        # Compute calibration
        try:
            if self.dist_mode == "Fisheye":
                calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW
                max_iter=30
                eps=1e-6
                camera_matrix = np.zeros((3, 3))
                dist_coefs = np.zeros((4, 1))
                rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(self.count)]
                tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(self.count)]
                objPoints = [x.reshape(1,-1,3) for x in self.obj_points]
                imgPoints = self.img_points
                rms, _, _, _, _ = \
                    cv2.fisheye.calibrate(
                        objPoints,
                        imgPoints,
                        img_shape,
                        camera_matrix,
                        dist_coefs,
                        rvecs,
                        tvecs,
                        calibration_flags,
                        (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, max_iter, eps)
                    )
                camera_model = Fisheye_Dist_Camera(camera_matrix, dist_coefs, img_shape, self.g_pool.capture.name)
            elif self.dist_mode == "Radial":
                rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(np.array(self.obj_points),
                                                                                   np.array(self.img_points),
                                                                                   self.g_pool.capture.frame_size, None,
                                                                                   None)
                camera_model = Radial_Dist_Camera(camera_matrix, dist_coefs, img_shape, self.g_pool.capture.name)
            else:
                raise ValueError("Unkown distortion model: {}".format(self.dist_mode))
        except ValueError as e:
            raise e
        except Exception as e:
            logger.warning("Camera calibration failed to converge!")
            logger.warning("Please try again with a better coverage of the cameras FOV!")
            return

        logger.info("Calibrated Camera, RMS:{}".format(rms))

        camera_model.save(self.g_pool.user_dir)
        self.g_pool.capture.intrinsics = camera_model



        # self.camera_intrinsics = camera_matrix.tolist(),dist_coefs.tolist(),self.g_pool.capture.frame_size TODO delete this, used anywhere?
        self.show_undistortion_switch.read_only=False
예제 #4
0
    def intrinsics(self):
        if not self.online:
            return self._intrinsics or Dummy_Camera(self.frame_size, self.name)

        if self._intrinsics is None or self._intrinsics.resolution != self.frame_size:
            lens_params = self.camera.get_lens_parameters()
            c_x, c_y = lens_params.principal_point
            f_x, f_y = lens_params.focal_length
            p_1, p_2 = lens_params.distortion_tangential
            k_1, k_2, *k_other = lens_params.distortion_radial
            K = [[f_x, 0.0, c_x], [0.0, f_y, c_y], [0.0, 0.0, 1.0]]
            D = k_1, k_2, p_1, p_2, *k_other
            self._intrinsics = Radial_Dist_Camera(K, D, self.frame_size, self.name)

            with open(os.path.join(self.g_pool.user_dir, "picoflexx_intrinsics"), "wb") as f:
                pickle.dump([
                    K, D, self.frame_size, self.name
                ], f)
        return self._intrinsics
예제 #5
0
    def intrinsics(self):
        if self._intrinsics is None or self._intrinsics.resolution != self.frame_size:
            self._intrinsics = load_intrinsics(self.g_pool.user_dir, self.name,
                                               self.frame_size)

            if type(self._intrinsics) is Dummy_Camera:
                logger.info("Was dummy camera")
                saved_fp = os.path.join(self.g_pool.user_dir,
                                        "picoflexx_intrinsics")

                if os.path.exists(saved_fp):
                    with open(saved_fp, "rb") as f:
                        K, D, self.frame_size, self.name = pickle.load(f)
                        self._intrinsics = Radial_Dist_Camera(
                            K, D, self.frame_size, self.name)
                        logger.info("Loaded from saved")
                else:
                    logger.info("No saved intrinsics?")
        return self._intrinsics