鱼眼相机模型经过P点的入射光线没有透镜的话,本应交于相机成像平面的e点。然而,经过鱼眼相机的折射,光线会交于相机成像平面的d点,就产生了畸变,因此畸变图像整体上呈现出像素朝图像中心点聚集的态势。而去畸变,就是将折射到d点的点,重新映射回到e点,因此去畸变之后的图像与原始的鱼眼图像相比,仿佛是把向心聚集的像素又重新向四周铺展开来。下表中的两幅图分别为鱼眼图和去畸变之后的展开图:
鱼眼相机的投影方式有很多种假设,例如等距投影、等立体角投影、正交投影、体视投影、线性投影。但是真实的鱼眼相机镜头并不完全遵循上述的这些模型假设。因此Kannala-Brandt提出了一种一般形式的估计,适用于不同类型的鱼眼相机:
,这个也是纳入opencv中的鱼眼相机畸变模型。现在基本上默认鱼眼相机模型遵循上述公式。公式中的 为光线入射角,r()为上图中 od 的长度。
中间矩阵为内参计算公式
畸变表通过畸变表可以得到畸变前后像素坐标的映射关系:其中angle表示光线的入射角 ,Real_Image_Height表示入射光线经过鱼眼相机透镜折射(出射角为 )与成像平面的交点(畸变点)。opencv Kannala-Brandt模型与畸变表之间的差异为:
具体的实现方法可以使用python的curve_fit函数,即可拟合出合适的k0,k1,k2,k3,k4系数。上面提到opencv Kannala-Brandt与厂家给的畸变表之间相差一个系数:focal_length,因此,在做曲线拟合的时候,要把这部分考虑进去:theta_input = data[:,0]*3.14/180 theta_fit = np.arctan(self.data[:,1]/0.95) #focal_lenth=0.95 distort_data, _ = curve_fit(func1, theta_input, theta_fit) 综上,我们通过曲线拟合的方法得到了畸变参数。
@param K Camera intrinsic matrix f$cameramatrix{K}f$. @param D Input vector of distortion coefficients f$distcoeffsfisheyef$. @param R Rectification transformation in the object space: 3x3 1-channel, or vector: 3x1/1x3 1-channel or 1x1 3-channel @param P New camera intrinsic matrix (3x3) or new projection matrix (3x4) @param size Undistorted image size. @param m1type Type of the first output map that can be CV_32FC1 or CV_16SC2 . See convertMaps() for details. @param map1 The first output map. @param map2 The second output map. */ CV_EXPORTS_W void initUndistortRectifyMap(InputArray K, InputArray D, InputArray R, InputArray P, const cv::Size& size, int m1type, OutputArray map1, OutputArray map2); 相机内参矩阵表示如下,其中 表示相机焦距 f 与相机cmos参数 的比值,这个 的物理意义为每个像素的实际长度,单位可以是mm/像素。 表示相机主点,即光心与图像平面相交的坐标,单位为像素。
那么问题来了,为什么既需要鱼眼相机的内参,又需要输出图像的相机内参呢,它们之间是什么关系呢?最开始的时候,很多同学肯定是把这两个相机内参设置成一样的,即都设置成鱼眼相机的大小,如下图所示。代码中去畸变之后图像的内参是从鱼眼相机内参深拷贝过来的。cv::Mat R = cv::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::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);
左侧为鱼眼图,右侧为去畸变图cv::Mat R = cv::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::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);
去畸变图像相机参数的主点扩大了两倍,同时生成图像大小扩到两倍从上图中我们依然不能获得到右侧完整的黑色大方格,因此需要进一步扩大去畸变后图像相机主点位置以及生成图像的分辨率:cv::Mat R = cv::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) *= 4; intrinsic_undis.at<float>(1,2) *= 4; cv::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);
现在我已经把去畸变图像相机内参的主点扩大为fish相机内参的4倍了,生成图像的长宽也放大了4倍,像素数量总体放大16倍,这样才勉强把大方格完全显示出来。我们知道提取角点需要用到图像处理算法,显然对这么大的图像做处理的效率非常低。cv::Mat R = cv::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::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++实现:/* func: warp from distort to undistort @param f_dx:f/dx @param f_dy:f/dy @param large_center_h: undis image center y @param large_center_w: undis image center x @param fish_center_h: fish image center y @param fish_center_w: fish image center x @param undis_param: factory param @param x: input coordinate x on the undis image @param y: input coordinate y on the undis image */ cv::Vec2f warpUndist2Fisheye(float fish_scale, float f_dx, float f_dy, float large_center_h, float large_center_w, float fish_center_h, float fish_center_w, cv::Vec4d undis_param, float x, float y) { f_dx *= fish_scale; f_dy *= fish_scale; float y_ = (y - large_center_h) / f_dy; // normalized plane float x_ = (x - large_center_w) / f_dx; float r_ = static_cast<float>(sqrt(pow(x_, 2) + pow(y_, 2))); // Look up table /*int num = atan(r_) / atan(m_d) * 1024; float angle_distorted = m_Lut[num];*/ float angle_undistorted = atan(r_); // theta float angle_undistorted_p2 = angle_undistorted * angle_undistorted; float angle_undistorted_p3 = angle_undistorted_p2 * angle_undistorted; float angle_undistorted_p5 = angle_undistorted_p2 * angle_undistorted_p3; float angle_undistorted_p7 = angle_undistorted_p2 * angle_undistorted_p5; float angle_undistorted_p9 = angle_undistorted_p2 * angle_undistorted_p7; float angle_distorted = static_cast<float>(angle_undistorted + undis_param[0] * angle_undistorted_p3 + undis_param[1] * angle_undistorted_p5 + undis_param[2] * angle_undistorted_p7 + undis_param[3] * angle_undistorted_p9); // scale float scale = angle_distorted / (r_ + 0.00001f); // scale = r_dis on the camera img plane // divide r_undis on the normalized plane cv::Vec2f warp_xy; float xx = (x - large_center_w) / fish_scale; float yy = (y - large_center_h) / fish_scale; warpPointOpencv(warp_xy, fish_center_h, fish_center_w, xx, yy, scale); return warp_xy; } void warpPointOpencv(cv::Vec2f &warp_xy, float map_center_h, float map_center_w, float x_, float y_, float scale) { warp_xy[0] = x_ * scale + map_center_w; warp_xy[1] = y_ * scale + map_center_h; } 针对上述代码,我们由浅入深地讲述算法流程
鱼眼相机成像模型上述代码中令fish_scale为1,先讨论最简单的,即让去畸变图像相机参数中的, 大小与鱼眼图相同,对照鱼眼相机模型这张图和代码,我们来梳理一下算法流程:算法流程
总体来讲这个基础的鱼眼去畸变算法的实现思路就是:在归一化平面上计算去畸变前后的像素坐标scale,然后运用到图像平面上。
主点位置示意图如上图所示,输出的去畸变图大小为Size,去畸变图相机内参中的主点位置为 ,在算法的实现中,首先会创建Size大小的mesh_grid,即map,确定主点的位置,然后根据mesh_grid上每个点的坐标,计算其与主点的相对位置,然后进行后续的计算(转换到归一化平面、计算scale等),因此根据这个逻辑如果上面的主点不在Size的中心,就会导致相机实际拍摄到内容的中心在主点区域,但是右下角会有很多的延申。例如下图这种:
从上图原始的鱼眼图中可以看出相机拍摄的内容中心大概在棋盘格附近,然而去畸变了之后棋盘格却跑到了左上角。这就是因为我们设置的主点偏左上,没有位于生成的去畸变图的中心。这就是2.2节中提到的:initUndistortRectifyMap函数中的size参数一般都是与去畸变之后图像的相机参数中主点大小强相关的。
相机焦距调整示意图上图中相机的真实焦距为f,假设cmos长度不变,我们只是把成像平面放在了 f/2 的位置上,显然调整焦距后的相机FOV更大,能够看到的东西越多。同理,对于标定车间中的大方格,假设我们调参使得 , 缩小一定的倍数,理论上就可以看到更多的内容。
将相机内参 f 缩小为 f/2 意味着我们将相机的cmos推导距离相机光心 f/2 处,在这个平面上做映射。算法流程如下:
方程的问题,用到非线性优化,速度很慢。因此我们通过畸变表,构建了一个多项式,通过反向拟合的方法,提前拟合出fish2undis的方程系数:#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]) 计算fish2undis的过程与undis2fish(3.1,3.2)的过程略有不同,但都是寻找 与 之间的映射关系,因为 f 平面才是我们真实拿到的fish图,我们最终还是要在这个原始的fish图上找点。
实现代码:cv::Vec2f CalibrateInit::warpFisheye2Undist(float fish_scale, float f_dx, float f_dy, float undis_center_h, float undis_center_w, float fish_center_h, float fish_center_w, cv::Vec4d undis_param, float x, float y) { // f_dx *= fish_scale; // f_dy *= fish_scale; float y_ = (y - fish_center_h) / f_dy; // normalized plane float x_ = (x - fish_center_w) / f_dx; float r_distorted = static_cast<float>(sqrt(pow(x_, 2) + pow(y_, 2))); float r_distorted_p2 = r_distorted * r_distorted; float r_distorted_p3 = r_distorted_p2 * r_distorted; float r_distorted_p4 = r_distorted_p2 * r_distorted_p2; float r_distorted_p5 = r_distorted_p2 * r_distorted_p3; float angle_undistorted = static_cast<float>(r_distorted + undis_param[0] * r_distorted_p2 + undis_param[1] * r_distorted_p3 + undis_param[2] * r_distorted_p4 + undis_param[3] * r_distorted_p5); // scale float r_undistorted = tanf(angle_undistorted); float scale = r_undistorted / (r_distorted + 0.00001f); // scale = r_dis on the camera img plane // divide r_undis on the normalized plane cv::Vec2f warp_xy; float xx = (x - fish_center_w) * fish_scale; float yy = (y - fish_center_h) * fish_scale; warpPointInverse(warp_xy, undis_center_h, undis_center_w, xx, yy, scale); return warp_xy; } void CalibrateInit::Vec2f& warp_xy, float map_center_h, float map_center_w, float x_, float y_, float scale) { warp_xy[0] = x_ * scale + map_center_w; warp_xy[1] = y_ * scale + map_center_h; }

审核编辑 :李倩
全部0条评论
快来发表一下你的评论吧 !