OpenCV相机标定及距离估计(单目)

OpenCV相机标定及距离估计(单⽬)
相机标定基本知识
对于针孔摄像机模型,⼀幅视图是通过透视变换将三维空间中的点投影到图像平⾯。投影公式如下:
或者
这⾥(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(objectPoints,
imagePoints,
imageSize,
cameraMatrix,
distCoeffs,
rvecs,
tvecs,
stdDeviationsIntrinsics,
stdDeviationsExtrinsics,
perViewErrors,
int flags = 0,
criteria = (+, 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>>).
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  and  . 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:
it运维系统详细设计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
硅胶气囊, , , ,
Reference
杂记
---
⼀次相机标定仅仅只是对物理相机模型的⼀次近似,再具体⼀点来说,⼀次标定仅仅是对相机物理模型在采样空间范围内的⼀次近似。所以当你成像物体所在的空间跟相机标定时的采样空间不⼀样的时候,你可能永远都没办法得到⾜够⾼的精度,当你⼤幅改变相机与成像物体的距离的时候,你最好重新标定相机。
通过摄像机标定我们可以知道些什么:
手机绑定
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度时最好。

本文发布于:2024-09-25 06:28:29,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/295058.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:参数   矩阵   图像   标定   摄像机   镜头   优化   直线
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议