相机标定基本知识

对于针孔摄像机模型,一幅视图是通过透视变换将三维空间中的点投影到图像平面。投影公式如下:

或者

这里(X, Y, Z)是一个点的世界坐标,(u, v)是点投影在图像平面的坐标,以像素为单位。A被称作摄像机矩阵,或者内参数矩阵。(cx, cy)是基准点(通常在图像的中心),fx, fy是以像素为单位的焦距。所以如果因为某些因素对来自于摄像机的一幅图像升采样或者降采样,所有这些参数(fx, fy, cx和cy)都将被缩放(乘或者除)同样的尺度。内参数矩阵不依赖场景的视图,一旦计算出,可以被重复使用(只要焦距固定)。旋转-平移矩阵[R|t]被称作外参数矩阵,它用来描述相机相对于一个固定场景的运动,或者相反,物体围绕相机的的刚性运动。也就是[R|t]将点(X, Y, Z)的坐标变换到某个坐标系,这个坐标系相对于摄像机来说是固定不变的。上面的变换等价与下面的形式(z≠0):

x' = x / z

y' = y / z

真正的镜头通常有一些形变,主要的变形为径向形变,也会有轻微的切向形变。所以上面的模型可以扩展为:

x' = x / z

y' = y / z

这里 r2 = x'2 + y'2

k1和k2是径向形变系数,p1和p1是切向形变系数。OpenCV中没有考虑高阶系数。形变系数跟拍摄的场景无关,因此它们是内参数,而且与拍摄图像的分辨率无关。

OpenCV标定函数

double cv::calibrateCamera ( InputArrayOfArrays  objectPoints,
    InputArrayOfArrays  imagePoints,
    Size  imageSize,
    InputOutputArray  cameraMatrix,
    InputOutputArray  distCoeffs,
    OutputArrayOfArrays  rvecs,
    OutputArrayOfArrays  tvecs,
    OutputArray  stdDeviationsIntrinsics,
    OutputArray  stdDeviationsExtrinsics,
    OutputArray  perViewErrors,
    int  flags = 0,
    TermCriteria  criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, DBL_EPSILON) 
  )    

Finds the camera intrinsic and extrinsic parameters from several views of a calibration pattern.

Parameters

objectPoints In the new interface it is a vector of vectors of calibration pattern points in the calibration pattern coordinate space (e.g. std::vector<std::vector<cv::Vec3f>>). The outer vector contains as many elements as the number of the pattern views. If the same calibration pattern is shown in each view and it is fully visible, all the vectors will be the same. Although, it is possible to use partially occluded patterns, or even different patterns in different views. Then, the vectors will be different. The points are 3D, but since they are in a pattern coordinate system, then, if the rig is planar, it may make sense to put the model to a XY coordinate plane so that Z-coordinate of each input object point is 0. In the old interface all the vectors of object points from different views are concatenated together.
imagePoints In the new interface it is a vector of vectors of the projections of calibration pattern points (e.g. std::vector<std::vector<cv::Vec2f>>). imagePoints.size() and objectPoints.size() and imagePoints[i].size() must be equal to objectPoints[i].size() for each i. In the old interface all the vectors of object points from different views are concatenated together.
imageSize Size of the image used only to initialize the intrinsic camera matrix.
cameraMatrix Output 3x3 floating-point camera matrix A=⎡⎣⎢⎢⎢fx000fy0cxcy1⎤⎦⎥⎥⎥ . If CV_CALIB_USE_INTRINSIC_GUESS and/or CALIB_FIX_ASPECT_RATIO are specified, some or all of fx, fy, cx, cy must be initialized before calling the function.
distCoeffs Output vector of distortion coefficients (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) of 4, 5, 8, 12 or 14 elements.
rvecs Output vector of rotation vectors (see Rodrigues ) estimated for each pattern view (e.g. std::vector<cv::Mat>>). That is, each k-th rotation vector together with the corresponding k-th translation vector (see the next output parameter description) brings the calibration pattern from the model coordinate space (in which object points are specified) to the world coordinate space, that is, a real position of the calibration pattern in the k-th pattern view (k=0.. M -1).
tvecs Output vector of translation vectors estimated for each pattern view.
stdDeviationsIntrinsics Output vector of standard deviations estimated for intrinsic parameters. Order of deviations values: (fx,fy,cx,cy,k1,k2,p1,p2,k3,k4,k5,k6,s1,s2,s3,s4,τx,τy) If one of parameters is not estimated, it's deviation is equals to zero.
stdDeviationsExtrinsics Output vector of standard deviations estimated for extrinsic parameters. Order of deviations values: (R1,T1,…,RM,TM) where M is number of pattern views, Ri,Ti are concatenated 1x3 vectors.
perViewErrors Output vector of the RMS re-projection error estimated for each pattern view.
flags Different flags that may be zero or a combination of the following values:

  • CALIB_USE_INTRINSIC_GUESS cameraMatrix contains valid initial values of fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially set to the image center ( imageSize is used), and focal distances are computed in a least-squares fashion. Note, that if intrinsic parameters are known, there is no need to use this function just to estimate extrinsic parameters. Use solvePnP instead.
  • CALIB_FIX_PRINCIPAL_POINT The principal point is not changed during the global optimization. It stays at the center or at a different location specified when CALIB_USE_INTRINSIC_GUESS is set too.
  • CALIB_FIX_ASPECT_RATIO The functions considers only fy as a free parameter. The ratio fx/fy stays the same as in the input cameraMatrix . When CALIB_USE_INTRINSIC_GUESS is not set, the actual input values of fx and fy are ignored, only their ratio is computed and used further.
  • CALIB_ZERO_TANGENT_DIST Tangential distortion coefficients (p1,p2) are set to zeros and stay zero.
  • CALIB_FIX_K1,...,CALIB_FIX_K6 The corresponding radial distortion coefficient is not changed during the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  • CALIB_RATIONAL_MODEL Coefficients k4, k5, and k6 are enabled. To provide the backward compatibility, this extra flag should be explicitly specified to make the calibration function use the rational model and return 8 coefficients. If the flag is not set, the function computes and returns only 5 distortion coefficients.
  • CALIB_THIN_PRISM_MODEL Coefficients s1, s2, s3 and s4 are enabled. To provide the backward compatibility, this extra flag should be explicitly specified to make the calibration function use the thin prism model and return 12 coefficients. If the flag is not set, the function computes and returns only 5 distortion coefficients.
  • CALIB_FIX_S1_S2_S3_S4 The thin prism distortion coefficients are not changed during the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  • CALIB_TILTED_MODEL Coefficients tauX and tauY are enabled. To provide the backward compatibility, this extra flag should be explicitly specified to make the calibration function use the tilted sensor model and return 14 coefficients. If the flag is not set, the function computes and returns only 5 distortion coefficients.
  • CALIB_FIX_TAUX_TAUY The coefficients of the tilted sensor model are not changed during the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0.
criteria Termination criteria for the iterative optimization algorithm.

Returns

the overall RMS re-projection error.

The function estimates the intrinsic camera parameters and extrinsic parameters for each of the views. The algorithm is based on [206] and [17] . The coordinates of 3D object points and their corresponding 2D projections in each view must be specified. That may be achieved by using an object with a known geometry and easily detectable feature points. Such an object is called a calibration rig or calibration pattern, and OpenCV has built-in support for a chessboard as a calibration rig (see findChessboardCorners ). Currently, initialization of intrinsic parameters (when CALIB_USE_INTRINSIC_GUESS is not set) is only implemented for planar calibration patterns (where Z-coordinates of the object points must be all zeros). 3D calibration rigs can also be used as long as initial cameraMatrix is provided.

The algorithm performs the following steps:

  • Compute the initial intrinsic parameters (the option only available for planar calibration patterns) or read them from the input parameters. The distortion coefficients are all set to zeros initially unless some of CALIB_FIX_K? are specified.
  • Estimate the initial camera pose as if the intrinsic parameters have been already known. This is done using solvePnP .
  • Run the global Levenberg-Marquardt optimization algorithm to minimize the reprojection error, that is, the total sum of squared distances between the observed feature points imagePoints and the projected (using the current estimates for camera parameters and the poses) object points objectPoints. See projectPoints for details.

Note

If you use a non-square (=non-NxN) grid and findChessboardCorners for calibration, and calibrateCamera returns bad values (zero distortion coefficients, an image center very far from (w/2-0.5,h/2-0.5), and/or large differences between fx and fy (ratios of 10:1 or more)), then you have probably used patternSize=cvSize(rows,cols) instead of using patternSize=cvSize(cols,rows) in findChessboardCorners .

See also

findChessboardCorners, solvePnP, initCameraMatrix2D, stereoCalibrate, undistort

Reference

https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html

杂记

---

一次相机标定仅仅只是对物理相机模型的一次近似,再具体一点来说,一次标定仅仅是对相机物理模型在采样空间范围内的一次近似。所以当你成像物体所在的空间跟相机标定时的采样空间不一样的时候,你可能永远都没办法得到足够高的精度,当你大幅改变相机与成像物体的距离的时候,你最好重新标定相机。

通过摄像机标定我们可以知道些什么:
1.外参数矩阵。告诉你现实世界点(世界坐标)是怎样经过旋转和平移,然后落到另一个现实世界点(摄像机坐标)上。
2.内参数矩阵。告诉你上述那个点在1的基础上,是如何继续经过摄像机的镜头、并通过针孔成像和电子转化而成为像素点的。
3.畸变矩阵。告诉你为什么上面那个像素点并没有落在理论计算该落在的位置上,还tm产生了一定的偏移和变形!!!

总的来说,摄像机标定是通过寻找对象在图像与现实世界的转换数学关系,找出其定量的联系,从而实现从图像中测量出实际数据的目的。

---

每个标定板摆放的角度对应一个单应矩阵。然后每个矩阵根据旋转矩阵的单位正交性,可以构成2个约束,对应2个方程。

内参数矩阵中包含了5个自由度(驻点坐标u0,v0),相机焦距(fx,fy),X轴和Y轴垂直扭曲skew。因此至少3个单应关系,才可以求解,因此至少3个摆放的角度。

另外考虑畸变参数的建模,一般有4个,因此使用LM方法完成非线性优化。实际应用中摆放的角度20个,不同的角度能够保证目标函数更加接近凸函数,便于完成所有参数的迭代优化,使得结果更加准确。

---

通过镜头,一个三维空间中的物体经常会被映射成一个倒立缩小的像(当然显微镜是放大的,不过常用的相机都是缩小的),被传感器感知到。

理想情况下,镜头的光轴(就是通过镜头中心垂直于传感器平面的直线)应该是穿过图像的正中间的,但是,实际由于安装精度的问题,总是存在误差,这种误差需要用内参来描述;

理想情况下,相机对x方向和y方向的尺寸的缩小比例是一样的,但实际上,镜头如果不是完美的圆,传感器上的像素如果不是完美的紧密排列的正方形,都可能会导致这两个方向的缩小比例不一致。内参中包含两个参数可以描述这两个方向的缩放比例,不仅可以将用像素数量来衡量的长度转换成三维空间中的用其它单位(比如米)来衡量的长度,也可以表示在x和y方向的尺度变换的不一致性;

理想情况下,镜头会将一个三维空间中的直线也映射成直线(即射影变换),但实际上,镜头无法这么完美,通过镜头映射之后,直线会变弯,所以需要相机的畸变参数来描述这种变形效果。然后,说到为什么需要20张图片,这只是一个经验值,实际上太多也不好,太少也不好。单纯从统计上来看,可能越多会越好,但是,实际上图片太多可能会让参数优化的结果变差,因为棋盘格角点坐标的确定是存在误差的,而且这种误差很难说是符合高斯分布的,同时,标定过程所用的非线性迭代优化算法不能保证总是得到最优解,而更多的图片,可能会增加算法陷入局部最优的可能性。

---

总结一下就是:外参:另一个(相机或世界)坐标系->这个个相机坐标系,内参:这个相机坐标系->图像坐标系

实际应用中,个人经验是,单目标定板还是不要倾斜角度太大,尽量在摄像机视场范围内的所有地方都出现,远近也可以做一做,但不需要拉的太远。不知道有没有更好的操作,之前这么做误差还行,所以也就先这么搞着。

张正友的标定法论文里结果显示11幅图之后的效果都很好,标定板绕一个轴转动的角度在45度时最好。

转载自:https://blog.csdn.net/ywcpig/article/details/80760757

相机标定中部分疑问和注意事项相关推荐

  1. lm opencv 算法_相机模型与标定(七)--LM算法在相机标定中的使用

    LM算法在相机标定的应用共有三处. (1)单目标定或双目标定中,在内参固定的情况下,计算最佳外参.OpenCV中对应的函数为findExtrinsicCameraParams2. (2)单目标定中,在 ...

  2. 相机标定中各种标定板介绍以及优缺点分析

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 作者:Jakob W 编译:ronghuaiyang(AI 公园) 导读 各种标定板的解释和分析. 准 ...

  3. 机器视觉基础回顾:相机标定中的坐标系

    前一段尝试了相机标定,虽然做到一半放弃了,但感觉机器视觉很多东西仍然有相当大的必要去深入了解下.就像程序员多少也要懂点计算机硬件的东西,才能从底层思维的角度去优化代码. 所以留一些痕迹. 前置基础知识 ...

  4. MATLAB 相机标定中标定板角点像素坐标系到世界坐标系的转换

    matlab 做相机标定后,想将第一张(任意一张都行)标定板角点所对应的像素坐标转换到世界坐标系下,标定板角点的像素坐标真值与世界坐标真值都非常容易获得,但是我通过内外参矩阵将像素坐标转换到世界坐标有 ...

  5. 6、相机标定中的特殊棋盘格检测方法(真是啥需求都会有)

    摘要 1. 需求 2. 算法的步骤 3. 实现的效果 4. 部分代码 5. 非常感谢您的阅读! 6 期待您加入 1. 需求 我目前在做自动驾驶车辆上的各类传感器的标定问题.很容易理解,各类传感器就相当 ...

  6. kinectV1相机标定中的问题

    1.报错:does not match name narrow_stereo in filehome/cwq/.ros/camera_info/rgb_A00365A11688042A.yaml 更改 ...

  7. 相机标定中标定棋盘的角点是哪个?

      棋盘中每个黑方块的交点就是角点,如下图紫色圆圈处. 图源:https://blog.csdn.net/hx1298234467/article/details/50791007

  8. 相机标定中的相机焦距

    相机坐标系以相机的光心(小孔)作为原点 光心:就是凸透镜的中心: 焦点:平行于主光轴的光经凸透镜汇聚的点称为凸透镜的焦点.焦点到凸透镜光心的距离就是这个凸透镜的焦距. 相机焦距值与视角成反比:焦距长, ...

  9. 工业互联网(十四)——相机标定(Camera calibration)原理、步骤

    转载: 最详细.最完整的相机标定讲解 图像处理--相机标定(Camera calibration) 相机标定 相机标定(Camera calibration)原理.步骤 工业相机标定相关知识整理 相机 ...

最新文章

  1. [原]不祥的CPU——Alpha
  2. asp.net2.0学习历程 菜鸟到中级程序员的飞跃
  3. WSUS3.0的安装及部署(域下)
  4. java编程笔记18 文件压缩与解压缩
  5. 全文服务(Microsoft 搜索)不可用。系统管理员必须启动此服务
  6. 磁盘剩余空间策略_MySQL磁盘消耗迅猛掌握这点就够了,包你事半功倍
  7. 屏幕阅读器安全吗_如何为屏幕阅读器设计网站布局
  8. (原创)如何进行有符号小数乘法运算?(Verilog)
  9. 计算机设计思想 —— 解耦(分离)与内聚
  10. 基于WirёGuαrd和UDP speeder的网游加速实现方案
  11. 高频电子线路实验 03 - | 环形混频与直接调频
  12. android模拟触控power键
  13. mix3android auto,小米MIX3滑盖全面屏值不值得买?三天真实体验说说优缺点
  14. Hyperledger caliper 安装记录
  15. 苹果3D物体捕捉Object Capture功能实现教程
  16. 【Mysql】Mysql数据库查询“表空间”
  17. 计算机一级形状填充在哪里,ps填充快捷键,ps填充图案在哪怎么用?
  18. filename=文件夹名+filename (单个文件夹下的+Python)
  19. 8个好玩又实用的神奇网站,帮你打开新世界大门!
  20. 细谈渗透测试的前期工作——信息收集

热门文章

  1. 容器编排技术 -- Kubernetes kubectl create quota 命令详解
  2. maven构建java web项目(idea开发)
  3. 【静态站点(二)】之 Gridsome 基础
  4. 【Day03】使用原型最大的好处及原型链的理解
  5. mac chrome 打包扩展程序
  6. Coding:文件网盘高速直链下载无限空间
  7. 输入一个三位正整数,输出百位数,十位数,个位数
  8. C#开发笔记之17-如何用C#深克隆一个对象(传统方案)?
  9. 响应式编程 函数式编程_函数式编程的基本原理简介
  10. 我的布尔玛CSS框架之旅