Guest User

Untitled

a guest
Jun 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. //
  2. // Created by root on 6/17/18.
  3. //
  4.  
  5. #ifndef VD_101_PATHTRACER_H
  6. #define VD_101_PATHTRACER_H
  7. #include "opencv2/video/tracking.hpp"
  8. #include "opencv2/imgproc/imgproc.hpp"
  9. #include "opencv2/highgui/highgui.hpp"
  10. #include "opencv2/features2d/features2d.hpp"
  11. #include "opencv2/calib3d/calib3d.hpp"
  12.  
  13.  
  14. #include <iostream>
  15. #include <ctype.h>
  16. #include <algorithm> // for copy
  17. #include <iterator> // for ostream_iterator
  18. #include <vector>
  19. #include <ctime>
  20. #include <sstream>
  21. #include <fstream>
  22. #include <string>
  23. #include <opencv/cv.h>
  24.  
  25. using namespace cv;
  26. using namespace std;
  27.  
  28. #define MIN_NUM_FEAT 200
  29.  
  30. struct PathTracer {
  31. vector<Point2f> trajectory;
  32.  
  33. PathTracer(Mat img1, Mat img2, bool show=true) : show(show), counter(0), scale(1.0), focal(1000){
  34. //read the first two frames from the dataset
  35. Mat img1g, img2g;
  36. img1 = quantizeImage(img1);
  37. img2 = quantizeImage(img2);
  38. // we work with grayscale images
  39. cvtColor(img1, img1g, COLOR_BGR2GRAY);
  40. cvtColor(img2, img2g, COLOR_BGR2GRAY);
  41. prevImage = img2g;
  42.  
  43. // feature detection, tracking
  44. vector<Point2f> points1, points2; //vectors to store the coordinates of the feature points
  45. featureDetection(img1g, points1); //detect features in img_1
  46. vector<uchar> status;
  47. featureTracking(img1g,img2g,points1,points2, status); //track those features to img_2
  48.  
  49. pp =cv::Point2d(img1g.cols/2, img1g.rows/2);
  50. //recovering the pose and the essential matrix
  51.  
  52. E = findEssentialMat(points2, points1, focal, pp, RANSAC, 0.999, 1.0, mask);
  53. recoverPose(E, points2, points1, R, t, focal, pp, mask);
  54.  
  55. Mat prevImage = img2g;
  56. Mat currImage;
  57. prevFeatures = points2;
  58.  
  59. R_f = R.clone();
  60. t_f = t.clone();
  61.  
  62.  
  63. if(show) {
  64. namedWindow("Trajectory", WINDOW_AUTOSIZE);// Create a window for display.
  65. }
  66.  
  67. traj = Mat::zeros(600, 600, CV_8UC3);
  68. }
  69.  
  70. void addFrame(Mat frame) {
  71. Mat img;
  72. frame = quantizeImage(frame);
  73. cvtColor(frame, img, COLOR_BGR2GRAY);
  74. vector<uchar> status;
  75. featureTracking(prevImage, img, prevFeatures, currFeatures, status);
  76.  
  77. E = findEssentialMat(currFeatures, prevFeatures, focal, pp, RANSAC, 0.999, 1.0, mask);
  78. recoverPose(E, currFeatures, prevFeatures, R, t, focal, pp, mask);
  79.  
  80. Mat prevPts(2, prevFeatures.size(), CV_64F), currPts(2, currFeatures.size(), CV_64F);
  81.  
  82.  
  83. for (int i = 0; i <
  84. prevFeatures.size(); i++) { //this (x,y) combination makes sense as observed from the source code of triangulatePoints on GitHub
  85. prevPts.at<double>(0, i) = prevFeatures.at(i).x;
  86. prevPts.at<double>(1, i) = prevFeatures.at(i).y;
  87.  
  88. currPts.at<double>(0, i) = currFeatures.at(i).x;
  89. currPts.at<double>(1, i) = currFeatures.at(i).y;
  90. }
  91.  
  92. scale = getAbsoluteScale(counter++, 0, t.at<double>(2));
  93.  
  94. if ((scale > 0.1) && (t.at<double>(2) > t.at<double>(0)) && (t.at<double>(2) > t.at<double>(1))) {
  95.  
  96. t_f = t_f + scale * (R_f * t);
  97. R_f = R * R_f;
  98.  
  99. } else {
  100. //cout << "scale below 0.1, or incorrect translation" << endl;
  101. }
  102.  
  103. // a redetection is triggered in case the number of feautres being trakced go below a particular threshold
  104. if (prevFeatures.size() < MIN_NUM_FEAT) {
  105. //cout << "Number of tracked features reduced to " << prevFeatures.size() << endl;
  106. //cout << "trigerring redection" << endl;
  107. featureDetection(prevImage, prevFeatures);
  108. featureTracking(prevImage, img, prevFeatures, currFeatures, status);
  109.  
  110. }
  111.  
  112. prevImage = img.clone();
  113. prevFeatures = currFeatures;
  114. auto point = Point2f(t_f.at<double>(0) + 300, t_f.at<double>(2) + 100);
  115. trajectory.push_back(point);
  116.  
  117. if(show) {
  118. circle(traj, point, 1, CV_RGB(255, 0, 0), 2);
  119. //rectangle(traj, Point(10, 30), Point(550, 50), CV_RGB(0, 0, 0), CV_FILLED);
  120.  
  121. }
  122. }
  123.  
  124. Point2f real_position = Point2f(300, 100);
  125. int angle = 90;
  126. void RealAction(vizdoom::Button b) {
  127. auto distance = 10;
  128.  
  129. switch(b) {
  130. case vizdoom::MOVE_RIGHT:{
  131. real_position.x += cos((angle + 90)* M_PI /180.0) * distance; real_position.y += sin((angle + 90)* M_PI /180.0) * distance;
  132. break;
  133. }
  134. case vizdoom::MOVE_LEFT: {
  135. real_position.x += cos((angle - 90)* M_PI /180.0) * distance; real_position.y += sin((angle - 90)* M_PI /180.0) * distance;
  136. break;
  137. }
  138. case vizdoom::MOVE_BACKWARD: {
  139. real_position.x -= cos(angle * M_PI /180.0) * distance; real_position.y -= sin(angle * M_PI /180.0) * distance;
  140. break;
  141. }
  142. case vizdoom::MOVE_FORWARD:{
  143. real_position.x += cos(angle * M_PI /180.0) * distance; real_position.y += sin(angle * M_PI /180.0) * distance;
  144. break;
  145. }
  146. case vizdoom::TURN_RIGHT:{
  147. angle += 7;
  148. break;
  149. }
  150. case vizdoom::TURN_LEFT: {
  151. angle -= 7;
  152. break;
  153. }
  154. }
  155. if(show) {
  156. circle(traj, real_position, 1, CV_RGB(255, 0, 255), 2);
  157. imshow("Trajectory", traj);
  158. }
  159. }
  160.  
  161. private:
  162. bool show;
  163. int counter;
  164. double scale, focal;
  165. cv::Point2d pp;
  166. vector<Point2f> prevFeatures, currFeatures;
  167. Mat prevImage, traj, mask;
  168. Mat E, R, t;
  169. Mat R_f, t_f; //the final rotation and tranlation vectors
  170.  
  171. cv::Mat quantizeImage(const cv::Mat& inImage, int numBits=4)
  172. {
  173.  
  174. cv::Mat retImage;
  175. //
  176. retImage = inImage.clone();
  177. /*
  178. uchar maskBit = 0xFF;
  179.  
  180. // keep numBits as 1 and (8 - numBits) would be all 0 towards the right
  181. maskBit = maskBit << (8 - numBits);
  182.  
  183. for(int j = 0; j < retImage.rows; j++)
  184. for(int i = 0; i < retImage.cols; i++)
  185. {
  186. cv::Vec3b valVec = retImage.at<cv::Vec3b>(j, i);
  187. valVec[0] = valVec[0] & maskBit;
  188. valVec[1] = valVec[1] & maskBit;
  189. valVec[2] = valVec[2] & maskBit;
  190. retImage.at<cv::Vec3b>(j, i) = valVec;
  191. }
  192. */
  193. //blur(retImage,retImage, Size(10,10));
  194. //imshow("dbg", retImage);
  195. return retImage ;
  196. }
  197.  
  198. //this function automatically gets rid of points for which tracking fails
  199. void featureTracking(Mat img_1, Mat img_2, vector<Point2f>& points1, vector<Point2f>& points2, vector<uchar>& status) {
  200. vector<float> err;
  201. Size winSize=Size(21,21);
  202. TermCriteria termcrit=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);
  203.  
  204. calcOpticalFlowPyrLK(img_1, img_2, points1, points2, status, err, winSize, 3, termcrit, 0, 0.001);
  205.  
  206. //getting rid of points for which the KLT tracking failed or those who have gone outside the frame
  207. int indexCorrection = 0;
  208. for( int i=0; i<status.size(); i++)
  209. { Point2f pt = points2.at(i- indexCorrection);
  210. if ((status.at(i) == 0)||(pt.x<0)||(pt.y<0)) {
  211. if((pt.x<0)||(pt.y<0)) {
  212. status.at(i) = 0;
  213. }
  214. points1.erase (points1.begin() + (i - indexCorrection));
  215. points2.erase (points2.begin() + (i - indexCorrection));
  216. indexCorrection++;
  217. }
  218.  
  219. }
  220.  
  221. }
  222.  
  223.  
  224. void featureDetection(Mat img_1, vector<Point2f>& points1) { //uses FAST as of now, modify parameters as necessary
  225. vector<KeyPoint> keypoints_1;
  226. int fast_threshold = 20;
  227. bool nonmaxSuppression = true;
  228. FAST(img_1, keypoints_1, fast_threshold, nonmaxSuppression);
  229. KeyPoint::convert(keypoints_1, points1, vector<int>());
  230. }
  231.  
  232. double getAbsoluteScale(int frame_id, int sequence_id, double z_cal) {
  233. /*
  234. string line;
  235. int i = 0;
  236. ifstream myfile ("/home/avisingh/Datasets/KITTI_VO/00.txt");
  237. double x =0, y=0, z = 0;
  238. double x_prev, y_prev, z_prev;
  239. if (myfile.is_open())
  240. {
  241. while (( getline (myfile,line) ) && (i<=frame_id))
  242. {
  243. z_prev = z;
  244. x_prev = x;
  245. y_prev = y;
  246. std::istringstream in(line);
  247. //cout << line << '\n';
  248. for (int j=0; j<12; j++) {
  249. in >> z ;
  250. if (j==7) y=z;
  251. if (j==3) x=z;
  252. }
  253.  
  254. i++;
  255. }
  256. myfile.close();
  257. }
  258.  
  259. else {
  260. cout << "Unable to open file";
  261. return 0;
  262. }*/
  263.  
  264. return 10; //sqrt((x-x_prev)*(x-x_prev) + (y-y_prev)*(y-y_prev) + (z-z_prev)*(z-z_prev)) ;
  265. }
  266. };
  267.  
  268.  
  269. #endif //VD_101_PATHTRACER_H
Add Comment
Please, Sign In to add comment