当前位置:   article > 正文

视觉里程计5(SLAM十四讲ch8)-LK光流_ch8 i cann't find associate.txt!

ch8 i cann't find associate.txt!

特征点法——>直接法 

光流(Optical Flow)

实践

采用TUM的RGB-D数据集

深度图和彩色图时间对齐

python associate.py rgb.txt depth.txt > associate.txt 

./build/useLK ../data

  1. #include <iostream>
  2. #include <fstream>
  3. #include <list>
  4. #include <vector>
  5. #include <chrono>
  6. using namespace std;
  7. #include <opencv2/core/core.hpp>
  8. #include <opencv2/highgui/highgui.hpp>
  9. #include <opencv2/features2d/features2d.hpp>
  10. #include <opencv2/video/tracking.hpp>
  11. int main( int argc, char** argv )
  12. {
  13. if ( argc != 2 )
  14. {
  15. cout<<"usage: useLK path_to_dataset"<<endl;
  16. return 1;
  17. }
  18. string path_to_dataset = argv[1];
  19. string associate_file = path_to_dataset + "/associate.txt";
  20. ifstream fin( associate_file );
  21. if ( !fin )
  22. {
  23. cerr<<"I cann't find associate.txt!"<<endl;
  24. return 1;
  25. }
  26. string rgb_file, depth_file, time_rgb, time_depth;
  27. list< cv::Point2f > keypoints; // 因为要删除跟踪失败的点,使用list
  28. cv::Mat color, depth, last_color;
  29. for ( int index=0; index<100; index++ )
  30. {
  31. fin>>time_rgb>>rgb_file>>time_depth>>depth_file;
  32. color = cv::imread( path_to_dataset+"/"+rgb_file );
  33. depth = cv::imread( path_to_dataset+"/"+depth_file, -1 );
  34. if (index ==0 )
  35. {
  36. // 对第一帧提取FAST特征点
  37. vector<cv::KeyPoint> kps;
  38. cv::Ptr<cv::FastFeatureDetector> detector = cv::FastFeatureDetector::create();
  39. detector->detect( color, kps );
  40. for ( auto kp:kps )
  41. keypoints.push_back( kp.pt );
  42. last_color = color;
  43. continue;
  44. }
  45. if ( color.data==nullptr || depth.data==nullptr )
  46. continue;
  47. // 对其他帧用LK跟踪特征点
  48. vector<cv::Point2f> next_keypoints;
  49. vector<cv::Point2f> prev_keypoints;
  50. for ( auto kp:keypoints )
  51. prev_keypoints.push_back(kp);
  52. vector<unsigned char> status;
  53. vector<float> error;
  54. chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  55. cv::calcOpticalFlowPyrLK( last_color, color, prev_keypoints, next_keypoints, status, error );
  56. chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  57. chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>( t2-t1 );
  58. cout<<"LK Flow use time:"<<time_used.count()<<" seconds."<<endl;
  59. // 把跟丢的点删掉
  60. int i=0;
  61. for ( auto iter=keypoints.begin(); iter!=keypoints.end(); i++)
  62. {
  63. if ( status[i] == 0 )
  64. {
  65. iter = keypoints.erase(iter);
  66. continue;
  67. }
  68. *iter = next_keypoints[i];
  69. iter++;
  70. }
  71. cout<<"tracked keypoints: "<<keypoints.size()<<endl;
  72. if (keypoints.size() == 0)
  73. {
  74. cout<<"all keypoints are lost."<<endl;
  75. break;
  76. }
  77. // 画出 keypoints
  78. cv::Mat img_show = color.clone();
  79. for ( auto kp:keypoints )
  80. cv::circle(img_show, kp, 10, cv::Scalar(0, 240, 0), 1);
  81. cv::imshow("corners", img_show);
  82. cv::waitKey(0);
  83. last_color = color;
  84. }
  85. return 0;
  86. }

 

                                                                                  。

                                                                                  。

                     。。

                         。

      

跟踪过程中一部分特征点会丢失,相机视角相对于最初的图像也发生了较大的改变。

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

闽ICP备14008679号