跳转到内容

镜头投影与畸变:从直线投影到鱼眼,以及哪种标定模型适用

每只镜头都把世界中的方向映射到传感器上的位置。这种映射就是投影(projection)。标定模型是描述它的一个公式,带有少数几个由标定去拟合的自由参数。选择模型,就是选择一个形状与镜头的物理行为相符的公式。

本页讲的是镜头在设计上遵循的各种投影、真实镜头偏离它们的方式,以及哪种标定模型适用于哪种镜头。它建立在镜头如何成像和相机标定指南的基础上。

大多数镜头是旋转对称的,所以投影可以用两个数来描述:

  • θ(theta):入射光线与光轴之间的夹角;
  • r:从主点到该光线在传感器上落点的距离。

投影函数(projection function) r(θ) 说明角度为 θ 的光线落在离中心多远的地方。镜头在设计上遵循少数几种经典函数之一,焦距 f 在每种函数中都是比例因子。

投影 公式 它保持什么 θ = 60° 时的 r/f θ = 90° 时的 r/f
透视(直线投影,针孔) r = f · tan(θ) 直线 1.73 无穷大
等距(f-theta) r = f · θ 每度对应相等的径向距离 1.05 1.57
等立体角(Equisolid angle) r = 2f · sin(θ/2) 每单位立体角对应相等的图像面积 1.00 1.41
体视(Stereographic) r = 2f · tan(θ/2) 局部形状和角度 1.15 2.00
正交(Orthographic) r = f · sin(θ) (对边缘压缩得最厉害) 0.87 1.00

公式中的 θ 以弧度计。Commonlands 的鱼眼指南列出了同样的五种,并指出直线投影 “preserves straight lines but cannot reach 180°”(意为:保持直线,但无法达到 180°),等距投影 “holds the radial scale constant for angular measurement”(意为:使径向比例保持恒定,便于角度测量),等立体角投影 “maps equal solid angles on the viewing sphere to equal image areas”(意为:把视球上相等的立体角映射为相等的图像面积),体视投影 “preserves local shapes and angles”(意为:保持局部形状和角度),而正交投影 “rare in machine vision”(意为:在机器视觉中很少见)。

Projection functions: image radius against ray angle A plot of image radius r, in units of the focal length f, against the angle theta between an incoming ray and the optical axis, from 0 to 100 degrees. The perspective curve r = f tan theta climbs steeply and goes to infinity at 90 degrees. The fisheye curves stay finite: stereographic r = 2f tan(theta/2) reaches 2f at 90 degrees, equidistant r = f theta is a straight line reaching about 1.57f, equisolid r = 2f sin(theta/2) reaches about 1.41f, and orthographic r = f sin theta flattens to f at 90 degrees. All five agree near the axis. 0°20°40°60°80°100°0123 angle of the ray off the optical axis, θ image radius r, in units of f perspectivestereographicequidistantequisolidorthographic
How far from the image centre a ray lands (r, in units of f) for a ray arriving θ off the axis. The perspective (pinhole) curve r = f·tan θ runs to infinity at 90°, so no rectilinear lens can reach a 180° view. The fisheye projections stay finite. All five agree near the axis, which is why a narrow lens looks like a pinhole.

在光轴附近,五种投影是一致的,因为对于小角度,tan θ、θ、2 sin(θ/2)、2 tan(θ/2) 和 sin θ 都接近 θ。这就是窄视场镜头可以按针孔加一个小修正来标定的原因。超过约 30° 之后,它们很快就分道扬镳。

透视投影让直线保持为直线,但在大角度时代价很高。它的像高(图像半径)按 tan θ 增长,在 90° 处趋于无穷大。沿半径方向的图像比例按 f / cos²θ 增长:在离轴 60° 处,每一度占用的像素是中心处的四倍。覆盖很宽视场的直线投影镜头必须把大部分传感器花在边缘上,而且它根本达不到 180°。

所以镜头设计者用直线换取覆盖范围。鱼眼镜头有意遵循其他投影之一。Commonlands 说得很明白:“The curved-line rendering in a fisheye image is a geometric consequence of the projection, not a manufacturing defect.”(意为:鱼眼图像中把直线呈现为曲线,是投影的几何结果,不是制造缺陷。)

这也是针孔模型加畸变多项式在鱼眼镜头上不再适用的原因。Usenko、Demmel 和 Cremers 在比较相机模型时写道:“in practice, even when distortion model is added the pinhole camera demonstrates suboptimal performance for a field-of-view greater than 120°.”(意为:在实践中,即使加上畸变模型,针孔相机在视场角大于 120° 时也表现欠佳。)

畸变(distortion)是光线实际落点与理想投影给出的落点之间的差别。它移动像点,但不使像点模糊。对于设计上为直线投影的镜头,畸变通常以百分比给出:

distortion (%) = (r_actual − r_ideal) / r_ideal × 100

其中 r_ideal = f · tan(θ)。负值表示图像向边缘被压缩;正值表示被拉伸。

Three shapes of radial distortion The same square grid imaged through three lenses. Barrel distortion: image magnification falls toward the edge, so lines bow outward. Pincushion distortion: magnification rises toward the edge, so lines bow inward. Mustache distortion: barrel near the centre turning to pincushion near the edge, so lines wave. Drawn with the radial model x_d = x(1 + k1 r^2 + k2 r^4): k1 negative for barrel, k1 positive for pincushion, k1 negative with k2 positive for mustache. Barrel Pincushion Mustache k1 < 0 k1 > 0 k1 < 0, k2 > 0 lines bow outward lines bow inward lines wave
The same square grid through three kinds of radial distortion, drawn with the model x_d = x(1 + k1·r² + k2·r⁴). Barrel (k1 < 0) bows lines outward, pincushion (k1 > 0) bows them inward, and mustache (k1 < 0 with k2 > 0) turns from barrel near the centre to pincushion toward the edge.
  • 桶形畸变(barrel distortion):放大率随离中心的距离增大而下降。偏离中心的直线向外弯。Commonlands 指出 “Residual barrel in a rectilinear wide-angle lens and designed barrel in a fisheye both grow with field angle.”(意为:直线投影广角镜头中残余的桶形畸变,和鱼眼镜头中设计出来的桶形畸变,都随视场角增大而增长。)
  • 枕形畸变(pincushion distortion):放大率随离中心的距离增大而上升。直线向内弯。
  • 胡子形畸变(mustache distortion):中心附近是桶形,到边缘转向枕形,所以直线呈波浪状。在下文的径向模型中,它表现为负的 k1 加正的 k2。

经典光学用孔径光阑(aperture stop)的位置解释前两种。光阑位于正透镜前方,往往产生桶形畸变;位于其后方,则产生枕形畸变。Commonlands 指出,常规镜头中的桶形畸变 “is classified as a third-order (Seidel) transverse aberration”(意为:被归类为三阶(赛德尔)横向像差),而 “In fisheye lenses the curvature is the intended projection mapping, far beyond the third-order regime, not an aberration.”(意为:在鱼眼镜头中,这种弯曲是有意设计的投影映射,远远超出三阶范围,不是像差。)

径向畸变(radial distortion)使点沿着从主点出发的直线移动,移动量只取决于点到主点的距离。它来自镜头设计本身,所以关于光轴对称。OpenCV 文档说真实镜头通常有 “mostly radial distortion, and slight tangential distortion.”(意为:主要是径向畸变,以及轻微的切向畸变。)

切向(偏心)畸变(tangential / decentering distortion)使点部分地向侧面移动。它来自制造和装配:各镜片的中心不完全位于同一条轴上,或者镜头没有完全正对传感器。OpenCV 教程把它描述为之所以出现是 “because the image-taking lense is not aligned perfectly parallel to the imaging plane.”(意为:因为拍摄镜头没有与成像平面完全平行对准。)Duane Brown 1966 年的论文 Decentering distortion of lenses 是这方面的经典论述。

两种较小的效应在 OpenCV 中有各自的项:

  • 薄棱镜畸变(thin-prism distortion)(系数 s1 到 s4):进一步的非对称项,用于描述径向项和切向项没有解释掉的部分。
  • 传感器倾斜(sensor tilt)(τx、τy):OpenCV 对相对于镜头倾斜的传感器建模,并指出 “The tilt causes a perspective distortion”(意为:倾斜会造成透视畸变)。

真实镜头的像高总是随光线角度的增大而增大。拟合出的模型如果自己折返回来,就是错的,即使它的误差看起来很小。OpenCV 文档警告说:“Radial distortion is always monotonic for real lenses, and if the estimator produces a non-monotonic result, this should be considered a calibration failure.”(意为:真实镜头的径向畸变总是单调的,如果估计器给出非单调的结果,应当视为标定失败。)它还说,这样的结果 “may look deceptively good near the image center but will work poorly in e.g. AR/SFM applications.”(意为:在图像中心附近可能看起来好得具有欺骗性,但在例如 AR/SFM 应用中效果会很差。)出现这种情况的一种途径:如果标定板从未到达图像的角落,那里的高阶项就没有任何约束。

每种标定模型都包含一个关于物理投影的假设。在下面的每个模型中,fx、fy、cx 和 cy 是以像素计的焦距和主点,(x, y, z) 是相机坐标系中的点。

针孔加径向–切向畸变(Brown–Conrady,“radtan”)

Section titled “针孔加径向–切向畸变(Brown–Conrady,“radtan”)”

假设:镜头接近直线投影,带一个小的径向修正和一个小的偏心修正。先用针孔投影,x′ = x/z,y′ = y/z,然后令 r² = x′² + y′²:

x″ = x′ · (1 + k1·r² + k2·r⁴ + k3·r⁶) + 2·p1·x′·y′ + p2·(r² + 2·x′²)
y″ = y′ · (1 + k1·r² + k2·r⁴ + k3·r⁶) + p1·(r² + 2·y′²) + 2·p2·x′·y′
u = fx · x″ + cx
v = fy · y″ + cy

k 项是径向的,p 项是切向的。OpenCV 的完整版本给径向因子加了一个有理分母 (1 + k4·r² + k5·r⁴ + k6·r⁶),再加上薄棱镜项和倾斜项。Kalibr 把这个模型叫作 radtan,系数为 [k1 k2 r1 r2](它的 r1 和 r2 是切向项)。因为它从 x/z 开始,所以无法表示离轴 90° 或超过 90° 的光线。

普通镜头和中等广角镜头选用它。mrcal 文档报告说,8 参数的有理模型 “works decently well for wide lenses”(意为:对广角镜头效果相当不错)。它说,对于非鱼眼镜头,它的 4 参数和 5 参数 OpenCV 模型 “work ok”(意为:效果尚可)。

Kannala–Brandt(“equidistant”,OpenCV fisheye)

Section titled “Kannala–Brandt(“equidistant”,OpenCV fisheye)”

假设:像高是光线角度 θ 的多项式,从等距投影出发。按 OpenCV 的 fisheye 模块和双球面模型论文所用的形式:

θ = atan2( √(x² + y²), z )
d(θ) = θ + k1·θ³ + k2·θ⁵ + k3·θ⁷ + k4·θ⁹
u = fx · d(θ) · x / √(x² + y²) + cx
v = fy · d(θ) · y / √(x² + y²) + cy

所有 k = 0 时,它是纯等距镜头。这个奇次多项式也可以弯曲去拟合等立体角和体视镜头,所以一个模型覆盖了大多数鱼眼镜头。Kannala and Brandt (2006) 提出它时称其 “suitable for fish-eye lens cameras as well as for conventional and wide-angle lens cameras”(意为:既适用于鱼眼镜头相机,也适用于常规和广角镜头相机)。Kalibr 把它叫作 equi;OpenCV 在 cv::fisheye 中实现它。

有一个实现细节对超广角镜头很重要。Usenko 等人指出,OpenCV 和 Kalibr 是在针孔投影之后才应用它的,所以它在 z = 0 处,也就是离轴 90° 处有一个奇点,“which makes it unsuitable for fisheye lenses with field-of-view close to 180°”(意为:这使它不适用于视场角接近 180° 的鱼眼镜头)。反投影还需要迭代求解,因为这个多项式没有闭式逆。

假设:点先被投影到一个单位球面上,然后通过一个针孔投影到图像上,该针孔从球心后移了一段距离 ξ(xi)。这个模型是为折反射(catadioptric)相机建立的,这种相机把镜头与曲面反射镜组合在一起。在 Usenko 等人提出的参数化形式中,令 d = √(x² + y² + z²),α 取值于 [0, 1]:

u = fx · x / (α·d + (1 − α)·z) + cx
v = fy · y / (α·d + (1 − α)·z) + cy

α = 0 时它退化为针孔模型。它也可以对鱼眼镜头建模,但 Usenko 等人指出 “it does not fit most fisheye lenses perfectly; thus, an additional distortion model is often added.”(意为:它不能完美拟合大多数鱼眼镜头;因此常常要再加一个畸变模型。)Kalibr 的 omni 模型和 OpenCV 的 cv::omnidir 模块都遵循 Mei and Rives (2007),通常与 radtan 畸变搭配使用。

假设:与统一模型类似,但球面被一个椭球面取代,由第二个参数 β 控制。投影与上面相同,只是:

d = √( β·(x² + y²) + z² )

β = 1 时它又回到统一模型。EUCM 有两个类似畸变的参数(α、β),有闭式逆,不含三角函数,这使它很快。它由 Khomutenko、Garcia 和 Martinet 提出,Kalibr 以 eucm 支持它。

假设:点依次被投影到两个单位球面上,两球相互偏移 ξ,然后通过一个偏移了 α/(1 − α) 的针孔投影。令 d1 = √(x² + y² + z²),d2 = √(x² + y² + (ξ·d1 + z)²):

u = fx · x / (α·d2 + (1 − α)·(ξ·d1 + z)) + cx
v = fy · y / (α·d2 + (1 − α)·(ξ·d1 + z)) + cy

Usenko、Demmel 和 Cremers 把它描述为这样一个模型:“that well fits with large field-of-view lenses, is computationally inexpensive and has a closed-form inverse”(意为:与大视场角镜头拟合良好,计算开销小,并且有闭式逆)。它总共有六个参数。Kalibr 以 ds 支持它。

假设:图像上的距离大致与光线角度成正比,带一个参数 w。按 Usenko 等人的记号,令 r_u = √(x² + y²):

r_d = atan2( 2·r_u·tan(w/2), z ) / w
u = fx · r_d · x / r_u + cx
v = fy · r_d · y / r_u + cy

它出自 Devernay and Faugeras (2001), Straight lines have to be straight。Usenko 等人写道,按其作者的说法,参数 w “approximately corresponds to the field-of-view of an ideal fisheye lens”(意为:近似对应于一只理想鱼眼镜头的视场角)。Kalibr 把它作为名为 fov 的畸变模型提供。它只有一个参数,是各鱼眼模型中约束最强的。

当最好的参数模型在残差中仍然留下规律性的图案时,有些工具改为拟合一个灵活的曲面。mrcal 的样条体视模型是 “mrcal’s attempt to model real-world lens behavior with more fidelity than the usual parametric models make possible.”(意为:mrcal 的一次尝试,要以比通常的参数模型所能达到的更高保真度,对真实镜头的行为建模。)这类模型需要标定板在整幅图像上的密集覆盖,因为样条只在有数据的地方受到约束。

Usenko 等人用上述每一种模型标定了视场角从 122° 到 195° 的五只镜头。在他们的 195° 镜头上(第一个序列),平均重投影误差为:

模型 参数个数 平均重投影误差
统一模型(UCM) 5 0.236 px
FOV 5 0.417 px
双球面模型 6 0.145 px
EUCM 6 0.145 px
Kannala–Brandt,2 个系数 6 0.164 px
Kannala–Brandt,4 个系数 8 0.145 px

他们的总结:“The evaluation results demonstrate that the model based on high-order polynomials (i.e., KB 8) shows the lowest reprojection error but is 5-10 times slower than competing models.”(意为:评估结果表明,基于高阶多项式的模型(即 KB 8)重投影误差最低,但比其他竞争模型慢 5-10 倍。)双球面模型在每个序列上与它的差距都在 1% 以内,EUCM 在 2% 以内。

拟合也可能彻底失败。BabelCalib 的作者在鱼眼和折反射数据集上测试后报告说 “Kalibr and OpenCV have both a 24% failure rate for the Kannala-Brandt model, and Kalibr has a 34% failure rate for FOV and 51% failure rate for EUCM.”(意为:对于 Kannala-Brandt 模型,Kalibr 和 OpenCV 的失败率都是 24%;Kalibr 对 FOV 的失败率为 34%,对 EUCM 的失败率为 51%。)他们的方法避开了这个问题:先拟合一个不需要初始猜测的更简单的反向投影模型,再把它转换成用户想要的模型。广角标定需要做合理性检查,而不只是看一个最终的误差数字。

镜头 物理行为 起步用的模型 在哪里找到它 备注
普通视场角 接近直线投影,轻微桶形 针孔 + radtan(k1、k2、p1、p2,也许加 k3) OpenCV 默认;Kalibr pinhole + radtan 只有角落需要时才加 k3。
广角直线投影,到约 120° 桶形强烈,设计上仍是直线投影 针孔 + 有理 radtan(k1–k6)或 Kannala–Brandt 打开有理模型标志的 OpenCV;mrcal OPENCV8;Kalibr pinhole + equi 检查结果是否单调。
鱼眼,超过约 120° 而不到 180° 等距、等立体角或体视 Kannala–Brandt(4 个系数)、双球面模型或 EUCM OpenCV cv::fisheye;Kalibr pinhole + equi、ds、eucm DS 和 EUCM 的投影和反投影更快;在 Usenko 等人的结果中 KB 拟合得最好。
鱼眼,接近或超过 180° 光线位于镜头平面上或其后方 双球面模型、EUCM 或统一模型 + 畸变 Kalibr ds、eucm、omni + radtan;OpenCV cv::omnidir 避开先除以 z 的实现。
折反射(镜头 + 反射镜) 通过反射镜实现单一视点 统一模型(Mei)+ radtan Kalibr omni + radtan;OpenCV cv::omnidir 统一模型就是为它建立的。
任何镜头,最高保真度 残差显示什么就是什么 样条模型 mrcal 样条体视模型 需要对整幅图像的密集覆盖。

无论什么镜头,有三条规则都成立。

  1. 使用你的处理流程所读取的模型。一个预期 Kannala–Brandt 参数的 SLAM 系统或数据集格式,从一个它读不了的模型的更好拟合中得不到任何好处。
  2. 根据整幅图像上的残差评判拟合,而不是根据一个 RMS 数字。角落残差中的规律性图案意味着模型跟不上镜头。
  3. 覆盖边缘。畸变最大的地方,正是标定板最难摆到的地方。只在中心拟合的模型会外推,而高阶多项式外推得很差。