18 #ifndef VISION_COMMON_RADTAN_DISTORTER_H_
19 #define VISION_COMMON_RADTAN_DISTORTER_H_
26 #include <opencv2/core/eigen.hpp>
28 #include <ceres/ceres.h>
37 Eigen::Matrix<T, 2, 1>
Distort(
const T* distortion,
const Eigen::Matrix<T, 3, 3>& intrinsics,
38 const Eigen::Matrix<T, 2, 1>& undistorted_point)
const {
39 const T& k1 = distortion[0];
40 const T& k2 = distortion[1];
41 const T& p1 = distortion[2];
42 const T& p2 = distortion[3];
46 const Eigen::Matrix<T, 2, 1> relative_coordinates =
RelativeCoordinates(undistorted_point, intrinsics);
47 const T& relative_x = relative_coordinates[0];
48 const T& relative_y = relative_coordinates[1];
50 const T r2 = relative_x * relative_x + relative_y * relative_y;
53 const T radial_distortion_coeff = 1.0 + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2;
54 T distorted_relative_x = relative_x * radial_distortion_coeff;
55 T distorted_relative_y = relative_y * radial_distortion_coeff;
58 distorted_relative_x =
59 distorted_relative_x + (2.0 * p1 * relative_x * relative_y + p2 * (r2 + 2.0 * relative_x * relative_x));
60 distorted_relative_y =
61 distorted_relative_y + (p1 * (r2 + 2.0 * relative_y * relative_y) + 2.0 * p2 * relative_x * relative_y);
63 return AbsoluteCoordinates(Eigen::Matrix<T, 2, 1>(distorted_relative_x, distorted_relative_y), intrinsics);
66 cv::Mat
Undistort(
const cv::Mat& distorted_image,
const Eigen::Matrix3d& intrinsics,
67 const Eigen::VectorXd& distortion)
const final {
68 cv::Mat undistorted_image;
69 cv::Mat intrinsics_mat;
70 cv::eigen2cv(intrinsics, intrinsics_mat);
71 cv::Mat distortion_vector;
72 cv::eigen2cv(distortion, distortion_vector);
73 cv::undistort(distorted_image, undistorted_image, intrinsics_mat, distortion_vector);
74 return undistorted_image;
77 Eigen::Vector2d
Undistort(
const Eigen::Vector2d& distorted_point,
const Eigen::Matrix3d& intrinsics,
78 const Eigen::VectorXd& distortion)
const final {
79 cv::Mat intrinsics_mat;
80 cv::eigen2cv(intrinsics, intrinsics_mat);
81 cv::Mat distortion_vector;
82 cv::eigen2cv(distortion, distortion_vector);
83 cv::Mat distorted_point_vector;
84 cv::eigen2cv(distorted_point, distorted_point_vector);
85 cv::Mat undistorted_point_vector;
86 cv::undistort(distorted_point_vector, undistorted_point_vector, intrinsics_mat, distortion_vector);
87 Eigen::Vector2d undistorted_point;
88 cv::cv2eigen(undistorted_point_vector, undistorted_point);
89 return undistorted_point;
94 #endif // VISION_COMMON_RADTAN_DISTORTER_H_