def calibrate_folder(args):
    """
    Calibrate camera based on chessboard images, write results to output folder.

    All images are read from disk. Chessboard points are found and used to
    calibrate the stereo pair. Finally, the calibration is written to the folder
    specified in ``args``.

    ``args`` needs to contain the following fields:
        input_files: List of paths to input files
        rows: Number of rows in chessboard
        columns: Number of columns in chessboard
        square_size: Size of chessboard squares in cm
        output_folder: Folder to write calibration to
    """
    print "file: " + args.input_files[0]
    height, width = cv2.imread(args.input_files[0]).shape[:2]
    calibrator = StereoCalibrator(args.rows, args.columns, args.square_size,
                                  (width, height))
    progress = ProgressBar(maxval=len(args.input_files),
                          widgets=[Bar("=", "[", "]"),
                          " ", Percentage()])
    print("Reading input files...")
    progress.start()
    while args.input_files:
        left, right = args.input_files[:2]

        print "processing: "
        print "    %s" %  left
        print "    %s" %  right
        print ""

        img_left, img_right = cv2.imread(left), cv2.imread(right)

        if img_left is not None and img_right is not None:
          calibrator.add_corners((img_left, img_right),
                                 show_results=args.show_chessboards)
          args.input_files = args.input_files[2:]
          progress.update(progress.maxval - len(args.input_files))
        else:
          print "error loading images."

    progress.finish()
    print("Calibrating cameras. This can take a while.")
    calibration = calibrator.calibrate_cameras()
    avg_error = calibrator.check_calibration(calibration)
    print("The average error between chessboard points and their epipolar "
          "lines is \n"
          "{} pixels. This should be as small as possible.".format(avg_error))
    calibration.export(args.output_folder)
Beispiel #2
0
def calibrate_folder(args):
    """
    Calibrate camera based on chessboard images, write results to output folder.

    All images are read from disk. Chessboard points are found and used to
    calibrate the stereo pair. Finally, the calibration is written to the folder
    specified in ``args``.

    ``args`` needs to contain the following fields:
        input_files: List of paths to input files
        rows: Number of rows in chessboard
        columns: Number of columns in chessboard
        square_size: Size of chessboard squares in cm
        output_folder: Folder to write calibration to
    """
    height, width = cv2.imread(args.input_files[0]).shape[:2]
    calibrator = StereoCalibrator(args.rows, args.columns, args.square_size,
                                  (width, height))
    progress = ProgressBar(maxval=len(args.input_files),
                           widgets=[Bar("=", "[", "]"), " ",
                                    Percentage()])
    print("Reading input files...")
    progress.start()
    while args.input_files:
        left, right = args.input_files[:2]
        img_left, im_right = cv2.imread(left), cv2.imread(right)
        calibrator.add_corners((img_left, im_right),
                               draw_results=args.show_chessboards)

        cv2.imshow("left_chessboard", img_left)
        cv2.imshow("right_chessboard", im_right)
        while True:
            key = cv2.waitKey(30) & 0xff
            if key == 27:  # esc
                break
        cv2.destroyWindow("left_chessboard")
        cv2.destroyWindow("right_chessboard")

        args.input_files = args.input_files[2:]
        progress.update(progress.maxval - len(args.input_files))

    progress.finish()
    print("Calibrating cameras. This can take a while.")
    calibration = calibrator.calibrate_cameras()
    avg_error = calibrator.check_calibration(calibration)
    print("The average error between chessboard points and their epipolar "
          "lines is \n"
          "{} pixels. This should be as small as possible.".format(avg_error))
    calibration.export(args.output_folder)
Beispiel #3
0
from NEU_StereoPair import StereoPair
from stereovision.calibration import StereoCalibrator
import cv2

rows, colums = 9, 6
square_size = 2.4
image_size = (640, 480)

img_left = cv2.imread('./left.jpg')
img_right = cv2.imread('./right.jpg')

calibrator = StereoCalibrator(rows, colums, square_size, image_size)

calibrator.add_corners((img_left, img_right), show_results=True)
calibration = calibrator.calibrate_cameras()
print(calibration)

avg_error = calibrator.check_calibration(calibration)
print(avg_error)

calibration.export('./calibration_result')
Beispiel #4
0
import os.path
import numpy as np
from stereovision.calibration import StereoCalibrator, StereoCalibration
from stereovision.blockmatchers import StereoBM, StereoSGBM
import cv2

calib_dir = './dataset_split_v2/CAM00054'
#if(not os.path.exists(calib_dir)):
calibrator = StereoCalibrator(8, 5, 2, (1536, 2048))
for idx in range(1, 14):
    calibrator.add_corners((cv2.imread(calib_dir+"_s1.jpg"), cv2.imread(calib_dir+"_s2.jpg")))


calibration = calibrator.calibrate_cameras()
print ("Calibation error:", calibrator.check_calibration(calibration))
calibration.export(calib_dir)

calibration = StereoCalibration(input_folder=calib_dir)

if True:
    block_matcher = StereoBM()
else:
    block_matcher = StereoSGBM()

for idx in range(1, 14):
    image_pair = (cv2.imread('images/left%02d.jpg' %idx), cv2.imread('images/right%02d.jpg' %idx))
    rectified_pair = calibration.rectify(image_pair)
    disparity = block_matcher.get_disparity(rectified_pair)
    norm_coeff = 255 / disparity.max()
    cv2.imshow('Disparity %02d' %idx, disparity * norm_coeff / 255)