def reprojection(self, folder, number=None): """ Dessiner la reprojection """ clean_folders([folder]) images_left = np.sort(glob.glob(self.single_path + 'left*.jpg')) images_right = np.sort(glob.glob(self.single_path + 'right*.jpg')) assert (len(images_right) != 0 or len(images_left) != 0), "Images pas trouvées. Vérifier le path" if number is not None: images_left = images_left[:number] images_right = images_right[:number] index = 0 for i in range(len(images_left)): ret_l, corners_l, gray_l = find_corners(images_left[i], self.patternSize) if ret_l == True: draw_reprojection(cv.imread(images_left[i]), self.objpoints_l[index], self.imgpoints_l[index], self.M1, self.d1, self.patternSize, self.squaresize, folder, "left_{}".format(i)) index += 1 jndex = 0 for j in range(len(images_right)): ret_r, corners_r, gray_r = find_corners(images_right[j], self.patternSize) if ret_r == True: draw_reprojection(cv.imread(images_right[j]), self.objpoints_r[jndex], self.imgpoints_r[jndex], self.M2, self.d2, self.patternSize, self.squaresize, folder, "right_{}".format(j)) jndex += 1
def calibrateSingle(self, single_path, single_detected_path, fisheye=False): """ ||Public method|| Calibration individuelle de 2 caméras Args: single_path (str): "path_to_single_images/" single_detected_path (str): "path_to_single_images_detected/" fisheye (Bool): True pour caméra fisheye """ self.single_path = single_path self.single_detected_path = single_detected_path clean_folders([single_detected_path]) self.objpoints_l, self.imgpoints_l, self.imageSize1 = self.__read_single( 'left') self.objpoints_r, self.imgpoints_r, self.imageSize2 = self.__read_single( 'right') if fisheye == True: self.err1, self.M1, self.d1, self.r1, self.t1, stdDeviationsIntrinsics1, stdDeviationsExtrinsics1, self.perViewErrors1 = cv.calibrateCameraExtended( self.objpoints_l, self.imgpoints_l, self.imageSize1, None, None, flags=self.fisheye_flags) self.err2, self.M2, self.d2, self.r2, self.t2, stdDeviationsIntrinsics2, stdDeviationsExtrinsics2, self.perViewErrors2 = cv.calibrateCameraExtended( self.objpoints_r, self.imgpoints_r, self.imageSize2, None, None, flags=self.fisheye_flags) else: self.err1, self.M1, self.d1, self.r1, self.t1, stdDeviationsIntrinsics1, stdDeviationsExtrinsics1, self.perViewErrors1 = cv.calibrateCameraExtended( self.objpoints_l, self.imgpoints_l, self.imageSize1, None, None, flags=self.not_fisheye_flags) self.err2, self.M2, self.d2, self.r2, self.t2, stdDeviationsIntrinsics2, stdDeviationsExtrinsics2, self.perViewErrors2 = cv.calibrateCameraExtended( self.objpoints_r, self.imgpoints_r, self.imageSize2, None, None, flags=self.not_fisheye_flags) # Print erreur de reprojection print('Erreur de reprojection RMS calibration individuelle') print(self.err1, self.err2)
def calibrateExtrinsics(self, stereo_path, stereo_detected_path, cibles, cibles_l, cibles_r, fisheye=False, calib_2_sets=False): """ ||Public method|| Calibration stéréo de deux caméra AVEC DES CIBLES Args: stereo_path (str): "path_to_stereo_images/" fisheye (Bool): True pour caméra fisheye calib_2_sets (Bool): True: pour utiliser des sets de photos différents (un pour la calibration individuelle des caméra et un pour la calibration stéréo) False: pour utiliser un seul set de photo pour la calibration individuelle et la calibration stéréo single_path: "path_to_single_images/" """ self.stereo_path = stereo_path self.stereo_detected_path = stereo_detected_path clean_folders([stereo_detected_path]) flags = 0 if fisheye: flags += self.fisheye_flags else: flags += self.not_fisheye_flags if self.R is not None: flags += cv.CALIB_USE_EXTRINSIC_GUESS if calib_2_sets: flags += cv.CALIB_FIX_INTRINSIC else: flags += cv.CALIB_USE_INTRINSIC_GUESS print('calibration stéréo') # calibration stereo objpoints, imgpoints_l, imgpoints_r = self.__read_stereo() self.errStereo, _, _, _, _, self.R, self.T, self.E, self.F, self.perViewErrors = cv.stereoCalibrateExtended( objpoints, imgpoints_l, imgpoints_r, self.M1, self.d1, self.M2, self.d2, self.imageSize1, self.R, self.T, flags=flags) print(len(self.perViewErrors)) # Enlever les outliers ------------------------------------------------- indices = np.indices(self.perViewErrors.shape)[0] b = self.perViewErrors < self.errStereo * 1 B = b[:, 0] * b[:, 1] indexes = indices[B][:, 0] if len(indexes) > 0: objpoints = np.array(objpoints)[indexes] imgpoints_l = np.array(imgpoints_l)[indexes] imgpoints_r = np.array(imgpoints_r)[indexes] # ---------------------------------------------------------------------- # Ajouter les points des cibles objpoints = list(objpoints) imgpoints_l = list(imgpoints_l) imgpoints_r = list(imgpoints_r) objpoints.append(cibles) imgpoints_l.append(cibles_l) imgpoints_r.append(cibles_r) self.errStereo, _, _, _, _, self.R, self.T, self.E, self.F, self.perViewErrors = cv.stereoCalibrateExtended( objpoints, imgpoints_l, imgpoints_r, self.M1, self.d1, self.M2, self.d2, self.imageSize1, self.R, self.T, flags=flags) print(len(self.perViewErrors)) # Print erreur de reprojection print('Erreur de reprojection RMS calibration stereo') print(self.errStereo)
def calibrateIntrinsics(self, single_path, single_detected_path, cibles, cibles_l, cibles_r, fisheye=False): """ ||Public method|| Calibration individuelle de 2 caméras AVEC DES CIBLES Args: single_path (str): "path_to_single_images/" single_detected_path (str): "path_to_single_images_detected/" fisheye (Bool): True pour caméra fisheye """ self.single_path = single_path self.single_detected_path = single_detected_path clean_folders([single_detected_path]) self.objpoints_l, self.imgpoints_l, self.imageSize1 = self.__read_single( 'left') self.objpoints_r, self.imgpoints_r, self.imageSize2 = self.__read_single( 'right') if fisheye == True: flags = self.fisheye_flags else: flags = self.not_fisheye_flags # GAUCHE self.err1, self.M1, self.d1, self.r1, self.t1, stdDeviationsIntrinsics1, stdDeviationsExtrinsics1, self.perViewErrors1 = cv.calibrateCameraExtended( self.objpoints_l, self.imgpoints_l, self.imageSize1, None, None, flags=flags) # DROITE self.err2, self.M2, self.d2, self.r2, self.t2, stdDeviationsIntrinsics2, stdDeviationsExtrinsics2, self.perViewErrors2 = cv.calibrateCameraExtended( self.objpoints_r, self.imgpoints_r, self.imageSize2, None, None, flags=flags) # Enlever les outliers ------------------------------------------------- # gauche objpoints_l = np.array(self.objpoints_l.copy()) imgpoints_l = np.array(self.imgpoints_l.copy()) indices = np.indices(self.perViewErrors1.shape)[0] self.indexes_l = indices[self.perViewErrors1 < self.err1 * 1] if len(self.indexes_l) > 0: objpoints_l = objpoints_l[self.indexes_l] imgpoints_l = imgpoints_l[self.indexes_l] # droite objpoints_r = np.array(self.objpoints_r.copy()) imgpoints_r = np.array(self.imgpoints_r.copy()) indices = np.indices(self.perViewErrors2.shape)[0] self.indexes_r = indices[self.perViewErrors2 < self.err2 * 1] if len(self.indexes_r) > 0: objpoints_r = objpoints_r[self.indexes_r] imgpoints_r = imgpoints_r[self.indexes_r] # ---------------------------------------------------------------------- # Ajouter les points des cibles objpoints_l = list(objpoints_l) objpoints_r = list(objpoints_r) imgpoints_l = list(imgpoints_l) imgpoints_r = list(imgpoints_r) objpoints_l.append(cibles) objpoints_r.append(cibles) imgpoints_l.append(cibles_l) imgpoints_r.append(cibles_r) self.err1, self.M1, self.d1, self.r1, self.t1, stdDeviationsIntrinsics1, stdDeviationsExtrinsics1, self.perViewErrors1 = cv.calibrateCameraExtended( objpoints_l, imgpoints_l, self.imageSize1, None, None, flags=flags) self.err2, self.M2, self.d2, self.r2, self.t2, stdDeviationsIntrinsics2, stdDeviationsExtrinsics2, self.perViewErrors2 = cv.calibrateCameraExtended( objpoints_r, imgpoints_r, self.imageSize2, None, None, flags=flags) # Print erreur de reprojection print('Erreur de reprojection RMS calibration individuelle') print(self.err1, self.err2)
def calibrateStereo(self, stereo_path, stereo_detected_path, single_detected_path, fisheye=False, calib_2_sets=False, single_path=None): """ ||Public method|| Args: stereo_path (str): "path_to_stereo_images/" fisheye (Bool): True pour caméra fisheye calib_2_sets (Bool): True: pour utiliser des sets de photos différents (un pour la calibration individuelle des caméra et un pour la calibration stéréo) False: pour utiliser un seul set de photo pour la calibration individuelle et la calibration stéréo single_path: "path_to_single_images/" """ self.stereo_path = stereo_path self.stereo_detected_path = stereo_detected_path clean_folders([stereo_detected_path]) if not calib_2_sets: single_path = stereo_path # faire calibration individuelle avant if self.err1 == None or self.err2 == None: self.calibrateSingle(single_path, single_detected_path, fisheye) # deux sets ou un set if calib_2_sets: flags = cv.CALIB_FIX_INTRINSIC else: flags = cv.CALIB_USE_INTRINSIC_GUESS # caméra fisheye if fisheye: flags += self.fisheye_flags else: flags += self.not_fisheye_flags # calibration stereo objpoints, imgpoints_l, imgpoints_r = self.__read_stereo() self.errStereo, _, _, _, _, self.R, self.T, self.E, self.F, self.stereo_per_view_err = cv.stereoCalibrateExtended( objpoints, imgpoints_l, imgpoints_r, self.M1, self.d1, self.M2, self.d2, self.imageSize1, None, None, flags=flags) # Enlever les outliers et recalibrer: indices = np.indices(self.stereo_per_view_err.shape)[0] indexes = indices[self.stereo_per_view_err > self.errStereo * 2] if len(indexes) > 0: for i in indexes: objpoints.pop(i) imgpoints_l.pop(i) imgpoints_r.pop(i) # re-calculs self.errStereo, _, _, _, _, self.R, self.T, self.E, self.F, self.stereo_per_view_err = cv.stereoCalibrateExtended( objpoints, imgpoints_l, imgpoints_r, self.M1, self.d1, self.M2, self.d2, self.imageSize1, None, None, flags=flags) # Print erreur de reprojection print('Erreur de reprojection RMS calibration stereo') print(self.errStereo)