当前位置:   article > 正文

鱼眼相机去畸变_opencv实现鱼眼畸变

opencv实现鱼眼畸变

在近一年的AVM算法开发工作中,鱼眼相机去畸变的玩法前前后后基本过了个遍。从最开始的调用Opencv API,到后来由于算法需要自己实现、正向的undis2fish、反向的fish2undis、鱼眼上检测、undis上标定,总之遇到很多坑,还好都解决了。正好最近有同学在AVM的帖子下面问这个东西的实现,今天在这里讨论一下。本帖从鱼眼相机模型开始讲起,包含Opencv API调参、基于畸变表的参数拟合、鱼眼相机去畸变算法原理和C++实现。

关键词:鱼眼相机、AVM全景环视系统

鱼眼相机基础

鱼眼相机模型

经过P点的入射光线没有透镜的话,本应交于相机成像平面的e点。然而,经过鱼眼相机的折射,光线会交于相机成像平面的d点,就产生了畸变,因此畸变图像整体上呈现出像素朝图像中心点聚集的态势。

而去畸变,就是将折射到d点的点,重新映射回到e点,因此去畸变之后的图像与原始的鱼眼图像相比,仿佛是把向心聚集的像素又重新向四周铺展开来。下表中的两幅图分别为鱼眼图和去畸变之后的展开图:

基于畸变表的拟合方法

每个相机都有它固定的相机参数,包含内参、畸变系数。可以使用特定的相机标定方法,得到这些相机参数。通常,我们可以使用相机在不同位置、不同角度对着标定板拍摄几十张照片,然后用某种优化方法,计算出相机参数的最优解,例如张正友棋盘格标定法。

然而,基于标定板的方法标定出的结果取决于光照是否充足、图像序列是否充足、全部的标定板序列是否能够充满整个图像幅面等因素。也就是说汽车标定的过程中需要人工摆放标定板的位置,指望算法工程师将产线上的工人培训得和他们一样专业显然不现实。大部分标定车间都是车开到标定场中间,十几秒标定出AVM系统所需要的参数,主要是4个鱼眼相机的外参,而非相机本身的参数(内参、畸变系数)。大概几十秒搞定一辆车,不可能有人工标定相机内参的过程。

好在相机在出厂时厂家一般都会提供相机的必要参数

  • 内参

{
"focal_length": 950,
"dx": 3,
"dy": 3,
"cx": 640,
"cy": 480
},

通过这些参数可以计算出内参矩阵:

"intrinsic" : [
316.66,
0.0,
640,
0.0,
316.66,
480,
0.0,
0.0,
1.0
]

内参计算公式:

 

  • 畸变表

 

  1. # load the distortion table and fit the distortion params
  2.   self.data = self.load_distortion_table(self.distortion_table_path)
  3.   self.data[:, 0= self.data[:, 0/ 180 * np.pi
  4.   self.data[:, 1= self.data[:, 1/ 0.91 #focal length = 0.91
  5.   self.distor_para, _ = curve_fit(self.func, self.data[:, 0], self.data[:, 1]) 

综上,我们通过曲线拟合的方法得到了畸变参数。

Opencv API 鱼眼图像去畸变方法

Opencv提供了基于Kannala-Brandt数学模型的鱼眼去畸变方法:cv::fisheye::initUndistortRectifyMap,该函数使用相机的内参和畸变参数计算出映射图mapx和mapy。

基础鱼眼图像去畸变

  1.    @param K Camera intrinsic matrix \f$cameramatrix{K}\f$.
  2.     @param D Input vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  3.     @param R Rectification transformation in the object space3x3 1-channel, or vector: 3x1/1x3
  4.     1-channel or 1x1 3-channel
  5.     @param P New camera intrinsic matrix (3x3or new projection matrix (3x4)
  6.     @param size Undistorted image size.
  7.     @param m1type Type of the first output map that can be CV_32FC1 or CV_16SC2 . See convertMaps()
  8.     for details.
  9.     @param map1 The first output map.
  10.     @param map2 The second output map.
  11.      */
  12.     CV_EXPORTS_W void initUndistortRectifyMap(InputArray K, InputArray D, InputArray R, InputArray P,
  13.         const cv::Size& size, int m1type, OutputArray map1, OutputArray map2);

那么问题来了,为什么既需要鱼眼相机的内参,又需要输出图像的相机内参呢,它们之间是什么关系呢?最开始的时候,很多同学肯定是把这两个相机内参设置成一样的,即都设置成鱼眼相机的大小,如下图所示。代码中去畸变之后图像的内参是从鱼眼相机内参深拷贝过来的。

  1. cv::Mat R = cv::Mat::eye(33, CV_32F);
  2. cv::Mat mapx_open, mapy_open;
  3. cv::Mat intrinsic_undis;
  4. fish_intrinsic.copyTo(intrinsic_undis);
  5. //intrinsic_undis.at<float>(0,2*= 2;
  6. //intrinsic_undis.at<float>(1,2*= 2;
  7. cv::fisheye::initUndistortRectifyMap(
  8.         fish_intrinsic, m_undis2fish_params, R, intrinsic_undis,
  9.         cv::Size(intrinsic_undis.at<float>(02* 2,
  10.                  intrinsic_undis.at<float>(12* 2),
  11.         CV_32FC1, mapx_open, mapy_open);
  12. cv::Mat test;
  13. cv::remap(disImg[3], test, mapx_open, mapy_open, cv::INTER_LINEAR);

 左侧为鱼眼图,右侧为去畸变图

相机主点参数调节

cv::Mat R = cv::Mat::eye(3, 3, CV_32F);
cv::Mat mapx_open, mapy_open;
cv::Mat intrinsic_undis;
fish_intrinsic.copyTo(intrinsic_undis);
intrinsic_undis.at<float>(0,2) *= 2;
intrinsic_undis.at<float>(1,2) *= 2;
cv::fisheye::initUndistortRectifyMap(
fish_intrinsic, m_undis2fish_params, R, intrinsic_undis,
cv::Size(intrinsic_undis.at<float>(0, 2) * 2,
intrinsic_undis.at<float>(1, 2) * 2),
CV_32FC1, mapx_open, mapy_open);
cv::Mat test;
cv::remap(disImg[3], test, mapx_open, mapy_open, cv::INTER_LINEAR);

 

去畸变图像相机参数的主点扩大了两倍,同时生成图像大小扩到两倍

从上图中我们依然不能获得到右侧完整的黑色大方格,因此需要进一步扩大去畸变后图像相机主点位置以及生成图像的分辨率:

  1. cv::Mat R = cv::Mat::eye(33, CV_32F);
  2. cv::Mat mapx_open, mapy_open;
  3. cv::Mat intrinsic_undis;
  4. fish_intrinsic.copyTo(intrinsic_undis);
  5. intrinsic_undis.at<float>(0,2*= 4;
  6. intrinsic_undis.at<float>(1,2*= 4;
  7. cv::fisheye::initUndistortRectifyMap(
  8.         fish_intrinsic, m_undis2fish_params, R, intrinsic_undis,
  9.         cv::Size(intrinsic_undis.at<float>(02* 2,
  10.                  intrinsic_undis.at<float>(12* 2),
  11.         CV_32FC1, mapx_open, mapy_open);
  12. cv::Mat test;
  13. cv::remap(disImg[3], test, mapx_open, mapy_open, cv::INTER_LINEAR);

 

现在我已经把去畸变图像相机内参的主点扩大为fish相机内参的4倍了,生成图像的长宽也放大了4倍,像素数量总体放大16倍,这样才勉强把大方格完全显示出来。我们知道提取角点需要用到图像处理算法,显然对这么大的图像做处理的效率非常低。

相机f参数调节

cv::Mat R = cv::Mat::eye(3, 3, CV_32F);
cv::Mat mapx_open, mapy_open;
cv::Mat intrinsic_undis;
fish_intrinsic.copyTo(intrinsic_undis);
intrinsic_undis.at<float>(0, 0) /= 4;
intrinsic_undis.at<float>(1, 1) /= 4;
/*intrinsic_undis.at<float>(0,2) *= 4;
intrinsic_undis.at<float>(1,2) *= 4;*/
cv::fisheye::initUndistortRectifyMap(
fish_intrinsic, m_undis2fish_params, R, intrinsic_undis,
cv::Size(intrinsic_undis.at<float>(0, 2) * 2,
intrinsic_undis.at<float>(1, 2) * 2),
CV_32FC1, mapx_open, mapy_open);
cv::Mat test;
cv::remap(disImg[3], test, mapx_open, mapy_open, cv::INTER_LINEAR);

 

左侧为鱼眼图,右侧为去畸变图,分辨率均为1280*960

从图中可以看出,当我们仅将相机焦距缩小时,可以看到更多的东西。虽然去畸变之后的图像很小只有1280*960,但是却可以看到完整的方格。

本节我们讨论了opencv API initUndistortRectifyMap函数的主点和f参数调节对于去畸变图像的影响,接下来的第3节,我们将会从去畸变算法原理入手,C++实现一波该算法。做这件事的原因很简单:opencv只提供了整张图像从undis2fish的映射,在avm的视角转换中,我们需要进行单个像素点的undis2fish,因此,我们需要自己实现一波这个去畸变过程

结论:缩小相机焦距可以使FOV增大,在更小分辨率的图像上呈现出更多的内容,看上去也是更加清晰。

鱼眼去畸变算法及其实现

畸变映射关系

鱼眼去畸变的算法实现就是遍历去畸变图像上的每一个点,寻找它们在鱼眼图像上的像素点坐标,计算它们之间的映射关系

C++实现:

  1. /*
  2. func: warp from distort to undistort
  3. @param f_dx:f/dx
  4. @param f_dy:f/dy
  5. @param large_center_h: undis image center y
  6. @param large_center_w: undis image center x
  7. @param fish_center_h: fish image center y
  8. @param fish_center_w: fish image center x
  9. @param undis_param: factory param
  10. @param x: input coordinate x on the undis image
  11. @param y: input coordinate y on the undis image
  12. */
  13. cv::Vec2f warpUndist2Fisheye(float fish_scale, float f_dx, float f_dy, float large_center_h,
  14. float large_center_w, float fish_center_h,
  15. float fish_center_w, cv::Vec4d undis_param, float x,
  16. float y) {
  17. f_dx *= fish_scale;
  18. f_dy *= fish_scale;
  19. float y_ = (y - large_center_h) / f_dy; // normalized plane
  20. float x_ = (x - large_center_w) / f_dx;
  21. float r_ = static_cast<float>(sqrt(pow(x_, 2) + pow(y_, 2)));
  22. // Look up table
  23. /*int num = atan(r_) / atan(m_d) * 1024;
  24. float angle_distorted = m_Lut[num];*/
  25. float angle_undistorted = atan(r_); // theta
  26. float angle_undistorted_p2 = angle_undistorted * angle_undistorted;
  27. float angle_undistorted_p3 = angle_undistorted_p2 * angle_undistorted;
  28. float angle_undistorted_p5 = angle_undistorted_p2 * angle_undistorted_p3;
  29. float angle_undistorted_p7 = angle_undistorted_p2 * angle_undistorted_p5;
  30. float angle_undistorted_p9 = angle_undistorted_p2 * angle_undistorted_p7;
  31. float angle_distorted = static_cast<float>(angle_undistorted +
  32. undis_param[0] * angle_undistorted_p3 +
  33. undis_param[1] * angle_undistorted_p5 +
  34. undis_param[2] * angle_undistorted_p7 +
  35. undis_param[3] * angle_undistorted_p9);
  36. // scale
  37. float scale = angle_distorted / (r_ + 0.00001f); // scale = r_dis on the camera img plane
  38. // divide r_undis on the normalized plane
  39. cv::Vec2f warp_xy;
  40. float xx = (x - large_center_w) / fish_scale;
  41. float yy = (y - large_center_h) / fish_scale;
  42. warpPointOpencv(warp_xy, fish_center_h, fish_center_w, xx, yy, scale);
  43. return warp_xy;
  44. }
  45. void warpPointOpencv(cv::Vec2f &warp_xy, float map_center_h, float map_center_w,
  46. float x_, float y_, float scale) {
  47. warp_xy[0] = x_ * scale + map_center_w;
  48. warp_xy[1] = y_ * scale + map_center_h;
  49. }

针对上述代码,我们由浅入深地讲述算法流程

基础的鱼眼去畸变(主点相关)

算法流程
1. 首先,对于图像平面上的像素点,要用相机的内参f、dx、dy,将其转化到归一化平面,对应上图中的e点。并计算其距离归一化平面中心的距离r_。并计算对应的入射角,即上图中的 theta角
2. 根据Kannala-Brandt的鱼眼模型公式,使用事先拟合的k1,k2,k3,k4参数计算归一化平面上去畸变之后点的位置r_distorted
3. 在归一化平面上计算去畸变前后点位置的比值: r_distorted/r_
4. 3中计算的比值为归一化平面上,同样可以应用到相机成像平面以及图像平面上。因此,可以对图像平面上的像素点,乘上这个系数,就得到了鱼眼图上像素点的位置。

对于算法流程中的3,4:

 

总体来讲这个基础的鱼眼去畸变算法的实现思路就是:在归一化平面上计算去畸变前后的像素坐标scale,然后运用到图像平面上。

 

从上图原始的鱼眼图中可以看出相机拍摄的内容中心大概在棋盘格附近,然而去畸变了之后棋盘格却跑到了左上角。这就是因为我们设置的主点偏左上,没有位于生成的去畸变图的中心。这就是2.2节中提到的:initUndistortRectifyMap函数中的size参数一般都是与去畸变之后图像的相机参数中主点大小强相关的。

进阶的 鱼眼去畸变(如何调整f)

正如第2节所说,我们需要在很小的图像上呈现出大方格。这就需要调整f,这个过程不太容易理解,我们画个图来理解一下:

 

Opencv API undistortPoints的实现

前面所有讨论的都是undis2fish的过程。在实际的AVM标定中,通常是对鱼眼相机检测角点,因为去畸变之后图像拉伸效果严重,提取的角点不准确。参考张正友标定法标定相机参数时,也是在获取到的图像上直接提取角点,解一个全局优化问题。whaosoft aiot http://143ai.com

 #forward
self.distor_para, _ = curve_fit(self.func, self.data[:, 0],self.data[:, 1])
#inverse
f_inverse_para, _ = curve_fit(self.func_inverse, self.data[:, 1], self.data[:, 0])

实现代码:

  1. cv::Vec2f CalibrateInit::warpFisheye2Undist(float fish_scale, float f_dx, float f_dy, float undis_center_h,
  2.     float undis_center_w, float fish_center_h,
  3.     float fish_center_w, cv::Vec4d undis_param, float x,
  4.     float y) {
  5.     // f_dx *= fish_scale;
  6.     // f_dy *= fish_scale;                        
  7.     float y_ = (y - fish_center_h) / f_dy;  // normalized plane
  8.     float x_ = (x - fish_center_w) / f_dx;
  9.     float r_distorted = static_cast<float>(sqrt(pow(x_, 2+ pow(y_, 2)));
  10.     float r_distorted_p2 = r_distorted * r_distorted;
  11.     float r_distorted_p3 = r_distorted_p2 * r_distorted;
  12.     float r_distorted_p4 = r_distorted_p2 * r_distorted_p2;
  13.     float r_distorted_p5 = r_distorted_p2 * r_distorted_p3;
  14.     float angle_undistorted = static_cast<float>(r_distorted +
  15.         undis_param[0* r_distorted_p2 +
  16.         undis_param[1* r_distorted_p3 +
  17.         undis_param[2* r_distorted_p4 +
  18.         undis_param[3* r_distorted_p5);
  19.     // scale
  20.     float r_undistorted = tanf(angle_undistorted);
  21.     float scale = r_undistorted / (r_distorted + 0.00001f);  // scale = r_dis on the camera img plane
  22.                                          // divide r_undis on the normalized plane
  23.     cv::Vec2f warp_xy;
  24.     float xx = (x - fish_center_w) * fish_scale;
  25.     float yy = (y - fish_center_h) * fish_scale;
  26.     warpPointInverse(warp_xy, undis_center_h, undis_center_w, xx, yy, scale);
  27.     return warp_xy;
  28. }
  29. void CalibrateInit::warpPointInverse(cv::Vec2f& warp_xy, float map_center_h, float map_center_w,
  30.     float x_, float y_, float scale) {
  31.     warp_xy[0= x_ * scale + map_center_w;
  32.     warp_xy[1= y_ * scale + map_center_h;
  33. }

总结

本贴讨论的内容为鱼眼相机图像基于畸变表的处理方法,AVM中畸变的运用非常灵活,所以笔者必须对它进行实现才可以灵活运用。据笔者所知有些AVM供应商的鱼眼畸变参数并不一定是依赖畸变表,有的也会拿来一批摄像头自行标定。具体那种方法更优,可能需要更多同行同学的实验和讨论得到结论。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/985945?site
推荐阅读
相关标签
  

闽ICP备14008679号