% Auto-generated by cameraCalibrator app on 09-May-2016
%-------------------------------------------------------


% Define images to process
imageFileNames = {'calFotky\vid30001.jpg',...
    'calFotky\vid30011.jpg',...
    'calFotky\vid30012.jpg',...
    'calFotky\vid30405.jpg',...
    'calFotky\vid30530.jpg',...
    'calFotky\vid30591.jpg',...
    'calFotky\vid30898.jpg',...
    'calFotky\vid30900.jpg',...
    'calFotky\vid30909.jpg',...
    'calFotky\vid30920.jpg',...
    'calFotky\vid31279.jpg',...
    'calFotky\vid31426.jpg',...
    'calFotky\vid31443.jpg',...
    'calFotky\vid31595.jpg',...
    'calFotky\vid31599.jpg',...
    };

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

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

% Calibrate the camera
[cameraParams, imagesUsed, estimationErrors] = estimateCameraParameters(imagePoints, worldPoints, ...
    'EstimateSkew', true, 'EstimateTangentialDistortion', true, ...
    'NumRadialDistortionCoefficients', 2, 'WorldUnits', 'mm', ...
    'InitialIntrinsicMatrix', [], 'InitialRadialDistortion', []);

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

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

% Display parameter estimation errors
% displayErrors(estimationErrors, cameraParams);

% 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('StructureFromMotionExample')
