主要内容

本页采用了机器翻译。点击此处可查看英文原文。

estimateCameraParameters

标定单目或立体相机

说明

[cameraParams,imagesUsed,estimationErrors] = estimateCameraParameters(imagePoints,worldPoints) 返回一个名为 cameraParams 的对象,其中包含单镜头和立体相机的内参、外参以及畸变系数的估计值。该函数还会返回用于估计相机参数的图像,以及单相机标定中的标准估计误差。estimateCameraParameters 函数用于估计外参和内参。

示例

[stereoParams,pairsUsed,estimationErrors] = estimateCameraParameters(imagePoints,worldPoints) 返回 stereoParams,这是一个包含立体相机参数的 stereoParameters 对象。该函数还会返回用于估计立体参数的图像,以及立体相机标定的标准估计误差。

示例

cameraParams = estimateCameraParameters(___,Name=Value) 支持上述语法中的任何参量组合,且可使用一个或多个名称-值参量指定选项。例如,WorldUnits="mm" 将世界单位设置为毫米。

示例

示例

全部折叠

创建一组标定图像。

imds = imageDatastore(fullfile(toolboxdir("vision"),"visiondata",...
            "calibration","mono"));
imageFileNames = imds.Files;

检测标定图案。

[imagePoints,patternDims] = detectCheckerboardPoints(imageFileNames);

生成正方形各角点的世界坐标。

squareSizeInMM = 29; % millimeters
worldPoints = patternWorldPoints("checkerboard",patternDims,squareSizeInMM);

标定相机。

I = preview(imds); 
imageSize = size(I,1:2);
params = estimateCameraParameters(imagePoints,worldPoints,ImageSize=imageSize);

直观展示标定精度。

showReprojectionErrors(params);

Figure contains an axes object. The axes object with title Mean Reprojection Error per Image, xlabel Images, ylabel Mean Error in Pixels contains 3 objects of type bar, line. This object represents Overall Mean Error: 0.18 pixels.

可视化相机外参。

figure;
showExtrinsics(params);

Figure contains an axes object. The axes object with title Extrinsic Parameters Visualization, xlabel X (mm), ylabel Z (mm) contains 23 objects of type patch, text, line.

drawnow;

绘制检测到的点和重投影点。

figure; 
imshow(imageFileNames{1}); 
hold on;
plot(imagePoints(:,1,1), imagePoints(:,2,1),"go");
plot(params.ReprojectedPoints(:,1,1),params.ReprojectedPoints(:,2,1),"r+");
legend("Detected Points","ReprojectedPoints");
hold off;

Figure contains an axes object. The hidden axes object contains 3 objects of type image, line. One or more of the lines displays its values using only markers These objects represent Detected Points, ReprojectedPoints.

指定标定图像。

leftImages = imageDatastore(fullfile(toolboxdir("vision"),"visiondata", ...
    "calibration","stereo","left"));
rightImages = imageDatastore(fullfile(toolboxdir("vision"),"visiondata", ...
    "calibration","stereo","right"));

检测棋盘格。

[imagePoints,patternDims] = ...
  detectCheckerboardPoints(leftImages.Files,rightImages.Files);

请指定棋盘格关键点的世界坐标。正方形的尺寸以毫米为单位。

squareSize = 108; % millimeters
worldPoints = patternWorldPoints("checkerboard",patternDims,squareSize);

对立体相机系统进行标定。这两台相机的分辨率相同。

I = readimage(leftImages,1); 
imageSize = size(I,1:2);
params = estimateCameraParameters(imagePoints,worldPoints,ImageSize=imageSize);

直观展示标定精度。

showReprojectionErrors(params)

Figure contains an axes object. The axes object with title Mean Reprojection Error per Image, xlabel Image Pairs, ylabel Mean Error in Pixels contains 5 objects of type bar, line. These objects represent Camera 1, Camera 2, Overall Mean Error: 0.06 pixels.

可视化相机外参。

figure
showExtrinsics(params)

Figure contains an axes object. The axes object with title Extrinsic Parameters Visualization, xlabel X (mm), ylabel Z (mm) contains 28 objects of type patch, text, line.

转换为结构体。

paramsStruct = toStruct(params)
paramsStruct = struct with fields:
       CameraParameters1: [1×1 struct]
       CameraParameters2: [1×1 struct]
       RotationOfCamera2: [3×3 double]
    TranslationOfCamera2: [-119.8720 -0.4005 -0.0258]
                 Version: [1×1 struct]
     RectificationParams: [1×1 struct]

创建一组标定图像。

imds = imageDatastore(fullfile(toolboxdir("vision"),"visiondata",...
                        "calibration","circleGrid","mono"));
calibrationImages = readall(imds);
calibrationImages = cat(4,calibrationImages{:});

定义圆点网格图案的维度。

patternDims = [8 11];

检测标定图像中的图案。

imagePoints = detectCircleGridPoints(calibrationImages, patternDims,...
                        PatternType="symmetric");

指定圆点网格关键点的世界坐标。中心距以毫米为单位。

centerDistance = 18; % millimeters
worldPoints = patternWorldPoints("circle-grid-symmetric",patternDims,centerDistance);

使用标定图像对相机进行标定。

imageSize = size(calibrationImages,1:2);
params = estimateCameraParameters(imagePoints,worldPoints,ImageSize=imageSize);

绘制检测到的图案网格和重新投影后的点。

figure
imshow(calibrationImages(:,:,:,1))
hold on
plot(imagePoints(:,1,1), imagePoints(:,2,1),"gx",MarkerSize=8)
plot(params.ReprojectedPoints(:,1,1),params.ReprojectedPoints(:,2,1),"r+",MarkerSize=8)
legend("Detected Points","ReprojectedPoints")
hold off

Figure contains an axes object. The hidden axes object contains 3 objects of type image, line. One or more of the lines displays its values using only markers These objects represent Detected Points, ReprojectedPoints.

输入参数

全部折叠

标定图案的关键点,指定为 [x,y] 形式的图像内部坐标数组。

标定输入数组 [x,y] 关键点
单目相机

M×2×numImages 由 [x,y] 个点组成的数组。

  • 图像数量 numImages 必须大于或等于 2。

  • 每个图案中关键点坐标的数量,M,必须大于 3。

仅在单相机标定中支持部分检测到的图案。若要将部分检测到的图案纳入估计值,请将 [NaN,NaN] 用作缺失关键点的 x-y 坐标。

立体相机

M×2×numPairs×2 点 [x,y] 组成的数组。

  • numPairs 表示包含标定图案的立体图像对的数量。必须大于 2。

  • 每个图案中关键点坐标的数量,M,必须大于 3。

  • imagePoints(:,:,:,1) 是来自相机 1 的点。

  • imagePoints(:,:,:,2) 是来自相机 2 的点。

数据类型: single | double

世界坐标系中标定图案的关键点,指定为一个 M 行 × 2 列的数组,包含 M 个 [x,y] 世界坐标。该图案必须是平面的;因此,z 坐标均为零。标定图像不得位于同一平面上。每个空间平面仅使用一张标定图像。

数据类型: single | double

名称-值参数

全部折叠

将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

如果使用的是 R2021a 之前的版本,请使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: "WorldUnits","mm" 将世界坐标系单位设置为毫米。

世界坐标单位,指定为字符向量或字符串标量。

估计偏斜,指定为逻辑标量。将此属性设置为 true 时,该函数会估算图像轴的倾斜度。当设置为 false 时,图像轴完全垂直,且该函数将倾斜度设为零。

要估计的径向畸变系数的数量,指定为值 23

径向畸变是指图像点沿从主点延伸出的径向线发生位移的现象。

  • 随着像点远离主点(正径向位移),成像放大率会减小,且像上会出现枕形畸变。

  • 随着像点向主点移动(负径向位移),像的放大率增加,且像上会出现桶形畸变。

Three grids that each represent a type of distortion. One with pincushion distortion (positive radial displacement), one with no distortion, and one with barrel distortion (negative radial displacement)

径向畸变系数用于对此类畸变进行建模。畸变点表示为 (xdistorted, ydistorted):

xdistorted = x(1 + k1*r2 + k2*r4 + k3*r6)

ydistorted= y(1 + k1*r2 + k2*r4 + k3*r6)

  • xy - 未发生畸变的像素位置。xy 采用归一化图像坐标。归一化图像坐标通过平移至光心并除以焦距(以像素为单位)从像素坐标计算得出。因此,xy 为无量纲。

  • k1k2k3 - 镜头的径向畸变系数。

  • r2 = x2 + y2

通常,两个系数足以用于标定。对于严重畸变(如广角镜头),可选择包含 k3 的三个系数。

切向畸变标志,指定为逻辑标量。将此属性设置为 true 时,该函数会估算切向畸变。当将其设置为 false 时,切向畸变可以忽略不计。

当镜头与图像平面不平行时,会发生切向畸变。切向畸变系数用于对此类畸变进行建模。

Comparison of zero tangential distortion and tangential distortion

畸变点表示为 (xdistorted, ydistorted):

xdistorted = x + [2 * p1 * x * y + p2 * (r2 + 2 * x2)]

ydistorted = y + [p1 * (r2 + 2 *y2) + 2 * p2 * x * y]

  • xy - 未发生畸变的像素位置。xy 采用归一化图像坐标。归一化图像坐标通过平移至光心并除以焦距(以像素为单位)从像素坐标计算得出。因此,xy 为无量纲。

  • p1p2 - 镜头的切向畸变系数。

  • r2 = x2 + y2

相机内参的初始估计值,指定为一个 3×3 矩阵。该矩阵的格式为:

[fxscx0fycy001]

坐标 [cx cy] 表示光学中心(主点),单位为像素。当 x-轴和 y-轴完全垂直时,倾斜参数 s 等于 0.

fx = F*sx

fy = F*sy

  • F 是世界单位中的焦距,通常以毫米为单位表示。

  • sxsy 分别表示在 xy 方向上每世界单位对应的像素数。

  • fxfy 以像素为单位表示。

如果未提供初始值,该函数将使用线性最小二乘法计算初始内参矩阵。

径向畸变系数的初始估计值,指定为一个由 2 个或 3 个元素组成的向量。如果您未提供初始值,该函数将使用 0 作为所有系数的初始值。

相机生成的图像尺寸,指定为一个 1×2 的 [mrows, ncols] 向量。要返回相机的 Intrinsics 属性值,必须指定 ImageSize 名称-值参量。

输出参量

全部折叠

相机参数,以 cameraParameters 对象的形式返回。如果输入中未指定 ImageSize 名称-值参量,则 cameraParams 对象将不包含 Intrinsics 属性的值。

用于估计相机参数的图像,以 numImages×1 逻辑数组的形式返回。numImages 对应于图像的数量。该数组指示了您用于估计相机参数的图像。数组中的逻辑值 true 表示您用于估计相机参数的图像。

该函数计算现实世界中的点与每个图像中检测到的点之间的单应性。如果某个图像的单应性计算失败,该函数会发出警告。该图像的点不会用于估计相机参数。该函数还会将 imagesUsed 的相应元素设置为 false

估计参数的标准误差,以 cameraCalibrationErrors 对象或 stereoCalibrationErrors 对象的形式返回。

立体视觉系统的相机参数,以 stereoParameters 对象的形式返回。该对象包含立体相机系统的内参、外参和镜头畸变参数。

用于估计相机参数的图像对,以 numPairs×1 逻辑数组的形式返回。numPairs 对应于图像对的数量。数组中的逻辑值 true 表示您使用了哪些图像对来估计相机参数。

算法

全部折叠

参考

[1] Zhang, Z. "A Flexible New Technique for Camera Calibration." IEEE Transactions on Pattern Analysis and Machine Intelligence 22, no. 11 (November 2000): 1330–34. https://doi.org/10.1109/34.888718.

[2] Heikkila, J., and O. Silven. “A Four-Step Camera Calibration Procedure with Implicit Image Correction.” In Proceedings of IEEE Computer Society Conference on Computer Vision and Pattern Recognition, 1106–12. San Juan, Puerto Rico: IEEE Comput. Soc, 1997. https://doi.org/10.1109/CVPR.1997.609468.

[3] Bouguet, J.Y. “Camera Calibration Toolbox for Matlab”, Computational Vision at the California Institute of Technology.

[4] Bradski, G., and A. Kaehler. Learning OpenCV : Computer Vision with the OpenCV Library. Sebastopol, CA: O'Reilly, 2008.

版本历史记录

在 R2014b 中推出

全部展开