当前位置:   article > 正文

SLAM14讲ch8代码实践部分_视觉slam14讲 实践演示 ch8

视觉slam14讲 实践演示 ch8

一、LK光流法

     1.1   optical_flow.cpp文件

  1. #include <opencv2/opencv.hpp>
  2. #include <string>
  3. #include <chrono>
  4. #include <Eigen/Core>
  5. #include <Eigen/Dense>
  6. #include "opencv2/highgui/highgui_c.h"
  7. using namespace std;
  8. using namespace cv;
  9. string file_1 = "xxx/000001.png"; // first image
  10. string file_2 = "xxx/000002.png"; // second image
  11. /// Optical flow tracker and interface 创建一个光流追踪器的类
  12. class OpticalFlowTracker {
  13. public:
  14. // 该类的构造函数,这里该函数只是在创建追踪器对象时将传入的参数设置为该追踪器属性的初始值。
  15. // 即img1 = img1_, img2 = img2_, ...
  16. OpticalFlowTracker(
  17. const Mat &img1_,
  18. const Mat &img2_,
  19. const vector<KeyPoint> &kp1_,
  20. vector<KeyPoint> &kp2_,
  21. vector<bool> &success_,
  22. bool inverse_ = true, bool has_initial_ = false) :
  23. img1(img1_), img2(img2_), kp1(kp1_), kp2(kp2_), success(success_), inverse(inverse_),
  24. has_initial(has_initial_) {}
  25. void calculateOpticalFlow(const Range &range); // 追踪器的追踪函数,这里只写了该函数的声明,具体的实现在该类的外部。
  26. // 追踪器的属性,包括第一个图像,第二个图像,包含第一个图像关键点的容器,第二个图像的关键点的容器,
  27. // 是否追踪成功的bool值的容器,是否使用逆公式,dx、dy是否有初始值。
  28. private:
  29. const Mat &img1;
  30. const Mat &img2;
  31. const vector<KeyPoint> &kp1;//第一个图像关键点的容器
  32. vector<KeyPoint> &kp2;
  33. vector<bool> &success; //是否追踪成功
  34. bool inverse = true; //是否使用逆公式
  35. bool has_initial = false; //dx,dy是否有初始值
  36. };
  37. /**
  38. * single level optical flow
  39. * @param [in] img1 the first image
  40. * @param [in] img2 the second image
  41. * @param [in] kp1 keypoints in img1
  42. * @param [in|out] kp2 keypoints in img2, if empty, use initial guess in kp1
  43. * @param [out] success true if a keypoint is tracked successfully
  44. * @param [in] inverse use inverse formulation?
  45. */
  46. //实现单层光流函数
  47. void OpticalFlowSingleLevel(
  48. const Mat &img1,
  49. const Mat &img2,
  50. const vector<KeyPoint> &kp1,//第一个图像关键点容器
  51. vector<KeyPoint> &kp2,
  52. vector<bool> &success, //是否成功追踪
  53. bool inverse = false, //是否使用ni公式
  54. bool has_initial_guess = false
  55. );
  56. /**
  57. * multi level optical flow, scale of pyramid is set to 2 by default
  58. * the image pyramid will be create inside the function
  59. * @param [in] img1 the first pyramid
  60. * @param [in] img2 the second pyramid
  61. * @param [in] kp1 keypoints in img1
  62. * @param [out] kp2 keypoints in img2
  63. * @param [out] success true if a keypoint is tracked successfully
  64. * @param [in] inverse set true to enable inverse formulation
  65. */
  66. void OpticalFlowMultiLevel(
  67. const Mat &img1,
  68. const Mat &img2,
  69. const vector<KeyPoint> &kp1,
  70. vector<KeyPoint> &kp2,
  71. vector<bool> &success,
  72. bool inverse = false
  73. );
  74. /**
  75. * get a gray scale value from reference image (bi-linear interpolated)
  76. * @param img
  77. * @param x
  78. * @param y
  79. * @return the interpolated value of this pixel pixel(像素)
  80. */
  81. //获取像素值函数声明
  82. inline float GetPixelValue(const cv::Mat &img, float x, float y) {
  83. // boundary check 检测边界
  84. if (x < 0) x = 0;
  85. if (y < 0) y = 0;
  86. if (x >= img.cols - 1) x = img.cols - 2;
  87. if (y >= img.rows - 1) y = img.rows - 2;
  88. float xx = x - floor(x);// floor(x)表示对x向下取整。
  89. float yy = y - floor(y);
  90. int x_a1 = std::min(img.cols - 1, int(x) + 1);
  91. int y_a1 = std::min(img.rows - 1, int(y) + 1);
  92. // 就是双线性插值,4个点,离哪个点越近权重越大,总权重为1
  93. return (1 - xx) * (1 - yy) * img.at<uchar>(y, x)
  94. + xx * (1 - yy) * img.at<uchar>(y, x_a1)
  95. + (1 - xx) * yy * img.at<uchar>(y_a1, x)
  96. + xx * yy * img.at<uchar>(y_a1, x_a1);
  97. }
  98. int main(int argc, char **argv) {
  99. // images, note they are CV_8UC1, not CV_8UC3 // 设置flags = 0,相当于图像的格式为CV_8UC1,像素位深8,通道1,即灰度图
  100. Mat img1 = imread(file_1, 0);
  101. Mat img2 = imread(file_2, 0);
  102. cout << img1.size() << endl; // 查看图像尺寸
  103. cout << img2.size() << endl;
  104. // key points, using GFTT here.
  105. vector<KeyPoint> kp1; //定义一个关键点容器kp1
  106. Ptr<GFTTDetector> detector = GFTTDetector::create(50000, 0.01, 10); // maximum 500 keypoints 生成一个角点检测器detector
  107. // 最大角点数量500,角点可以接受的最小特征值0.01,角点间的最小距离20
  108. detector->detect(img1, kp1); // 检测img1中的角点并存入kp1容器中
  109. // now lets track these key points in the second image
  110. // first use single level LK in the validation picture
  111. vector<KeyPoint> kp2_single;// 单层光流关键点容器
  112. vector<bool> success_single;// 单层光流关键点追踪成功与否的bool值容器
  113. OpticalFlowSingleLevel(img1, img2, kp1, kp2_single, success_single);
  114. // then test multi-level LK
  115. vector<KeyPoint> kp2_multi;// 多层光流关键点容器
  116. vector<bool> success_multi;
  117. chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  118. OpticalFlowMultiLevel(img1, img2, kp1, kp2_multi, success_multi, true);
  119. chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  120. auto time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  121. cout << "optical flow by gauss-newton: " << time_used.count() << endl;
  122. // use opencv's flow for validation
  123. vector<Point2f> pt1, pt2;
  124. for (auto &kp: kp1) pt1.push_back(kp.pt);
  125. // status : 输出状态向量(无符号字符),如果找到相应特征的流,则向量的每个元素设置为1,否则设置为0。
  126. vector<uchar> status;
  127. // error :输出错误的矢量; 向量的每个元素都设置为相应特征的错误,错误度量的类型可以在flags参数中设置;
  128. // 如果未找到流,则未定义错误(使用status参数查找此类情况)。
  129. vector<float> error;
  130. t1 = chrono::steady_clock::now();
  131. cv::calcOpticalFlowPyrLK(img1, img2, pt1, pt2, status, error);
  132. t2 = chrono::steady_clock::now();
  133. time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  134. cout << "optical flow by opencv: " << time_used.count() << endl;
  135. // plot the differences of those functions
  136. Mat img2_single;//单层
  137. cv::cvtColor(img2, img2_single, CV_GRAY2BGR);//将img2转化为BGR图并以img2_single输出
  138. for (int i = 0; i < kp2_single.size(); i++) {
  139. if (success_single[i]) {
  140. cv::circle(img2_single, kp2_single[i].pt, 2, cv::Scalar(0, 250, 0), 2);// 第二个图像中追踪到的关键点画绿圆
  141. cv::line(img2_single, kp1[i].pt, kp2_single[i].pt, cv::Scalar(0, 250, 0));// 追踪到的关键点从第一个图像到第二个图像的流向
  142. }
  143. }
  144. //可视化光流
  145. Mat img2_multi;//多层
  146. cv::cvtColor(img2, img2_multi, CV_GRAY2BGR);//将img2转化为BGR图并以img2_single输出
  147. for (int i = 0; i < kp2_multi.size(); i++) {
  148. if (success_multi[i])// 追踪成功的关键点
  149. {
  150. cv::circle(img2_multi, kp2_multi[i].pt, 2, cv::Scalar(0, 250, 0), 2); // 第二个图像中追踪到的关键点画绿圆
  151. cv::line(img2_multi, kp1[i].pt, kp2_multi[i].pt, cv::Scalar(0, 250, 0)); // 追踪到的关键点从第一个图像到第二个图像的流向
  152. }
  153. }
  154. Mat img2_CV;
  155. cv::cvtColor(img2, img2_CV, CV_GRAY2BGR);
  156. for (int i = 0; i < pt2.size(); i++) {
  157. if (status[i]) {
  158. cv::circle(img2_CV, pt2[i], 2, cv::Scalar(0, 250, 0), 2);
  159. cv::line(img2_CV, pt1[i], pt2[i], cv::Scalar(0, 250, 0));
  160. }
  161. }
  162. cv::imshow("tracked single level", img2_single);
  163. cv::imshow("tracked multi level", img2_multi);
  164. cv::imshow("tracked by opencv", img2_CV);
  165. cv::waitKey(0);
  166. return 0;
  167. }
  168. void OpticalFlowSingleLevel(
  169. const Mat &img1,
  170. const Mat &img2,
  171. const vector<KeyPoint> &kp1,
  172. vector<KeyPoint> &kp2,
  173. vector<bool> &success,
  174. bool inverse, bool has_initial) {
  175. kp2.resize(kp1.size()); //将kp2大小设置为和kp1大小一致
  176. success.resize(kp1.size()); //successs应该与关键点的数量一致
  177. OpticalFlowTracker tracker(img1, img2, kp1, kp2, success, inverse, has_initial); //创建一个追踪器tracker
  178. parallel_for_(Range(0, kp1.size()),
  179. std::bind(&OpticalFlowTracker::calculateOpticalFlow, &tracker, placeholders::_1));
  180. }
  181. // kp1.size()是关键点的数量。bind是一个绑定函数,它相当于调用tracker.calculateOpticalFlow()。
  182. // placeholders::_1是占位符,表示传入第一个参数,此处Range(0, kp1.size())为传入的参数。
  183. void OpticalFlowTracker::calculateOpticalFlow(const Range &range) {
  184. // parameters
  185. int half_patch_size = 4; //使用关键点周围64个像素
  186. int iterations = 10; //迭代次数
  187. //遍历kp1中每一个关键点
  188. for (size_t i = range.start; i < range.end; i++) {
  189. auto kp = kp1[i];
  190. double dx = 0, dy = 0; // dx,dy need to be estimated 初始化为0
  191. //如果为true,则将dx,dy设置为两个关键点之间的差值
  192. if (has_initial) {
  193. dx = kp2[i].pt.x - kp.pt.x;
  194. dy = kp2[i].pt.y - kp.pt.y;
  195. // keypoint.pt表示关键点的坐标,keypoint.pt.x表示关键点的x坐标,keypoint.pt.y表示关键点的y坐标。
  196. }
  197. double cost = 0, lastCost = 0;//当前代价和之前的代价
  198. bool succ = true; // indicate if this point succeeded 假设关键点被成功追踪
  199. // Gauss-Newton iterations
  200. Eigen::Matrix2d H = Eigen::Matrix2d::Zero(); // hessian 海赛距证(二阶导数)
  201. Eigen::Vector2d b = Eigen::Vector2d::Zero(); // bias 偏置
  202. Eigen::Vector2d J; // jacobian 求解参数的一阶导
  203. for (int iter = 0; iter < iterations; iter++) {
  204. if (inverse == false) {
  205. H = Eigen::Matrix2d::Zero();
  206. b = Eigen::Vector2d::Zero();
  207. } else {
  208. // only reset b 只重置b矩阵
  209. b = Eigen::Vector2d::Zero();
  210. }
  211. cost = 0;
  212. // compute cost and jacobian (使用该关键点周围64个像素)
  213. for (int x = -half_patch_size; x < half_patch_size; x++)
  214. for (int y = -half_patch_size; y < half_patch_size; y++) {
  215. double error = GetPixelValue(img1, kp.pt.x + x, kp.pt.y + y) -
  216. GetPixelValue(img2, kp.pt.x + x + dx, kp.pt.y + y + dy);; // Jacobian
  217. if (inverse == false) {
  218. J = -1.0 * Eigen::Vector2d(
  219. 0.5 * (GetPixelValue(img2, kp.pt.x + dx + x + 1, kp.pt.y + dy + y) -
  220. GetPixelValue(img2, kp.pt.x + dx + x - 1, kp.pt.y + dy + y)),
  221. 0.5 * (GetPixelValue(img2, kp.pt.x + dx + x, kp.pt.y + dy + y + 1) -
  222. GetPixelValue(img2, kp.pt.x + dx + x, kp.pt.y + dy + y - 1))
  223. );
  224. } else if (iter == 0) {
  225. // in inverse mode, J keeps same for all iterations
  226. // NOTE this J does not change when dx, dy is updated, so we can store it and only compute error
  227. J = -1.0 * Eigen::Vector2d(
  228. 0.5 * (GetPixelValue(img1, kp.pt.x + x + 1, kp.pt.y + y) -
  229. GetPixelValue(img1, kp.pt.x + x - 1, kp.pt.y + y)),
  230. 0.5 * (GetPixelValue(img1, kp.pt.x + x, kp.pt.y + y + 1) -
  231. GetPixelValue(img1, kp.pt.x + x, kp.pt.y + y - 1))
  232. );
  233. }
  234. // compute H, b and set cost;
  235. b += -error * J;
  236. cost += error * error;
  237. if (inverse == false || iter == 0) {
  238. // also update H
  239. // 单层光流时每次迭代都会更新H,多层光流时只有每层的第0次迭代才会更新H。
  240. H += J * J.transpose();
  241. }
  242. }
  243. // compute update
  244. Eigen::Vector2d update = H.ldlt().solve(b);
  245. if (std::isnan(update[0])) {
  246. // sometimes occurred when we have a black or white patch and H is irreversible // 在黑色或白色像素点并且H不可逆时,可能会出现nan值,该关键点追踪失败
  247. cout << "update is nan" << endl;
  248. succ = false;
  249. break;
  250. }
  251. if (iter > 0 && cost > lastCost) {
  252. break;
  253. }
  254. // update dx, dy
  255. dx += update[0];
  256. dy += update[1];
  257. lastCost = cost;
  258. succ = true;
  259. if (update.norm() < 1e-2) {
  260. // converge
  261. break;
  262. }
  263. }
  264. success[i] = succ;
  265. // set kp2
  266. kp2[i].pt = kp.pt + Point2f(dx, dy); //追踪到第一个图像点在第二个图像点的位置
  267. }
  268. }
  269. //实现多层光流
  270. void OpticalFlowMultiLevel(
  271. const Mat &img1,
  272. const Mat &img2,
  273. const vector<KeyPoint> &kp1,
  274. vector<KeyPoint> &kp2,
  275. vector<bool> &success,
  276. bool inverse) {
  277. // parameters
  278. int pyramids = 4;//金字塔层数为4
  279. double pyramid_scale = 0.5;//采样率为0.5
  280. double scales[] = {1.0, 0.5, 0.25, 0.125};// 每一层相对于原始图像的采样率
  281. // create pyramids 创建金字塔
  282. chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  283. vector<Mat> pyr1, pyr2; // image pyramids 两个图像容器,包含图像金字塔每一层的图像
  284. for (int i = 0; i < pyramids; i++) {
  285. if (i == 0) {
  286. pyr1.push_back(img1);
  287. pyr2.push_back(img2);
  288. }
  289. // 之后每一层放置前一层0.5上采样的图像(双线性插值的方式修改图像尺寸)
  290. else {
  291. Mat img1_pyr, img2_pyr;
  292. cv::resize(pyr1[i - 1], img1_pyr,
  293. cv::Size(pyr1[i - 1].cols * pyramid_scale, pyr1[i - 1].rows * pyramid_scale));
  294. cv::resize(pyr2[i - 1], img2_pyr,
  295. cv::Size(pyr2[i - 1].cols * pyramid_scale, pyr2[i - 1].rows * pyramid_scale));
  296. pyr1.push_back(img1_pyr);
  297. pyr2.push_back(img2_pyr);
  298. }
  299. }
  300. chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  301. auto time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  302. cout << "build pyramid time: " << time_used.count() << endl;
  303. // coarse-to-fine LK tracking in pyramids
  304. vector<KeyPoint> kp1_pyr, kp2_pyr; // 两个存储关键点的容器,这里存储的是顶层(第3层,总共4层)图像的关键点
  305. for (auto &kp:kp1) {
  306. auto kp_top = kp;// 顶层相对于原图的采样率为0.5^3 = 0.125,
  307. // 将底层(第一个图像)的关键点的坐标都乘以0.125当作顶层图像的关键点
  308. kp_top.pt *= scales[pyramids - 1];
  309. kp1_pyr.push_back(kp_top);
  310. kp2_pyr.push_back(kp_top);
  311. }
  312. for (int level = pyramids - 1; level >= 0; level--) {
  313. // from coarse to fine
  314. success.clear();
  315. t1 = chrono::steady_clock::now();
  316. OpticalFlowSingleLevel(pyr1[level], pyr2[level], kp1_pyr, kp2_pyr, success, inverse, true); // 先对顶层应用单层光流。
  317. t2 = chrono::steady_clock::now();
  318. auto time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  319. cout << "track pyr " << level << " cost time: " << time_used.count() << endl;
  320. //提前准备好下一层的关键点,
  321. // 就是讲上一层的关键点坐标除以采样率,这里除以0.5,即将关键点坐标乘以2.
  322. if (level > 0) {
  323. for (auto &kp: kp1_pyr)
  324. kp.pt /= pyramid_scale;
  325. for (auto &kp: kp2_pyr)
  326. kp.pt /= pyramid_scale;
  327. }
  328. }
  329. for (auto &kp: kp2_pyr)
  330. kp2.push_back(kp);
  331. }

1.2 CMakeList文件

  1. cmake_minimum_required(VERSION 2.8)
  2. project(ch8)
  3. set(CMAKE_BUILD_TYPE "Release")
  4. add_definitions("-DENABLE_SSE")
  5. set(CMAKE_CXX_FLAGS "-std=c++11 ${SSE_FLAGS} -g -O3 -march=native")
  6. find_package(OpenCV REQUIRED)
  7. find_package(Sophus REQUIRED)
  8. find_package(Pangolin REQUIRED)
  9. include_directories(
  10. ${OpenCV_INCLUDE_DIRS}
  11. ${G2O_INCLUDE_DIRS}
  12. ${Sophus_INCLUDE_DIRS}
  13. "/usr/include/eigen3/"
  14. ${Pangolin_INCLUDE_DIRS}
  15. )
  16. add_executable(optical_flow optical_flow.cpp)
  17. target_link_libraries(optical_flow ${OpenCV_LIBS} )

  1.3 需要注意的问题

            1.  在源代码中没有加头文件 #include "opencv2/highgui/highgui_c.h",这会导致出现CV_GRAY2BGR没有被定义的错误。解决方法就是在cpp文件中加上头文件#include "opencv2/highgui/highgui_c.h",即可。

        2.图片路径问题,在上面cpp代码中,作者讲图片路径用了

        string file_1 = "xxx/000001.png";  // first image
        string file_2 = "xxx/000002.png";  // second image

代替。这里需要将其改成你对应图片的绝对路径。具体做法是找到图片对应的位置,在终端打开,使用命令pwd打印当前路径,复制粘贴进去即可。在作者电脑上这两行代码为,具体路径以你电脑上为准。

  1. string file_1 = "/home/l/learn_slam/ch8/LK1.png"; // first image
  2. string file_2 = "/home/l/learn_slam/ch8/LK2.png"; // second image

改完之后进行编译,执行就可以运行。

二、直接法代码实现

2.1 direct_method.cpp文件   

  1. #include <opencv2/opencv.hpp>
  2. #include <sophus/se3.hpp>
  3. #include <boost/format.hpp>
  4. #include <pangolin/pangolin.h>
  5. #include "opencv2/highgui/highgui_c.h"
  6. using namespace std;
  7. typedef vector<Eigen::Vector2d, Eigen::aligned_allocator<Eigen::Vector2d>> VecVector2d;
  8. // Camera intrinsics 相机内参
  9. double fx = 718.856, fy = 718.856, cx = 607.1928, cy = 185.2157;
  10. // baseline 双目相机基线
  11. double baseline = 0.573;
  12. // paths 图像路径
  13. string left_file = "../left.png";
  14. string disparity_file = "../disparity.png";
  15. boost::format fmt_others("../%06d.png"); // other files
  16. // useful typedefs
  17. typedef Eigen::Matrix<double, 6, 6> Matrix6d;
  18. typedef Eigen::Matrix<double, 2, 6> Matrix26d;
  19. typedef Eigen::Matrix<double, 6, 1> Vector6d;
  20. /// class for accumulator jacobians in parallel 用于并行计算雅可比矩阵的类
  21. class JacobianAccumulator {
  22. public:
  23. //类的构造函数,使用列表进行初始化
  24. JacobianAccumulator(
  25. const cv::Mat &img1_,
  26. const cv::Mat &img2_,
  27. const VecVector2d &px_ref_,//角点坐标
  28. const vector<double> depth_ref_,//路标点的Z坐标值
  29. Sophus::SE3d &T21_) :
  30. img1(img1_), img2(img2_), px_ref(px_ref_), depth_ref(depth_ref_), T21(T21_) {
  31. projection = VecVector2d(px_ref.size(), Eigen::Vector2d(0, 0));
  32. }
  33. /// accumulate jacobians in a range 在range范围内加速计算雅可比矩阵
  34. void accumulate_jacobian(const cv::Range &range);
  35. /// get hessian matrix 获取海塞矩阵
  36. Matrix6d hessian() const { return H; }
  37. /// get bias 获取矩阵b
  38. Vector6d bias() const { return b; }
  39. /// get total cost 获取总共的代价
  40. double cost_func() const { return cost; }
  41. /// get projected points 获取图像2中的角点坐标
  42. VecVector2d projected_points() const { return projection; }
  43. /// reset h, b, cost to zero 将海塞矩阵H,矩阵b和代价cost置为0
  44. void reset() {
  45. H = Matrix6d::Zero();
  46. b = Vector6d::Zero();
  47. cost = 0;
  48. }
  49. private:
  50. const cv::Mat &img1;
  51. const cv::Mat &img2;
  52. const VecVector2d &px_ref;//图像1中角点坐标
  53. const vector<double> depth_ref;//图像1中路标点的Z坐标值
  54. Sophus::SE3d &T21;
  55. VecVector2d projection; // projected points
  56. std::mutex hessian_mutex;
  57. Matrix6d H = Matrix6d::Zero();
  58. Vector6d b = Vector6d::Zero();
  59. double cost = 0;
  60. };
  61. /**
  62. * pose estimation using direct method
  63. * @param img1
  64. * @param img2
  65. * @param px_ref
  66. * @param depth_ref
  67. * @param T21
  68. */
  69. void DirectPoseEstimationMultiLayer(
  70. const cv::Mat &img1,
  71. const cv::Mat &img2,
  72. const VecVector2d &px_ref,
  73. const vector<double> depth_ref,
  74. Sophus::SE3d &T21
  75. );
  76. //定义DirectPoseEstimationMultiLayer函数 多层直接法
  77. /**
  78. * pose estimation using direct method
  79. * @param img1
  80. * @param img2
  81. * @param px_ref
  82. * @param depth_ref
  83. * @param T21
  84. */
  85. void DirectPoseEstimationSingleLayer(
  86. const cv::Mat &img1,
  87. const cv::Mat &img2,
  88. const VecVector2d &px_ref,
  89. const vector<double> depth_ref,
  90. Sophus::SE3d &T21
  91. );
  92. //定义DirectPoseEstimationSingleLayer函数 单层直接法
  93. // bilinear interpolation 双线性插值求灰度值
  94. inline float GetPixelValue(const cv::Mat &img, float x, float y) //inline表示内联函数,它是为了解决一些频繁调用的小函数大量消耗栈空间的问题而引入的
  95. {
  96. // boundary check
  97. if (x < 0) x = 0;
  98. if (y < 0) y = 0;
  99. if (x >= img.cols) x = img.cols - 1;
  100. if (y >= img.rows) y = img.rows - 1;
  101. //...|I1 I2|...
  102. //...| |...
  103. //...| |...
  104. //...|I3 I4|...
  105. uchar *data = &img.data[int(y) * img.step + int(x)];//x和y是整数
  106. //data[0] -> I1 data[1] -> I2 data[img.step] -> I3 data[img.step + 1] -> I4
  107. float xx = x - floor(x);//xx算出的是x的小数部分
  108. float yy = y - floor(y);//yy算出的是y的小数部分
  109. return float//最终的像素灰度值
  110. (
  111. (1 - xx) * (1 - yy) * data[0] +
  112. xx * (1 - yy) * data[1] +
  113. (1 - xx) * yy * data[img.step] +
  114. xx * yy * data[img.step + 1]
  115. );
  116. }
  117. int main(int argc, char **argv) {
  118. cv::Mat left_img = cv::imread(left_file, 0);//0表示返回灰度图,left.png表示000000.png
  119. cv::Mat disparity_img = cv::imread(disparity_file, 0);//0表示返回灰度图,disparity.png是left.png的视差图
  120. // let's randomly pick pixels in the first image and generate some 3d points in the first image's frame
  121. //在图像1中随机选择一些像素点,然后恢复深度,得到一些路标点
  122. cv::RNG rng;
  123. int nPoints = 2000;
  124. int boarder = 20;
  125. VecVector2d pixels_ref;
  126. vector<double> depth_ref;
  127. // generate pixels in ref and load depth data
  128. for (int i = 0; i < nPoints; i++) {
  129. int x = rng.uniform(boarder, left_img.cols - boarder); // don't pick pixels close to boarder 不要拾取靠近边界的像素
  130. int y = rng.uniform(boarder, left_img.rows - boarder); // don't pick pixels close to boarder 不要拾取靠近边界的像素
  131. int disparity = disparity_img.at<uchar>(y, x);
  132. double depth = fx * baseline / disparity; // you know this is disparity to depth
  133. depth_ref.push_back(depth);
  134. pixels_ref.push_back(Eigen::Vector2d(x, y));
  135. }
  136. // estimates 01~05.png's pose using this information
  137. Sophus::SE3d T_cur_ref;
  138. for (int i = 1; i < 6; i++)// 1~5 i从1到5,共5张图
  139. {
  140. cv::Mat img = cv::imread((fmt_others % i).str(), 0);//读取图片,0表示返回一张灰度图
  141. // try single layer by uncomment this line
  142. // DirectPoseEstimationSingleLayer(left_img, img, pixels_ref, depth_ref, T_cur_ref);
  143. //利用单层直接法计算图像img相对于left_img的位姿T_cur_ref,以图片left.png为基准
  144. DirectPoseEstimationMultiLayer(left_img, img, pixels_ref, depth_ref, T_cur_ref);//调用DirectPoseEstimationMultiLayer 多层直接法
  145. }
  146. return 0;
  147. }
  148. void DirectPoseEstimationSingleLayer(
  149. const cv::Mat &img1,
  150. const cv::Mat &img2,
  151. const VecVector2d &px_ref,//第1张图中的角点坐标
  152. const vector<double> depth_ref,//第1张图中路标点的Z坐标值 就是深度信息
  153. Sophus::SE3d &T21) {
  154. const int iterations = 10;//设置迭代次数为10
  155. double cost = 0, lastCost = 0;//将代价和最终代价初始化为0
  156. auto t1 = chrono::steady_clock::now();//开始计时
  157. JacobianAccumulator jaco_accu(img1, img2, px_ref, depth_ref, T21);
  158. for (int iter = 0; iter < iterations; iter++) {
  159. jaco_accu.reset();//重置
  160. //完成并行计算海塞矩阵H,矩阵b和代价cost
  161. cv::parallel_for_(cv::Range(0, px_ref.size()),
  162. std::bind(&JacobianAccumulator::accumulate_jacobian, &jaco_accu, std::placeholders::_1));
  163. Matrix6d H = jaco_accu.hessian();//计算海塞矩阵
  164. Vector6d b = jaco_accu.bias();//计算b矩阵
  165. // solve update and put it into estimation
  166. //求解增量方程更新优化变量T21
  167. Vector6d update = H.ldlt().solve(b);;
  168. T21 = Sophus::SE3d::exp(update) * T21;
  169. cost = jaco_accu.cost_func();
  170. if (std::isnan(update[0])) //解出来的更新量不是一个数字,退出迭代
  171. {
  172. // sometimes occurred when we have a black or white patch and H is irreversible
  173. cout << "update is nan" << endl;
  174. break;
  175. }
  176. if (iter > 0 && cost > lastCost) //代价不再减小,退出迭代
  177. {
  178. cout << "cost increased: " << cost << ", " << lastCost << endl;
  179. break;
  180. }
  181. if (update.norm() < 1e-3) //更新量的模小于1e-3,退出迭代
  182. {
  183. // converge
  184. break;
  185. }
  186. lastCost = cost;
  187. cout << "iteration: " << iter << ", cost: " << cost << endl;
  188. }//GN(高斯牛顿法)迭代结束
  189. cout << "T21 = \n" << T21.matrix() << endl;//输出T21矩阵
  190. auto t2 = chrono::steady_clock::now();//计时结束
  191. auto time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);//计算耗时
  192. cout << "direct method for single layer: " << time_used.count() << endl;//输出使用单层直接法所用时间
  193. // plot the projected pixels here
  194. cv::Mat img2_show;
  195. cv::cvtColor(img2, img2_show, CV_GRAY2BGR);
  196. VecVector2d projection = jaco_accu.projected_points();
  197. for (size_t i = 0; i < px_ref.size(); ++i) {
  198. auto p_ref = px_ref[i];
  199. auto p_cur = projection[i];
  200. if (p_cur[0] > 0 && p_cur[1] > 0) {
  201. cv::circle(img2_show, cv::Point2f(p_cur[0], p_cur[1]), 2, cv::Scalar(0, 250, 0), 2);
  202. cv::line(img2_show, cv::Point2f(p_ref[0], p_ref[1]), cv::Point2f(p_cur[0], p_cur[1]),
  203. cv::Scalar(0, 250, 0));
  204. }
  205. }
  206. cv::imshow("current", img2_show);
  207. cv::waitKey();
  208. }
  209. void JacobianAccumulator::accumulate_jacobian(const cv::Range &range) {
  210. // parameters
  211. const int half_patch_size = 1;
  212. int cnt_good = 0;
  213. Matrix6d hessian = Matrix6d::Zero();
  214. Vector6d bias = Vector6d::Zero();
  215. double cost_tmp = 0;
  216. for (size_t i = range.start; i < range.end; i++) {
  217. // compute the projection in the second image //point_ref表示图像1中的路标点
  218. Eigen::Vector3d point_ref =
  219. depth_ref[i] * Eigen::Vector3d((px_ref[i][0] - cx) / fx, (px_ref[i][1] - cy) / fy, 1);
  220. Eigen::Vector3d point_cur = T21 * point_ref;//point_cur表示图像2中的路标点
  221. if (point_cur[2] < 0) // depth invalid
  222. continue;
  223. //u,v表示图像2中对应的角点坐标
  224. float u = fx * point_cur[0] / point_cur[2] + cx, v = fy * point_cur[1] / point_cur[2] + cy;//视觉slam十四讲p99式5.5
  225. // u = fx * X / Z + cx v = fy * Y / Z + cy X -> point_cur[0] Y -> point_cur[1] Z -> point_cur[2]
  226. if (u < half_patch_size || u > img2.cols - half_patch_size || v < half_patch_size ||
  227. v > img2.rows - half_patch_size)
  228. continue;
  229. projection[i] = Eigen::Vector2d(u, v);//projection表示图像2中相应的角点坐标值
  230. double X = point_cur[0], Y = point_cur[1], Z = point_cur[2],
  231. Z2 = Z * Z, Z_inv = 1.0 / Z, Z2_inv = Z_inv * Z_inv;// Z2_inv = (1 / (Z * Z))
  232. cnt_good++;
  233. // and compute error and jacobian 计算海塞矩阵H,矩阵b和代价cost
  234. for (int x = -half_patch_size; x <= half_patch_size; x++)
  235. for (int y = -half_patch_size; y <= half_patch_size; y++) {
  236. //ei = I1(p1,i) - I(p2,i)其中p1,p2空间点P在两个时刻的像素位置坐标
  237. double error = GetPixelValue(img1, px_ref[i][0] + x, px_ref[i][1] + y) -
  238. GetPixelValue(img2, u + x, v + y);//视觉slam十四讲p219式8.13
  239. Matrix26d J_pixel_xi;
  240. Eigen::Vector2d J_img_pixel;
  241. //视觉slam十四讲p220式8.18
  242. J_pixel_xi(0, 0) = fx * Z_inv;
  243. J_pixel_xi(0, 1) = 0;
  244. J_pixel_xi(0, 2) = -fx * X * Z2_inv;
  245. J_pixel_xi(0, 3) = -fx * X * Y * Z2_inv;
  246. J_pixel_xi(0, 4) = fx + fx * X * X * Z2_inv;
  247. J_pixel_xi(0, 5) = -fx * Y * Z_inv;
  248. J_pixel_xi(1, 0) = 0;
  249. J_pixel_xi(1, 1) = fy * Z_inv;
  250. J_pixel_xi(1, 2) = -fy * Y * Z2_inv;
  251. J_pixel_xi(1, 3) = -fy - fy * Y * Y * Z2_inv;
  252. J_pixel_xi(1, 4) = fy * X * Y * Z2_inv;
  253. J_pixel_xi(1, 5) = fy * X * Z_inv;
  254. // -fx * inv_z 相当于-fx / Z
  255. //0
  256. // -fx * X * Z2_inv相当于fx * X / ( Z * Z )
  257. //--fx * X * Y * Z2_inv相当于fx * X * Y / ( Z * Z) +fx
  258. //fx + fx * X * X * Z2_inv相当于fx * X * X / (Z * Z)
  259. //-fx * Y * Z_inv相当于 fx * Y / Z
  260. //0
  261. //fy * Z_inv相当于-fy / Z
  262. //-fy * Y * Z2_inv相当于fy * Y / (Z * Z)
  263. //-fy - fy * Y * Y * Z2_inv相当于fy + fy * Y * Y / (Z * Z)
  264. //fy * X * Y * Z2_inv相当于fy * X * Y / ( Z * Z)
  265. //fy * X * Z_inv相当于-fy * X / Z
  266. J_img_pixel = Eigen::Vector2d(
  267. 0.5 * (GetPixelValue(img2, u + 1 + x, v + y) - GetPixelValue(img2, u - 1 + x, v + y)),
  268. 0.5 * (GetPixelValue(img2, u + x, v + 1 + y) - GetPixelValue(img2, u + x, v - 1 + y))
  269. );//dx,dy是优化变量 即(Δu,Δv) 计算雅克比矩阵
  270. //dx,dy是优化变量 即(Δu,Δv) 计算雅克比矩阵
  271. //相当于 J = - [ {I1( u + i + 1,v + j )-I1(u + i - 1,v + j )}/2,I1( u + i,v + j + 1)-I1( u + i ,v + j - 1)}/2]T T表示转置
  272. //I1 -> 图像1的灰度信息
  273. //i -> x
  274. //j -> y
  275. // total jacobian
  276. Vector6d J = -1.0 * (J_img_pixel.transpose() * J_pixel_xi).transpose();
  277. hessian += J * J.transpose();
  278. bias += -error * J;
  279. cost_tmp += error * error;
  280. }
  281. }
  282. if (cnt_good) {
  283. // set hessian, bias and cost
  284. unique_lock<mutex> lck(hessian_mutex);
  285. H += hessian;//H = Jij Jij(T)(累加和)
  286. b += bias;//b = -Jij * eij(累加和)
  287. cost += cost_tmp / cnt_good;//cost = || eij ||2 2范数
  288. }
  289. }
  290. void DirectPoseEstimationMultiLayer(
  291. const cv::Mat &img1,
  292. const cv::Mat &img2,
  293. const VecVector2d &px_ref,
  294. const vector<double> depth_ref,
  295. Sophus::SE3d &T21) {
  296. // parameters
  297. int pyramids = 4;//金字塔层数为4
  298. double pyramid_scale = 0.5;//每层之间的缩放因子设为0.5
  299. double scales[] = {1.0, 0.5, 0.25, 0.125};
  300. // create pyramids 创建图像金字塔
  301. vector<cv::Mat> pyr1, pyr2; // image pyramids pyr1 -> 图像1的金字塔 pyr2 -> 图像2的金字塔
  302. for (int i = 0; i < pyramids; i++) {
  303. if (i == 0) {
  304. pyr1.push_back(img1);
  305. pyr2.push_back(img2);
  306. } else {
  307. cv::Mat img1_pyr, img2_pyr;
  308. //将图像pyr1[i-1]的宽和高各缩放0.5倍得到图像img1_pyr
  309. cv::resize(pyr1[i - 1], img1_pyr,
  310. cv::Size(pyr1[i - 1].cols * pyramid_scale, pyr1[i - 1].rows * pyramid_scale));
  311. //将图像pyr2[i-1]的宽和高各缩放0.5倍得到图像img2_pyr
  312. cv::resize(pyr2[i - 1], img2_pyr,
  313. cv::Size(pyr2[i - 1].cols * pyramid_scale, pyr2[i - 1].rows * pyramid_scale));
  314. pyr1.push_back(img1_pyr);
  315. pyr2.push_back(img2_pyr);
  316. }
  317. }
  318. double fxG = fx, fyG = fy, cxG = cx, cyG = cy; // backup the old values 备份旧值
  319. for (int level = pyramids - 1; level >= 0; level--) {
  320. VecVector2d px_ref_pyr; // set the keypoints in this pyramid level 设置此金字塔级别中的关键点
  321. for (auto &px: px_ref) {
  322. px_ref_pyr.push_back(scales[level] * px);
  323. }
  324. // scale fx, fy, cx, cy in different pyramid levels 在不同的金字塔级别缩放 fx, fy, cx, cy
  325. fx = fxG * scales[level];
  326. fy = fyG * scales[level];
  327. cx = cxG * scales[level];
  328. cy = cyG * scales[level];
  329. DirectPoseEstimationSingleLayer(pyr1[level], pyr2[level], px_ref_pyr, depth_ref, T21);
  330. }
  331. }

2.2 CMakeList文件  

  1. cmake_minimum_required(VERSION 2.8)
  2. project(ch8)
  3. set(CMAKE_BUILD_TYPE "Release")
  4. add_definitions("-DENABLE_SSE")
  5. # set(CMAKE_CXX_FLAGS "-std=c++11 ${SSE_FLAGS} -g -O3 -march=native")
  6. set(CMAKE_CXX_FLAGS "-std=c++14 -O2 ${SSE_FLAGS} -msse4")
  7. find_package(OpenCV REQUIRED)
  8. find_package(Sophus REQUIRED)
  9. find_package(Pangolin REQUIRED)
  10. include_directories(
  11. ${OpenCV_INCLUDE_DIRS}
  12. ${G2O_INCLUDE_DIRS}
  13. ${Sophus_INCLUDE_DIRS}
  14. "/usr/include/eigen3/"
  15. ${Pangolin_INCLUDE_DIRS}
  16. )
  17. add_executable(optical_flow optical_flow.cpp)
  18. target_link_libraries(optical_flow ${OpenCV_LIBS} fmt)
  19. add_executable(direct_method direct_method.cpp)
  20. target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES} fmt)

2.3 编译时可能遇到的问题

          1.可能遇到error: there are no arguments to ‘slots_reference’ that depend on a template parameter, so a declaration of ‘slots_reference’ must be available [-fpermissive]
 1486 |         cow_copy_type<list_type, Lockable> ref = slots_reference();
      |                                                  ^~~~~~~~~~~~~~~等一系列问题,具体如下图所示。

b928ad3ba8464a2f980986350fbb0c82.png

        原因在于C++ 版本不同,在源代码中使用的是

        set(CMAKE_CXX_FLAGS "-std=c++11 ${SSE_FLAGS} -g -O3 -march=native")

由上我们可以看到使用的是C++11,故在电脑中会出现一系列未定义的问题。解决方法是改C++版本即可改成c++14即可,具体如下:

set(CMAKE_CXX_FLAGS "-std=c++14 -O2 ${SSE_FLAGS} -msse4")

        2.改完c++依然出现如下错误

9d3346d3437045e5a9a69385e347e961.png

 由错误我们可以知道是缺少fmt,如果你之前没有下载fmt可以在csdn上查找相关下载fmt文件,如果你已经下载好了仍然出现上述错误,原因在于最初源代码中在CMakeList.text文件关于target_link_libraries部分没有加fmt,解决方法是在所有target_link_libraries后面加上fmt即可,具体代码见上述cmakelist文件。

 

 

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

闽ICP备14008679号