%% Configure Monocular Camera Using Checkerboard Pattern
% Configure a monocular fisheye camera by removing lens distortion and then estimating the camera's extrinsic parameters. Use an image of a checkerboard as the calibration pattern. For a more detailed look at how to configure a monocular camera that has a fisheye lens, see the Configure Monocular Fisheye Camera example.
% Load the intrinsic parameters of a monocular camera that has a fisheye lens. intrinsics is a fisheyeIntrinsics object.

ld = load('fisheyeCameraIntrinsics');
intrinsics = ld.intrinsics;

% Load an image of a checkerboard pattern that is placed flat on the ground. This image is for illustrative purposes and was not taken from a camera mounted to the vehicle. In a camera mounted to the vehicle, the X-axis of the pattern points to the right of the vehicle, and the Y-axis of the pattern points to the camera. Display the image.

imageFileName = fullfile(toolboxdir('driving'),'drivingdata','checkerboard.png');
I = imread(imageFileName);
imshow(I)

% Detect the coordinates of the checkerboard corners in the image.

[imagePoints,boardSize] = detectCheckerboardPoints(I);

%  Generate the corresponding world coordinates of the corners.

squareSize = 0.029; % Square size in meters
worldPoints = generateCheckerboardPoints(boardSize,squareSize);

% Estimate the extrinsic parameters required to configure the monoCamera object. Because the checkerboard pattern is directly on the ground, set the height of the pattern's origin to 0.

patternOriginHeight = 0;
[pitch,yaw,roll,height] = estimateMonoCameraParameters(intrinsics, ...
                             imagePoints,worldPoints,patternOriginHeight);

% Because monoCamera does not accept fisheyeIntrinsics objects, remove distortion from the image and compute new intrinsic parameters from the undistorted image. camIntrinsics is an cameraIntrinsics object. Display the image to confirm distortion is removed.

[undistortedI,camIntrinsics] = undistortFisheyeImage(I,intrinsics,'Output','full');
imshow(undistortedI)

% Configure the monocular camera using the estimated parameters.

monoCam = monoCamera(camIntrinsics,height,'Pitch',pitch,'Yaw',yaw,'Roll',roll)

% Copyright 2018 The MathWorks, Inc.