Advertisement
OscarAHB

Untitled

Apr 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.03 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include "opencv2/video/tracking.hpp"
  5. #include <iostream>
  6. #include <sstream>
  7. #include <cmath>
  8. #include <algorithm>
  9.  
  10. #define PI 3.14159265
  11.  
  12. using namespace std;
  13. using namespace cv;
  14.  
  15. bool selectObject = false;
  16. Rect selection;
  17. Point origin;
  18. int trackObject = 0;
  19. Mat image;
  20.  
  21. string ZeroPadNumber(int num);
  22. static void onMouse(int event, int x, int y, int, void*);
  23.  
  24. float evalXDisp(const vector<Point2f> evalPoints[2]);
  25. float histComp(Mat i1, Mat i2);
  26.  
  27. int main(int argc, char** argv)
  28. {
  29.     //string path = argv[1];
  30.     string path("../2009_09_08_drive_0019/");
  31.     namedWindow("Map", CV_WINDOW_NORMAL);
  32.     Mat m1 = Mat(1600, 800, CV_8UC1, double(0));
  33.     imshow("Map", m1);
  34.     Point2f mapStart = Point2f(400, m1.rows);
  35.     Mat src, src_prev, img_prev, img;
  36.     unsigned numPoints = 30;
  37.     vector<Point2f> grid;
  38.     vector<Point2f> lk_pts;
  39.     TermCriteria termcrit(TermCriteria::COUNT | TermCriteria::EPS, 30, 0.01);
  40.     Size winSize(31, 31);
  41.  
  42.     namedWindow("Select ROI", WINDOW_AUTOSIZE);
  43.     setMouseCallback("Select ROI", onMouse, 0);
  44.  
  45.     while (true) { //Select ROI-----------------------------------
  46.         src = imread(path + "I1_000000.png", 0);
  47.         src.copyTo(image);
  48.  
  49.         if (selection.width > 0 && selection.height > 0) {
  50.             rectangle(image, selection, Scalar(255));
  51.         }
  52.  
  53.         imshow("Select ROI", image);
  54.         if ((waitKey(30) & 0xff) == 32) {
  55.             if (selection.width > 0 && selection.height > 0) {
  56.                 break;
  57.             }
  58.             else
  59.                 cout << "You must select a ROI" << endl;
  60.         }
  61.     }
  62.  
  63.     //Create ROI's
  64.     destroyWindow("Select ROI");
  65.  
  66.     //Insert coordinate points
  67.     int sampleSpace = 4;
  68.     Point2f dummy;
  69.     for (int i = 0; i<selection.width; i += sampleSpace) {
  70.         for (int j = 0; j<selection.height; j += sampleSpace) {
  71.             dummy.x = selection.x + i;
  72.             dummy.y = selection.y + j;
  73.             grid.push_back(dummy);
  74.         }
  75.     }
  76.  
  77.  
  78.     src.copyTo(img);
  79.     src.copyTo(img_prev);
  80.     Mat roi(img, selection);
  81.     Mat roi_prev(img_prev, selection);
  82.     namedWindow("Sequence", CV_WINDOW_AUTOSIZE);
  83.  
  84.     vector<float> substract(grid.size());
  85.     vector<float> sig(grid.size());
  86.     vector<float> y(grid.size());
  87.     vector<Point2f>points(1249);
  88.  
  89.  
  90.     for (int j = 1; j <= 1248; j++)
  91.     { //Display images-----------------------------------
  92.         src_prev = imread(path + "I1_" + ZeroPadNumber(j - 1) + ".png", 0);
  93.         src = imread(path + "I1_" + ZeroPadNumber(j) + ".png", 0);
  94.  
  95.         src.copyTo(image);
  96.         src.copyTo(img);
  97.         src_prev.copyTo(img_prev);
  98.  
  99.         rectangle(image, selection, Scalar(255));
  100.         //imshow("ROI", roi);
  101.         //imshow("ROI_PREV", roi_prev);
  102.         vector<uchar> status;
  103.         vector<float> err;
  104.         calcOpticalFlowPyrLK(img_prev, img, grid, lk_pts, status, err, winSize, 3, termcrit, 0);
  105.         float moda = 0;
  106.         int frecuencia = 0;
  107.  
  108.         for (int k = 0; k < grid.size(); k++)
  109.         {
  110.             circle(image, grid[k], 1, Scalar(155), -1, 8);
  111.             line(image, grid[k], lk_pts[k], Scalar(255));
  112.             circle(image, lk_pts[k], 2, Scalar(255), -1, 8);
  113.             if (status[k] == 1) //Buscamos los valores 1 dentro del vector con la igualdad
  114.             {
  115.                 substract[k] = grid.at(k).x - lk_pts.at(k).x;
  116.                 // moda
  117.                 for (int h = k - 1; h >= 0; h--)
  118.                 {
  119.                     if (substract[k] == substract[h])
  120.                     {
  121.                         sig[h]++;
  122.                         if (sig[h] > moda)
  123.                         {
  124.                             moda = k;
  125.                             frecuencia = sig[h];
  126.                         }
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.         // Compara histogramas
  132.         if (status[j] == 1)
  133.         {
  134.             float score = histComp(roi, roi_prev);
  135.             y[j] = score;
  136.         }
  137.         points.at(j).x = 200 + substract[moda];
  138.         points.at(j).y = j * 2;
  139.         cout << "[" << j << "]   Moda: " << substract[moda] << "     Score: " << y[j] << endl;
  140.  
  141.         imshow("Sequence", image);
  142.         mapStart = points[j];
  143.         circle(m1, mapStart, 4, Scalar(155), -1, 10);
  144.         //line(m1, points[j], points[j + 1], Scalar(255));
  145.         imshow("Map", m1);
  146.  
  147.         if (waitKey(10) > 10)
  148.             break;
  149.     }
  150.     waitKey(0);
  151.     return 0;
  152. }
  153.  
  154. string ZeroPadNumber(int num) {
  155.     stringstream ss;
  156.  
  157.     ss << num;
  158.     string ret;
  159.     ss >> ret;
  160.  
  161.     // Append zero chars
  162.     int str_length = ret.length();
  163.     for (int i = 0; i < 6 - str_length; i++)
  164.         ret = "0" + ret;
  165.     return ret;
  166. }
  167.  
  168. static void onMouse(int event, int x, int y, int, void*) {
  169.     if (selectObject) {
  170.         selection.x = MIN(x, origin.x);
  171.         selection.y = MIN(y, origin.y);
  172.         selection.width = std::abs(x - origin.x);
  173.         selection.height = std::abs(y - origin.y);
  174.  
  175.         selection &= Rect(0, 0, image.cols, image.rows);
  176.     }
  177.  
  178.     switch (event) {
  179.     case CV_EVENT_LBUTTONDOWN:
  180.         origin = Point(x, y);
  181.         selection = Rect(x, y, 0, 0);
  182.         selectObject = true;
  183.         break;
  184.     case CV_EVENT_LBUTTONUP:
  185.         selectObject = false;
  186.         if (selection.width > 0 && selection.height > 0)
  187.             trackObject = -1;
  188.         break;
  189.     }
  190. }
  191.  
  192. float evalXDisp(const vector<Point2f> evalPoints[2]) {
  193.     float accum = 0;
  194.     float ax = 0;
  195.     float x = 0;
  196.  
  197.     for (unsigned i = 0; i != evalPoints[0].size(); ++i) {
  198.         ax = evalPoints[1][i].x;
  199.         x = evalPoints[0][i].x;
  200.         accum += ax - x;
  201.     }
  202.     accum = accum / evalPoints[1].size();
  203.     return accum;
  204. }
  205.  
  206. float histComp(Mat i1, Mat i2) {
  207.     MatND hist_1;
  208.     MatND hist_2;
  209.     Mat mask = Mat(i1.rows, i1.cols, CV_8UC1, double(0));
  210.     rectangle(mask, selection, Scalar(255), CV_FILLED, 8);
  211.  
  212.     int bins = 16;
  213.     int histSize[] = { bins };
  214.     float s_ranges[] = { 0, 256 };
  215.     const float* ranges[] = { s_ranges };
  216.  
  217.     calcHist(&i1, 1, 0, Mat(), hist_1, 1, histSize, ranges, true, false);
  218.     normalize(hist_1, hist_1, 0.0001, 1, NORM_MINMAX, -1, Mat());
  219.     calcHist(&i2, 1, 0, Mat(), hist_2, 1, histSize, ranges, true, false);
  220.     normalize(hist_2, hist_2, 0.0001, 1, NORM_MINMAX, -1, Mat());
  221.  
  222.     int numrows = bins;
  223.  
  224.     //make signature
  225.     Mat sig1(numrows, 3, CV_32FC1);
  226.     Mat sig2(numrows, 3, CV_32FC1);
  227.  
  228.     //fill value into signature
  229.     for (int h = 0; h< bins; h++)
  230.     {
  231.  
  232.         float binval = hist_1.at< float>(h, 0);
  233.         sig1.at< float>(h, 0) = binval;
  234.         sig1.at< float>(h, 1) = h;
  235.         sig1.at< float>(h, 2) = 0;
  236.  
  237.         binval = hist_2.at< float>(h, 0);
  238.         sig2.at< float>(h, 0) = binval;
  239.         sig2.at< float>(h, 1) = h;
  240.         sig2.at< float>(h, 2) = 0;
  241.  
  242.     }
  243.  
  244.     float emd = cv::EMD(sig1, sig2, CV_DIST_L2);
  245.  
  246.     return emd;
  247.  
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement