changeset 123:df3bdd8e50ba

displays tracking from video and webcam
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 16 Aug 2011 00:46:22 -0400
parents 654f1c748644
children 1e68e18b1aa5
files c/feature-based-tracking.cpp
diffstat 1 files changed, 28 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/c/feature-based-tracking.cpp	Mon Aug 15 18:37:14 2011 -0400
+++ b/c/feature-based-tracking.cpp	Tue Aug 16 00:46:22 2011 -0400
@@ -16,6 +16,19 @@
 
 using namespace std;
 
+void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) {
+  for (int i = 0; i < (int)matches.size(); i++)
+    {
+      Point2f pt_new = query[matches[i].queryIdx].pt;
+      Point2f pt_old = train[matches[i].trainIdx].pt;
+      Point2f dist = pt_new - pt_old;
+      if (norm(dist) < 20) {
+	cv::line(img, pt_new, pt_old, Scalar(125, 255, 125), 1);
+	cv::circle(img, pt_new, 2, Scalar(255, 0, 125), 1);
+      }
+    }
+}
+
 int main(int argc, char *argv[]) {
 
   BriefDescriptorExtractor brief(32);
@@ -70,32 +83,35 @@
   int frameNum = 0;
   for (;;)
     {
-      frameNum+=2;
+      frameNum++;
+      //capture.set(CV_CAP_PROP_POS_FRAMES, frameNum);
+      //capture.grab();capture.grab();capture.retrieve(frame);
       capture >> frame;
-      cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl;
-      if (frame.empty())
-	break;
+      //cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl;
+      while (frame.empty())
+	capture >> frame;//break;
       
       cvtColor(frame, gray, CV_RGB2GRAY);
       
       detector.detect(gray, query_kpts); //Find interest points
+      cout << query_kpts.size() << " kpts" << endl;
       
       brief.compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location
       
       // find how keypoints descriptions are matched to previous ones (in train kpts probably)
-      display = frame.clone();
+      //display = frame.clone();
       if (!train_kpts.empty())
         {
-	  //vector<KeyPoint> test_kpts;
-	  //warpKeypoints(H_prev.inv(), query_kpts, test_kpts);
-	  //Mat mask = windowedMatchingMask(test_kpts, train_kpts, 25, 25);
 	  desc_matcher.match(query_desc, train_desc, matches);
-	  drawMatches(frame, train_kpts, frame, query_kpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG);
-	} // TODO do something like the match relative of the sample
+	  cout << "matches:" << matches.size() << endl;
+	  drawMatchesRelative(train_kpts, query_kpts, matches, frame);
+	  //drawMatches(frame, train_kpts, frame, query_kpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG);
+	}
 
-      imshow("frame", display);
+      imshow("frame", frame);
       train_kpts = query_kpts;
-      int key = waitKey(0);
+      query_desc.copyTo(train_desc);
+      int key = waitKey(5);
       if (::interruptionKey(key))
 	break;
     }