comparison c/feature-based-tracking.cpp @ 125:28907fde9855

work on klt tracker (problem on computer at poly)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 16 Aug 2011 19:35:07 -0400
parents 1e68e18b1aa5
children d19d6e63dd77
comparison
equal deleted inserted replaced
124:1e68e18b1aa5 125:28907fde9855
6 6
7 #include "opencv2/highgui/highgui.hpp" 7 #include "opencv2/highgui/highgui.hpp"
8 //#include "opencv2/imgproc/imgproc.hpp" 8 //#include "opencv2/imgproc/imgproc.hpp"
9 #include "opencv2/features2d/features2d.hpp" 9 #include "opencv2/features2d/features2d.hpp"
10 10
11 #include <boost/shared_ptr.hpp>
12
11 #include <iostream> 13 #include <iostream>
12 //#include <list> 14 //#include <list>
13 #include <vector> 15 #include <vector>
14 16
15 using namespace std; 17 using namespace std;
16 using namespace cv; 18 using namespace cv;
17 19 using namespace boost;
18 //#include "cv.h"
19
20 using namespace std;
21 20
22 void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) { 21 void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) {
23 for (int i = 0; i < (int)matches.size(); i++) 22 for (int i = 0; i < (int)matches.size(); i++)
24 { 23 {
25 Point2f pt_new = query[matches[i].queryIdx].pt; 24 Point2f pt_new = query[matches[i].queryIdx].pt;
31 } 30 }
32 } 31 }
33 } 32 }
34 33
35 int main(int argc, char *argv[]) { 34 int main(int argc, char *argv[]) {
36 vector<TrajectoryPoint2f> features; 35 //vector<TrajectoryPoint2f> features;
36
37 BriefDescriptorExtractor brief(32); 37 BriefDescriptorExtractor brief(32);
38 38 const int DESIRED_FTRS = 500;
39 //shared_ptr<FeatureDetector> detector = shared_ptr<FeatureDetector>(new GridAdaptedFeatureDetector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4));
40 GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4);
41
39 VideoCapture capture; 42 VideoCapture capture;
43 Mat frame, display;
40 44
41 Mat frame, display;
42 KLTFeatureTrackingParameters params; 45 KLTFeatureTrackingParameters params;
43 params.frame1 = 0; 46 params.frame1 = 0;
44 params.nFrames = -1; 47 params.nFrames = -1;
45 // TODO ajouter klt paremeters, reprendre code de 48 // TODO ajouter klt paremeters, reprendre code de opencvfeaturetracker
46 // GoodFeaturesToTrackDetector feature_detector(Params.max_nfeatures, Params.feature_quality, Params.min_feature_distance_klt, Params.window_size, Params.useHarrisDetector_GoodFeaturesToTrackDetector, Params.k_GoodFeaturesToTrackDetector); 49 //GoodFeaturesToTrackDetector detector(Params.max_nfeatures, Params.feature_quality, Params.min_feature_distance_klt, Params.window_size, Params.useHarrisDetector_GoodFeaturesToTrackDetector, Params.k_GoodFeaturesToTrackDetector);
47 // search descriptor_match.h 50 // search descriptor_match.h
51
52 BruteForceMatcher<Hamming> descMatcher;
53 vector<DMatch> matches;
48 54
49 if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter 55 if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter
50 capture.open(argc == 2 ? argv[1][0] - '0' : 0); 56 capture.open(argc == 2 ? argv[1][0] - '0' : 0);
51 else if( argc >= 2 ) 57 else if( argc >= 2 )
52 { 58 {
72 //help(argv); 78 //help(argv);
73 cout << "capture device " << argv[1] << " failed to open!" << endl; 79 cout << "capture device " << argv[1] << " failed to open!" << endl;
74 return 1; 80 return 1;
75 } 81 }
76 82
77 vector<DMatch> matches;
78
79 BruteForceMatcher<Hamming> desc_matcher;
80
81 vector<KeyPoint> prevKpts, currKpts; 83 vector<KeyPoint> prevKpts, currKpts;
82 vector<unsigned char> match_mask;
83 84
84 Mat gray; 85 Mat gray;
85 86
86 Mat prevDesc, currDesc; 87 Mat prevDesc, currDesc;
87 const int DESIRED_FTRS = 500;
88 GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4);
89
90 // TODO structure de donnee paires pointeur trajectory, numero de keypoint 88 // TODO structure de donnee paires pointeur trajectory, numero de keypoint
91 89 int key = '?';
92 for (int frameNum = 0; (params.frame1+frameNum < params.nFrames) || (params.nFrames < 0); frameNum++) { 90 for (int frameNum = 0; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) {
93 frameNum++;
94 //capture.set(CV_CAP_PROP_POS_FRAMES, frameNum);
95 //capture.grab();capture.grab();capture.retrieve(frame);
96 capture >> frame; 91 capture >> frame;
97 //cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl; 92 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl;
98 while (frame.empty()) 93 while (frame.empty())
99 capture >> frame;//break; 94 capture >> frame;//break;
100 95
101 cvtColor(frame, gray, CV_RGB2GRAY); 96 cvtColor(frame, gray, CV_RGB2GRAY);
102 97
104 //cout << currKpts.size() << " kpts" << endl; 99 //cout << currKpts.size() << " kpts" << endl;
105 100
106 brief.compute(gray, currKpts, currDesc); //Compute brief descriptors at each keypoint location 101 brief.compute(gray, currKpts, currDesc); //Compute brief descriptors at each keypoint location
107 102
108 //display = frame.clone(); 103 //display = frame.clone();
109 if (!prevKpts.empty()) 104 if (!prevKpts.empty()) {
110 { 105 cout << matches.size() << " matches" << endl;
111 desc_matcher.match(currDesc, prevDesc, matches); 106 descMatcher.match(currDesc, prevDesc, matches);
112 cout << "matches:" << matches.size() << endl; 107 cout << matches.size() << " matches" << endl;
113 drawMatchesRelative(prevKpts, currKpts, matches, frame); 108 drawMatchesRelative(prevKpts, currKpts, matches, frame);
114 //drawMatches(frame, prevKpts, frame, currKpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG); 109 //drawMatches(frame, prevKpts, frame, currKpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG);
115 } 110 }
116 111
117 imshow("frame", frame); 112 imshow("frame", frame);
118 prevKpts = currKpts; 113 prevKpts = currKpts;
119 currDesc.copyTo(prevDesc); 114 currDesc.copyTo(prevDesc);
120 int key = waitKey(0); 115 key = waitKey(0);
121 if (::interruptionKey(key))
122 break;
123 } 116 }
124 117
125 return 0; 118 return 0;
126 } 119 }
127 120