马春杰杰 Exit Reader Mode

[mcj]利用MATLAB进行相机标定,附源码!

前面写了关于c++的代码,以及python的,后来发现MATLAB也可以,并且更简单。代码如下:

% Auto-generated by cameraCalibrator app on 26-Mar-2019
%-------------------------------------------------------
% Define images to process
imageFileNames = {'/home/ubuntu/Desktop/mcj/left1.jpg',...
    '/home/ubuntu/Desktop/mcj/left2.jpg',...
    '/home/ubuntu/Desktop/mcj/left4.jpg',...
    '/home/ubuntu/Desktop/mcj/left6.jpg',...
    '/home/ubuntu/Desktop/mcj/left9.jpg',...
    };

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

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

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

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

会生成两个图,一个是标定图片的空间位置

另一个是每张图片的重投影误差

图片最好准备多点,实测MATLAB会剔除一些图片。

仔细阅读了OpenCV的说明之后你会大概明白畸变参数,总共有五个,径向畸变3个(k1,k2,k3)和切向畸变2个(p1,p2)。
径向畸变

切向畸变

以及在OpenCV中的畸变系数的排列(这点一定要注意k1,k2,p1,p2,k3),千万不要以为k是连着的。

并且通过实验表明,三个参数的时候由于k3所对应的非线性较为剧烈。估计的不好,容易产生极大的扭曲,所以我们在MATLAB中选择使用两参数,并且选择错切和桶形畸变

可以看到workspace已经生成结果了~

第三个即为相机参数,打开看看:

里面的RadialDistortion对应k1,k2,k3设置为0了。
TangentialDistortion对应p1,p2。

IntrinsicMatrix对应内参,注意这个和OpenCV中是转置的关系,注意不要搞错。

还有内参矩阵,注意这是转置的: