def calibrate(self): rows = 6 columns = 8 square_size = 3 image_size = (640, 480) path = 'resources/pictures/calibration_photos/' right = 'photo_right' left = 'photo_left' ext = '.jpg' calibrator = StereoCalibrator(rows, columns, square_size, image_size) #print('Start processing') for i in range(1, 16): im_path_left = os.path.join(path, left) + str(i) + ext im_path_right = os.path.join(path, right) + str(i) + ext if os.path.exists(im_path_left) and os.path.exists(im_path_right): img_left = cv2.imread(im_path_left) img_right = cv2.imread(im_path_right) else: print(str(i) + ' step failed. Wrong path.') continue try: calibrator._get_corners(img_left) calibrator._get_corners(img_right) except ChessboardNotFoundError as error: print(error) print("Pair No " + str(i) + " ignored") else: calibrator.add_corners((img_left, img_right), True) print('End processing') return
def calibration(): calibrator = StereoCalibrator(rows, columns, square_size, image_size) print('Start processing') for i in range(1, 16): im_path_left = os.path.join(path, path2, left) + str(i) + ext im_path_right = os.path.join(path, path2, right) + str(i) + ext if os.path.exists(im_path_left) and os.path.exists(im_path_right): img_left = cv2.imread(im_path_left) img_right = cv2.imread(im_path_right) else: print(str(i) + ' step failed. Wrong path.') continue try: calibrator._get_corners(img_left) calibrator._get_corners(img_right) except ChessboardNotFoundError as error: print(error) print("Pair No " + str(i) + " ignored") else: calibrator.add_corners((img_left, img_right), True) print('End processing') calibration = calibrator.calibrate_cameras() calibration.export('D:\learn\8 sem\diplom\diplom\calib_result') print('Calibration complete!')
def start_calibration(self): calibrator = StereoCalibrator(self.rows, self.columns, self.square_size, (self.img_width, self.img_height)) photo_counter = 0 print("Start calibration, press any key on image to move to the next") while photo_counter != self.total_photos: print('Import pair No ' + str(photo_counter)) leftName = 'capture/pairs/left_' + str(photo_counter).zfill( 2) + '.png' rightName = 'capture/pairs/right_' + str(photo_counter).zfill( 2) + '.png' photo_counter = photo_counter + 1 if os.path.isfile(leftName) and os.path.isfile(rightName): imgLeft = cv2.imread(leftName, 1) imgRight = cv2.imread(rightName, 1) try: calibrator._get_corners(imgLeft) calibrator._get_corners(imgRight) except ChessboardNotFoundError as error: print(error) print("Pair No " + str(photo_counter) + " ignored") else: calibrator.add_corners((imgLeft, imgRight), True) print('End cycle') print('Starting calibration... It can take several minutes!') calibration = calibrator.calibrate_cameras() calibration.export('calibrate/calib_result') print('Calibration complete!') # Lets rectify and show last pair after calibration calibration = StereoCalibration(input_folder='calibrate/calib_result') rectified_pair = calibration.rectify((imgLeft, imgRight)) cv2.imshow('Left CALIBRATED', rectified_pair[0]) cv2.imshow('Right CALIBRATED', rectified_pair[1]) cv2.imwrite("calibrate/rectifyed_left.jpg", rectified_pair[0]) cv2.imwrite("calibrate/rectifyed_right.jpg", rectified_pair[1]) cv2.waitKey(0)
square_size = 2.5 calibrator = StereoCalibrator(rows, columns, square_size, image_size) photo_counter = 0 print('Start cycle') while photo_counter != total_photos: photo_counter = photo_counter + 1 print('Import pair No ' + str(photo_counter)) leftName = './pairs/left_' + str(photo_counter).zfill(2) + '.png' rightName = './pairs/right_' + str(photo_counter).zfill(2) + '.png' if os.path.isfile(leftName) and os.path.isfile(rightName): imgLeft = cv2.imread(leftName, 1) imgRight = cv2.imread(rightName, 1) try: calibrator._get_corners(imgLeft) calibrator._get_corners(imgRight) except ChessboardNotFoundError as error: print(error) print("Pair No " + str(photo_counter) + " ignored") else: calibrator.add_corners((imgLeft, imgRight), True) print('End cycle') print('Starting calibration... It can take several minutes!') calibration = calibrator.calibrate_cameras() calibration.export('calib_result') print('Calibration complete!') # Lets rectify and show last pair after calibration
print ("Starting photo sequence") all_left_corners = np.array([[0, 0]]) all_right_corners = np.array([[0, 0]]) for frame in camera.capture_continuous(capture, format="bgra", \ use_video_port=True, resize=(img_width,img_height)): t1 = datetime.now() cntdwn_timer = countdown - int ((t1-t2).total_seconds()) # If cowntdown is zero - let's record next image if cntdwn_timer <= -1: ## check frame for two boards imgLeft = frame[0:img_height,0:img_width//2] #Y+H and X+W imgRight = frame[0:img_height,img_width//2:] try: left_corners = np.squeeze(calibrator._get_corners(imgLeft)) right_corners = np.squeeze(calibrator._get_corners(imgRight)) all_left_corners = np.vstack([all_left_corners, left_corners.astype(int)]) all_right_corners = np.vstack([all_right_corners, right_corners.astype(int)]) except ChessboardNotFoundError as error: print (error) print ("Pair No try again") t2 = datetime.now() continue counter += 1 filename = './scenes/scene_'+str(img_width)+'x'+str(img_height)+'_'+\ str(counter) + '.png' cv2.imwrite(filename, frame)