% Auto-generated by cameraCalibrator app on 06-Apr-2015
%-------------------------------------------------------

clear
close all

% Define images to process
imageFileNames = {'C:\ece564\stereo\stereo2\cam1_image_1.jpg',...
    'C:\ece564\stereo\stereo2\cam1_image_3.jpg',...
    'C:\ece564\stereo\stereo2\cam1_image_6.jpg',...
    'C:\ece564\stereo\stereo2\cam1_image_8.jpg',...
    };

% Detect checkerboards in images
[imagePoints, boardSize, imagesUsed] = detectCheckerboardPoints(imageFileNames);
imageFileNames = imageFileNames(imagesUsed);

% Generate world coordinates of the corners of the squares
squareSize = 3;  % in units of 'in'
worldPoints = generateCheckerboardPoints(boardSize, squareSize);

% Calibrate the camera
cameraParams = estimateCameraParameters(imagePoints, worldPoints, ...
    'EstimateSkew', false, 'EstimateTangentialDistortion', false, ...
    'NumRadialDistortionCoefficients', 2, 'WorldUnits', 'in');

% View reprojection errors
h1=figure; showReprojectionErrors(cameraParams, 'BarGraph');

% Visualize pattern locations
h2=figure; showExtrinsics(cameraParams, 'CameraCentric');

% For example, you can use the calibration data to remove effects of lens distortion.
originalImage = imread(imageFileNames{1});
undistortedImage = undistortImage(originalImage, cameraParams);

% See additional examples of how to use the calibration data.  At the prompt type:
% showdemo('MeasuringPlanarObjectsExample')
% showdemo('SparseReconstructionExample')
Warning: The checkerboard must be asymmetric:
one side should be even, and the other should
be odd. Otherwise, the orientation of the board
may be detected incorrectly.