Mercurial Hosting > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 140:8de5e8256224
added function to save vectors of features
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 23 Aug 2011 19:04:41 -0400 |
parents | 47329bd16cc0 |
children | 6f10a227486c |
comparison
equal
deleted
inserted
replaced
139:47329bd16cc0 | 140:8de5e8256224 |
---|---|
49 int pointNum; | 49 int pointNum; |
50 | 50 |
51 FeaturePointMatch(FeatureTrajectoryPtr _feature, const int& _pointNum): | 51 FeaturePointMatch(FeatureTrajectoryPtr _feature, const int& _pointNum): |
52 feature(_feature), pointNum(_pointNum) {} | 52 feature(_feature), pointNum(_pointNum) {} |
53 }; | 53 }; |
54 | |
55 inline void saveFeatures(vector<FeatureTrajectoryPtr>& features, TrajectoryDBAccess<Point2f>& db, const unsigned int& minNFeatures = 0) { | |
56 if (features.size() >= minNFeatures) { | |
57 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db); | |
58 features.clear(); | |
59 } | |
60 } | |
54 | 61 |
55 int main(int argc, char *argv[]) { | 62 int main(int argc, char *argv[]) { |
56 // BriefDescriptorExtractor brief(32); | 63 // BriefDescriptorExtractor brief(32); |
57 // const int DESIRED_FTRS = 500; | 64 // const int DESIRED_FTRS = 500; |
58 // GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); | 65 // GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); |
108 // cout << "capture device " << argv[1] << " failed to open!" << endl; | 115 // cout << "capture device " << argv[1] << " failed to open!" << endl; |
109 // return 1; | 116 // return 1; |
110 // } | 117 // } |
111 | 118 |
112 // database | 119 // database |
113 TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessList<Point2f>(); | 120 boost::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
114 //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); | 121 //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); |
115 trajectoryDB->connect(params.databaseFilename.c_str()); | 122 trajectoryDB->connect(params.databaseFilename.c_str()); |
116 trajectoryDB->createTable(); | 123 trajectoryDB->createTable(); |
117 | 124 |
118 vector<KeyPoint> prevKpts, currKpts; | 125 vector<KeyPoint> prevKpts, currKpts; |
119 vector<Point2f> prevPts, currPts, newPts; | 126 vector<Point2f> prevPts, currPts, newPts; |
120 vector<uchar> status; | 127 vector<uchar> status; |
121 vector<float> errors; | 128 vector<float> errors; |
122 Mat prevDesc, currDesc; | 129 Mat prevDesc, currDesc; |
123 | 130 |
124 vector<FeatureTrajectoryPtr> features; | 131 vector<FeatureTrajectoryPtr> lostFeatures; |
125 vector<FeaturePointMatch> featurePointMatches; | 132 vector<FeaturePointMatch> featurePointMatches; |
126 | 133 |
127 int key = '?'; | 134 int key = '?'; |
128 unsigned int savedFeatureId=0; | 135 unsigned int savedFeatureId=0; |
129 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { | 136 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { |
160 if (deleteFeature) { | 167 if (deleteFeature) { |
161 if (iter->feature->length() >= params.minFeatureTime) { | 168 if (iter->feature->length() >= params.minFeatureTime) { |
162 iter->feature->setId(savedFeatureId); | 169 iter->feature->setId(savedFeatureId); |
163 savedFeatureId++; | 170 savedFeatureId++; |
164 /// \todo smoothing | 171 /// \todo smoothing |
165 iter->feature->write(*trajectoryDB); | 172 //iter->feature->write(*trajectoryDB); |
173 lostFeatures.push_back(iter->feature); | |
166 } | 174 } |
167 iter = featurePointMatches.erase(iter); | 175 iter = featurePointMatches.erase(iter); |
168 } else { | 176 } else { |
169 trackedPts.push_back(currPts[iter->pointNum]); | 177 trackedPts.push_back(currPts[iter->pointNum]); |
170 iter->pointNum = trackedPts.size()-1; | 178 iter->pointNum = trackedPts.size()-1; |
171 iter++; | 179 iter++; |
172 } | 180 } |
173 } | 181 } |
174 currPts = trackedPts; | 182 currPts = trackedPts; |
175 assert(currPts.size() == featurePointMatches.size()); | 183 assert(currPts.size() == featurePointMatches.size()); |
184 saveFeatures(lostFeatures, *trajectoryDB); | |
176 | 185 |
177 if (params.display) { | 186 if (params.display) { |
178 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) | 187 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) |
179 fp.feature->draw(frame, Colors::red()); | 188 fp.feature->draw(frame, Colors::red()); |
180 } | 189 } |
193 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) | 202 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) |
194 featureMask.at<uchar>(i,j)=0; | 203 featureMask.at<uchar>(i,j)=0; |
195 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.windowSize, params.useHarrisDetector, params.k); | 204 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.windowSize, params.useHarrisDetector, params.k); |
196 BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) { | 205 BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) { |
197 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p)); | 206 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p)); |
198 //features.push_back(f); | |
199 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); | 207 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); |
200 currPts.push_back(p); | 208 currPts.push_back(p); |
201 } | 209 } |
202 // currPts.insert(currPts.end(), newPts.begin(), newPts.end()); | 210 // currPts.insert(currPts.end(), newPts.begin(), newPts.end()); |
203 //::keyPoints2Points(currKpts, currPts, false); | 211 //::keyPoints2Points(currKpts, currPts, false); |