Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 480:f43bc0b0ba74
cleaning code
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 01 Apr 2014 17:42:40 -0400 |
| parents | ca5784652d57 |
| children | b6ad86ee7033 |
comparison
equal
deleted
inserted
replaced
| 479:7828fec8bbd2 | 480:f43bc0b0ba74 |
|---|---|
| 57 | 57 |
| 58 FeaturePointMatch(FeatureTrajectoryPtr _feature, const int& _pointNum): | 58 FeaturePointMatch(FeatureTrajectoryPtr _feature, const int& _pointNum): |
| 59 feature(_feature), pointNum(_pointNum) {} | 59 feature(_feature), pointNum(_pointNum) {} |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 inline void saveFeatures(vector<FeatureTrajectoryPtr>& features, TrajectoryDBAccess<Point2f>& db, const string& positionsTableName, const string& velocitiesTableName, const unsigned int& minNFeatures = 0) { | 62 inline void saveFeatures(vector<FeatureTrajectoryPtr>& features, TrajectoryDBAccess<Point2f>& db, const string& positionsTableName, const string& velocitiesTableName) { |
| 63 /// \todo smoothing | 63 /// \todo smoothing |
| 64 if (features.size() >= minNFeatures) { | 64 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db, positionsTableName, velocitiesTableName); |
| 65 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db, positionsTableName, velocitiesTableName); | 65 features.clear(); |
| 66 features.clear(); | |
| 67 } | |
| 68 } | 66 } |
| 69 | 67 |
| 70 void trackFeatures(const KLTFeatureTrackingParameters& params) { | 68 void trackFeatures(const KLTFeatureTrackingParameters& params) { |
| 71 // BriefDescriptorExtractor brief(32); | 69 // BriefDescriptorExtractor brief(32); |
| 72 // const int DESIRED_FTRS = 500; | 70 // const int DESIRED_FTRS = 500; |
| 143 if (!success || frame.empty() || frame.size() != videoSize) | 141 if (!success || frame.empty() || frame.size() != videoSize) |
| 144 break; | 142 break; |
| 145 | 143 |
| 146 if (frameNum%50 ==0) | 144 if (frameNum%50 ==0) |
| 147 cout << "frame " << frameNum << endl; | 145 cout << "frame " << frameNum << endl; |
| 148 | |
| 149 //capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; | |
| 150 | |
| 151 // int emptyFrameNum = 0; | |
| 152 // while (frame.empty()) { | |
| 153 // cerr << "empty frame " << emptyFrameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES)<< endl; | |
| 154 // capture >> frame;//break; | |
| 155 // emptyFrameNum++; | |
| 156 // if (emptyFrameNum>=3000) | |
| 157 // exit(0); | |
| 158 // } | |
| 159 | 146 |
| 160 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); | 147 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); |
| 161 // "normal" feature detectors: detect features here | |
| 162 // detector.detect(currentFrameBW, currKpts); // see video_homography c++ sample | |
| 163 | 148 |
| 164 if (!prevPts.empty()) { | 149 if (!prevPts.empty()) { |
| 165 //::keyPoints2Points(prevKpts, prevPts); | |
| 166 currPts.clear(); | 150 currPts.clear(); |
| 167 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), /* int flags = */ 0, params.minFeatureEigThreshold); | 151 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), /* int flags = */ 0, params.minFeatureEigThreshold); |
| 168 /// \todo try calcOpticalFlowFarneback | 152 /// \todo try calcOpticalFlowFarneback |
| 169 | 153 |
| 170 std::vector<Point2f> trackedPts; | 154 std::vector<Point2f> trackedPts; |
| 173 bool deleteFeature = false; | 157 bool deleteFeature = false; |
| 174 | 158 |
| 175 if (status[iter->pointNum]) { | 159 if (status[iter->pointNum]) { |
| 176 iter->feature->addPoint(frameNum, currPts[iter->pointNum], homography); | 160 iter->feature->addPoint(frameNum, currPts[iter->pointNum], homography); |
| 177 | 161 |
| 178 deleteFeature |= iter->feature->isDisplacementSmall(params.nDisplacements, minTotalFeatureDisplacement) | 162 deleteFeature = iter->feature->isDisplacementSmall(params.nDisplacements, minTotalFeatureDisplacement) |
| 179 || !iter->feature->isMotionSmooth(params.accelerationBound, params.deviationBound); | 163 || !iter->feature->isMotionSmooth(params.accelerationBound, params.deviationBound); |
| 180 if (deleteFeature) | 164 if (deleteFeature) |
| 181 iter->feature->shorten(); | 165 iter->feature->shorten(); |
| 182 } else | 166 } else |
| 183 deleteFeature = true; | 167 deleteFeature = true; |
| 206 // vector<Rect> locations; | 190 // vector<Rect> locations; |
| 207 // hog.detectMultiScale(frame, locations, 0, Size(8,8), Size(32,32), 1.05, 2); | 191 // hog.detectMultiScale(frame, locations, 0, Size(8,8), Size(32,32), 1.05, 2); |
| 208 // BOOST_FOREACH(Rect r, locations) | 192 // BOOST_FOREACH(Rect r, locations) |
| 209 // rectangle(frame, r.tl(), r.br(), cv::Scalar(0,255,0), 3); | 193 // rectangle(frame, r.tl(), r.br(), cv::Scalar(0,255,0), 3); |
| 210 } | 194 } |
| 211 //drawOpticalFlow(prevPts, currPts, status, frame); | |
| 212 | |
| 213 // cout << matches.size() << " matches" << endl; | |
| 214 // descMatcher.match(currDesc, prevDesc, matches); | |
| 215 // cout << matches.size() << " matches" << endl; | |
| 216 //drawMatchesRelative(prevKpts, currKpts, matches, frame); | |
| 217 } | 195 } |
| 218 | 196 |
| 219 // adding new features, using mask around existing feature positions | 197 // adding new features, using mask around existing feature positions |
| 220 Mat featureMask = mask.clone(); | 198 Mat featureMask = mask.clone(); |
| 221 for (unsigned int n=0;n<currPts.size(); n++) | 199 for (unsigned int n=0;n<currPts.size(); n++) |
| 226 BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) { | 204 BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) { |
| 227 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p, homography)); | 205 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p, homography)); |
| 228 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); | 206 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); |
| 229 currPts.push_back(p); | 207 currPts.push_back(p); |
| 230 } | 208 } |
| 231 // currPts.insert(currPts.end(), newPts.begin(), newPts.end()); | |
| 232 //::keyPoints2Points(currKpts, currPts, false); | |
| 233 | |
| 234 //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location | |
| 235 | 209 |
| 236 if (params.display) { | 210 if (params.display) { |
| 237 imshow("mask", featureMask*256); | 211 imshow("mask", featureMask*256); |
| 238 imshow("frame", frame); | 212 imshow("frame", frame); |
| 239 key = waitKey(2); | 213 key = waitKey(2); |
| 240 } | 214 } |
| 241 previousFrameBW = currentFrameBW.clone(); | 215 previousFrameBW = currentFrameBW.clone(); |
| 242 prevPts = currPts; | 216 prevPts = currPts; |
| 243 //prevKpts = currKpts; | 217 } |
| 244 //currDesc.copyTo(prevDesc); | 218 |
| 245 } | 219 // save the remaining currently tracked features |
| 220 std::vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin(); | |
| 221 while (iter != featurePointMatches.end()) { | |
| 222 if (iter->feature->length() >= params.minFeatureTime) { | |
| 223 iter->feature->setId(savedFeatureId); | |
| 224 savedFeatureId++; | |
| 225 f->write(*trajectoryDB, "positions", "velocities") | |
| 226 } | |
| 227 iter++; | |
| 228 } | |
| 246 | 229 |
| 247 trajectoryDB->endTransaction(); | 230 trajectoryDB->endTransaction(); |
| 248 trajectoryDB->disconnect(); | 231 trajectoryDB->disconnect(); |
| 249 } | 232 } |
| 250 | 233 |
