【问题标题】:Camera lens distortion in OpenGLOpenGL中的相机镜头失真
【发布时间】:2017-11-13 08:30:06
【问题描述】:

我正在尝试为我的 SLAM 项目模拟镜头失真效果。 已在 OpenGL 中给出并加载了扫描的彩色 3D 点云。 我想做的是在给定的姿势下渲染 2D 场景,并在鱼眼相机的真实图像和渲染图像之间做一些视觉里程计。 由于相机镜头畸变严重,在渲染阶段也要考虑。

问题是我不知道把镜头畸变放在哪里。着色器?

我发现some open codes 将失真放入几何着色器中。但是我猜这个畸变模型与计算机视觉社区中的镜头畸变模型不同。在 CV 社区中,镜头畸变通常发生在投影平面上。

This one 与我的工作非常相似,但他们没有使用失真模型。

谁有好主意?

我刚刚找到another implementation。他们的代码在片段着色器和几何着色器中都实现了失真。但是片段着色器版本可以应用于我的情况。因此,我想以下方法会起作用:

# vertex shader
p'=T.model x T.view x p
p_f = FisheyeProjection(p') // custom fish eye projection

【问题讨论】:

  • 另一种经常使用的方法是渲染到纹理,然后将其映射到矩形上,然后扭曲其形状

标签: opengl camera shader distortion fisheye


【解决方案1】:

受 VR 社区的启发,我通过顶点置换实现了变形。 对于高分辨率,这在计算上更有效,但需要具有良好顶点密度的网格。您可能希望在扭曲图像之前应用曲面细分。

这里是实现 OpenCV 有理失真模型的代码(公式见https://docs.opencv.org/4.0.1/d9/d0c/group__calib3d.html):

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal_in;
layout (location = 2) in vec2 texture_coordinate_in;
uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform float dist_coeffs[8];
uniform mat4 projection_matrix;
uniform vec3 light_position;
out vec2 texture_coordinate;
out vec3 normal;
out vec3 light_direction;

// distort the real world vertices using the rational model
vec4 distort(vec4 view_pos)
{
  // normalize
  float z = view_pos[2];
  float z_inv = 1 / z;
  float x1 = view_pos[0] * z_inv;
  float y1 = view_pos[1] * z_inv;
  // precalculations
  float x1_2 = x1*x1;
  float y1_2 = y1*y1;
  float x1_y1 = x1*y1;
  float r2 = x1_2 + y1_2;
  float r4 = r2*r2;
  float r6 = r4*r2;
  // rational distortion factor
  float r_dist = (1 + dist_coeffs[0]*r2 +dist_coeffs[1]*r4 + dist_coeffs[4]*r6) 
    / (1 + dist_coeffs[5]*r2 + dist_coeffs[6]*r4 + dist_coeffs[7]*r6);
  // full (rational + tangential) distortion
  float x2 = x1*r_dist + 2*dist_coeffs[2]*x1_y1 + dist_coeffs[3]*(r2 + 2*x1_2);
  float y2 = y1*r_dist + 2*dist_coeffs[3]*x1_y1 + dist_coeffs[2]*(r2 + 2*y1_2);
  // denormalize for projection (which is a linear operation)
  return vec4(x2*z, y2*z, z, view_pos[3]);
}

void main()
{
  vec4 local_pos = vec4(position, 1.0);
  vec4 world_pos  =  model_matrix * local_pos;
  vec4 view_pos = view_matrix * world_pos;
  vec4 dist_pos = distort(view_pos);
  gl_Position = projection_matrix * dist_pos;
  // lighting on world coordinates not distorted ones
  normal = mat3(transpose(inverse(model_matrix))) * normal_in;
  light_direction = normalize(light_position - world_pos.xyz);
  texture_coordinate = texture_coordinate_in;
}

请务必注意,失真是在 z 归一化坐标中计算的,但在失真的最后一行中被非归一化为视图坐标。这允许使用像这篇文章中的投影矩阵:http://ksimek.github.io/2013/06/03/calibrated_cameras_in_opengl/

编辑:对于任何有兴趣在上下文中查看代码的人,我已经在一个小的library 中发布了代码,this example 中使用了失真着色器。

【讨论】:

  • 你好,伙计,我在我的着色器中实现了代码,它的工作非常好,除了当我离对象太近时,有时我会得到奇怪的伪影。这就像当一个顶点超出某个域时,它会被错误地投影。
  • 你是否离你的对象比截头锥允许的更近?
  • 不,这不是截锥体问题,因为当我进行不失真的投影时,没有任何问题。
  • 我知道 OpenCV 的 projectPoints 有其局限性,有时当未失真点的投影超出图像周围的某个椭圆时,失真会产生奇怪的结果。所以我认为这是同样的问题。
【解决方案2】:

镜头畸变通常会将直线变成曲线。然而,当使用 OpenGL 栅格化线和三角形时,无论您如何变换顶点,图元的边缘都保持笔直。

如果您的模型具有足够精细的曲面细分,那么将失真合并到顶点变换中是可行的。如果您只渲染点,它也可以工作。

但是,当您的目标是普遍适用性时,您必须以某种方式处理直边基元。一种方法是使用几何着色器进一步细分传入的模型;或者您可以使用镶嵌着色器。

另一种方法是渲染成立方体贴图,然后使用着色器为其创建等效镜头。我实际上建议使用它来生成鱼眼图像。

畸变本身通常由 3 到 5 阶多项式表示,将未畸变的光学中心轴角距离映射到畸变角距离。

【讨论】:

  • 嗨!感谢您的想法。我正在使用基于面元的地图表示,其中每个点都有一个固定半径约为 1 厘米或更小的圆盘,足够小。所以,我想你的第一个解决方案没有细分它们对我有用。第二个想法听起来也不错。我会检查立方体贴图。谢谢!
猜你喜欢
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
相关资源
最近更新 更多