comparison c/feature-based-tracking.cpp @ 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
comparison
equal deleted inserted replaced
122:654f1c748644 123:df3bdd8e50ba
13 using namespace cv; 13 using namespace cv;
14 14
15 //#include "cv.h" 15 //#include "cv.h"
16 16
17 using namespace std; 17 using namespace std;
18
19 void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) {
20 for (int i = 0; i < (int)matches.size(); i++)
21 {
22 Point2f pt_new = query[matches[i].queryIdx].pt;
23 Point2f pt_old = train[matches[i].trainIdx].pt;
24 Point2f dist = pt_new - pt_old;
25 if (norm(dist) < 20) {
26 cv::line(img, pt_new, pt_old, Scalar(125, 255, 125), 1);
27 cv::circle(img, pt_new, 2, Scalar(255, 0, 125), 1);
28 }
29 }
30 }
18 31
19 int main(int argc, char *argv[]) { 32 int main(int argc, char *argv[]) {
20 33
21 BriefDescriptorExtractor brief(32); 34 BriefDescriptorExtractor brief(32);
22 35
68 GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); 81 GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4);
69 82
70 int frameNum = 0; 83 int frameNum = 0;
71 for (;;) 84 for (;;)
72 { 85 {
73 frameNum+=2; 86 frameNum++;
87 //capture.set(CV_CAP_PROP_POS_FRAMES, frameNum);
88 //capture.grab();capture.grab();capture.retrieve(frame);
74 capture >> frame; 89 capture >> frame;
75 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl; 90 //cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl;
76 if (frame.empty()) 91 while (frame.empty())
77 break; 92 capture >> frame;//break;
78 93
79 cvtColor(frame, gray, CV_RGB2GRAY); 94 cvtColor(frame, gray, CV_RGB2GRAY);
80 95
81 detector.detect(gray, query_kpts); //Find interest points 96 detector.detect(gray, query_kpts); //Find interest points
97 cout << query_kpts.size() << " kpts" << endl;
82 98
83 brief.compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location 99 brief.compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location
84 100
85 // find how keypoints descriptions are matched to previous ones (in train kpts probably) 101 // find how keypoints descriptions are matched to previous ones (in train kpts probably)
86 display = frame.clone(); 102 //display = frame.clone();
87 if (!train_kpts.empty()) 103 if (!train_kpts.empty())
88 { 104 {
89 //vector<KeyPoint> test_kpts;
90 //warpKeypoints(H_prev.inv(), query_kpts, test_kpts);
91 //Mat mask = windowedMatchingMask(test_kpts, train_kpts, 25, 25);
92 desc_matcher.match(query_desc, train_desc, matches); 105 desc_matcher.match(query_desc, train_desc, matches);
93 drawMatches(frame, train_kpts, frame, query_kpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG); 106 cout << "matches:" << matches.size() << endl;
94 } // TODO do something like the match relative of the sample 107 drawMatchesRelative(train_kpts, query_kpts, matches, frame);
108 //drawMatches(frame, train_kpts, frame, query_kpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG);
109 }
95 110
96 imshow("frame", display); 111 imshow("frame", frame);
97 train_kpts = query_kpts; 112 train_kpts = query_kpts;
98 int key = waitKey(0); 113 query_desc.copyTo(train_desc);
114 int key = waitKey(5);
99 if (::interruptionKey(key)) 115 if (::interruptionKey(key))
100 break; 116 break;
101 } 117 }
102 118
103 Feature f; 119 Feature f;