24 #include <orb_slam/Optimizer.h>
25 #include <orb_slam/ORBmatcher.h>
26 #include <Eigen/Geometry>
31 const cv::Mat& f2Gray,
32 std::vector<cv::Point2f>& p1,
33 std::vector<cv::Point2f>& p2,
34 std::vector<uchar>& inliers,
35 std::vector<float>& err)
40 cv::TermCriteria criteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 10, 0.03);
46 p2.reserve(p1.size());
47 inliers.reserve(p1.size());
48 err.reserve(p1.size());
50 cv::Size winSize(11, 11);
52 cv::calcOpticalFlowPyrLK(
67 const std::vector<cv::Point2f>& p2,
68 std::vector<cv::Point2f>& goodP1,
69 std::vector<cv::Point2f>& goodP2,
70 std::vector<uchar>& inliers,
71 std::vector<float>& err)
77 goodP1.reserve(p1.size());
78 goodP2.reserve(p1.size());
81 for (
int i = 0; i < p1.size(); i++)
83 if (inliers[i] && err[i] < 10.0)
85 goodP1.push_back(p1[i]);
86 goodP2.push_back(p2[i]);
87 avgMotion += (float)cv::norm(p1[i] - p2[i]);
90 return avgMotion / goodP1.size();
94 std::vector<cv::Point2f>& p1,
95 std::vector<cv::Point2f>& p2,
103 cv::Mat H = estimateAffinePartial2D(p1, p2);
104 float zrot = (float)atan2(H.at<
double>(1, 0), H.at<
double>(0, 0));
108 for (
int i = 0; i < p1.size(); i++)
110 dx += p2[i].x - p1[i].x;
111 dy += p2[i].y - p1[i].y;
113 dx /= (float)p1.size();
114 dy /= (float)p1.size();
116 Eigen::Vector3f v1(0, 0, 1.0);
117 Eigen::Vector3f vx(dx, 0, (
float)K.at<
double>(0, 0));
118 Eigen::Vector3f vy(0, dy, (
float)K.at<
double>(0, 0));
122 float xrot = -acos(v1.dot(vx));
123 float yrot = -acos(v1.dot(vy));
127 if (vx.dot(Eigen::Vector3f::UnitX()) > 0)
132 if (vy.dot(Eigen::Vector3f::UnitY()) > 0)
141 const std::vector<cv::Point2f>& p1,
142 const std::vector<cv::Point2f>& p2,
146 std::vector<uchar>& inliers)
153 double cx = K.at<
double>(0, 2);
154 double cy = K.at<
double>(1, 2);
156 cv::Point2f c((
float)cx, (
float)cy);
157 std::vector<cv::Point2f> p1C = p1;
158 std::vector<cv::Point2f> p2C = p2;
159 for (
int i = 0; i < p1.size(); i++)
167 cv::Mat aHb = cv::estimateAffinePartial2D(p1C, p2C, inliers);
173 cv::Mat bRa = aHb.rowRange(0, 2).colRange(0, 2).t();
174 cv::Mat aTR = aHb.col(2);
175 cv::Mat bTR = bRa * aTR;
178 zAngRAD = (float)atan2(aHb.at<
double>(1, 0), aHb.at<
double>(0, 0));
180 yAngRAD = (float)atan(bTR.at<
double>(0) / K.at<
double>(0, 0));
182 xAngRAD = (float)atan(bTR.at<
double>(1) / K.at<
double>(1, 1));
188 const std::vector<cv::Point2f>& p1,
189 const std::vector<cv::Point2f>& p2,
193 std::vector<uchar>& inliers)
200 double cx = K.at<
double>(0, 2);
201 double cy = K.at<
double>(1, 2);
204 cv::Point2f c((
float)cx, (
float)cy);
205 std::vector<cv::Point2f> p1C = p1;
206 std::vector<cv::Point2f> p2C = p2;
207 for (
int i = 0; i < p1.size(); i++)
218 cv::Mat aHb = cv::estimateAffinePartial2D(p1C, p2C, inliers);
231 yAngRAD = (float)atan(aHb.at<
double>(0, 2) / K.at<
double>(0, 0));
233 xAngRAD = (float)atan(aHb.at<
double>(1, 2) / K.at<
double>(1, 1));
247 Eigen::Matrix3f mx, my, mz;
248 mx = Eigen::AngleAxisf(xAngRAD, Eigen::Vector3f::UnitX());
249 my = Eigen::AngleAxisf(yAngRAD, Eigen::Vector3f::UnitY());
250 mz = Eigen::AngleAxisf(zAngRAD, Eigen::Vector3f::UnitZ());
256 Rx = cv::Mat::eye(4, 4, CV_32F);
257 eRx.copyTo(Rx.rowRange(0, 3).colRange(0, 3));
259 Ry = cv::Mat::eye(4, 4, CV_32F);
260 eRy.copyTo(Ry.rowRange(0, 3).colRange(0, 3));
262 Rz = cv::Mat::eye(4, 4, CV_32F);
263 eRz.copyTo(Rz.rowRange(0, 3).colRange(0, 3));
269 r = (cv::Mat_<float>(3, 3) << m(0), m(3), m(6), m(1), m(4), m(7), m(2), m(5), m(8));