comparison c/feature-based-tracking.cpp @ 130:2a6e7a9a5c53

changed to goodFeaturesToTrack instead of generic detectors
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 17 Aug 2011 17:30:30 -0400
parents 536510f60854
children 45c64e68053c
comparison
equal deleted inserted replaced
129:4742b2b6d851 130:2a6e7a9a5c53
1 //#include "Feature.hpp" 1 #include "Feature.hpp"
2 #include "Parameters.hpp" 2 #include "Parameters.hpp"
3 #include "cvutils.hpp" 3 #include "cvutils.hpp"
4 #include "utils.hpp" 4 #include "utils.hpp"
5 5
6 #include "src/Trajectory.h" 6 #include "src/Trajectory.h"
41 } 41 }
42 } 42 }
43 } 43 }
44 44
45 int main(int argc, char *argv[]) { 45 int main(int argc, char *argv[]) {
46 //vector<TrajectoryPoint2f> features;
47
48 BriefDescriptorExtractor brief(32); 46 BriefDescriptorExtractor brief(32);
49 const int DESIRED_FTRS = 500; 47 const int DESIRED_FTRS = 500;
50 //shared_ptr<FeatureDetector> detector = shared_ptr<FeatureDetector>(new GridAdaptedFeatureDetector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4)); 48 //shared_ptr<FeatureDetector> detector = shared_ptr<FeatureDetector>(new GridAdaptedFeatureDetector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4));
51 //GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); 49 //GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4);
52 50
60 params.featureQuality = 0.1; 58 params.featureQuality = 0.1;
61 params.minFeatureDistanceKLT = 3; 59 params.minFeatureDistanceKLT = 3;
62 params.windowSize = 3; 60 params.windowSize = 3;
63 params.useHarrisDetector = false; 61 params.useHarrisDetector = false;
64 params.k = 0.4; 62 params.k = 0.4;
65 GoodFeaturesToTrackDetector detector(params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, params.windowSize, params.useHarrisDetector, params.k); 63 //GoodFeaturesToTrackDetector detector(params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, params.windowSize, params.useHarrisDetector, params.k);
66 64
67 params.pyramidLevel = 3; 65 params.pyramidLevel = 3;
68 params.maxNumberTrackingIterations = 20; // 30 66 params.maxNumberTrackingIterations = 20; // 30
69 params.minTrackingError = 0.3; // 0.01 67 params.minTrackingError = 0.3; // 0.01
70 params.derivLambda = 0.5; 68 params.derivLambda = 0.5;
102 cout << "capture device " << argv[1] << " failed to open!" << endl; 100 cout << "capture device " << argv[1] << " failed to open!" << endl;
103 return 1; 101 return 1;
104 } 102 }
105 103
106 vector<KeyPoint> prevKpts, currKpts; 104 vector<KeyPoint> prevKpts, currKpts;
107 vector<Point2f> prevPts, currPts; 105 vector<Point2f> prevPts, currPts, newPts;
108 vector<uchar> status; 106 vector<uchar> status;
109 vector<float> errors; 107 vector<float> errors;
110 Mat prevDesc, currDesc; 108 Mat prevDesc, currDesc;
111 109
112 110 vector<FeatureTrajectory> features;
111
113 // TODO structure de donnee paires pointeur trajectory, numero de keypoint 112 // TODO structure de donnee paires pointeur trajectory, numero de keypoint
114 int key = '?'; 113 int key = '?';
115 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { 114 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) {
116 capture >> frame; 115 capture >> frame;
117 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; 116 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl;
118 while (frame.empty()) 117 while (frame.empty())
119 capture >> frame;//break; 118 capture >> frame;//break;
120 119
121 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); 120 cvtColor(frame, currentFrameBW, CV_RGB2GRAY);
121
122 // "normal" feature detectors: detect features here
123 // detector.detect(currentFrameBW, currKpts); // see video_homography c++ sample
122 124
123 if (!prevPts.empty()) { 125 if (!prevPts.empty()) {
124 //::keyPoints2Points(prevKpts, prevPts); 126 //::keyPoints2Points(prevKpts, prevPts);
125 currPts.clear(); 127 currPts.clear();
126 calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(3 /*static_cast<int>(TermCriteria::COUNT)+static_cast<int>(TermCriteria::EPS)*/, params.maxNumberTrackingIterations, params.minTrackingError), params.derivLambda, 0); // OPTFLOW_USE_INITIAL_FLOW 128 calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(3 /*static_cast<int>(TermCriteria::COUNT)+static_cast<int>(TermCriteria::EPS)*/, params.maxNumberTrackingIterations, params.minTrackingError), params.derivLambda, 0); // OPTFLOW_USE_INITIAL_FLOW
143 Mat featureMask = Mat::ones(videoSize, CV_8UC1); 145 Mat featureMask = Mat::ones(videoSize, CV_8UC1);
144 for (unsigned int n=0;n<currPts.size(); n++) 146 for (unsigned int n=0;n<currPts.size(); n++)
145 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++) 147 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++)
146 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) 148 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++)
147 featureMask.at<uchar>(i,j)=0; 149 featureMask.at<uchar>(i,j)=0;
148 detector.detect(currentFrameBW, currKpts, featureMask); 150 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.windowSize, params.useHarrisDetector, params.k);
149 ::keyPoints2Points(currKpts, currPts, false); 151 currPts.insert(currPts.end(), newPts.begin(), newPts.end());
152 //::keyPoints2Points(currKpts, currPts, false);
150 153
151 //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location 154 //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location
152 155
153 imshow("frame", frame); 156 imshow("frame", frame);
154 imshow("mask", featureMask*256); 157 imshow("mask", featureMask*256);