Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 800:2cade72d75ad dev
modified so there is no tracking outside of the mask (does not continue if features can still be matched) as requested
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 31 May 2016 17:06:41 -0400 |
| parents | 85af65b6d531 |
| children | d3e8dd9f3ca4 daa992ac6b44 |
comparison
equal
deleted
inserted
replaced
| 799:0662c87a61c9 | 800:2cade72d75ad |
|---|---|
| 60 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db, positionsTableName, velocitiesTableName); | 60 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db, positionsTableName, velocitiesTableName); |
| 61 features.clear(); | 61 features.clear(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 void trackFeatures(const KLTFeatureTrackingParameters& params) { | 64 void trackFeatures(const KLTFeatureTrackingParameters& params) { |
| 65 // BriefDescriptorExtractor brief(32); | |
| 66 // const int DESIRED_FTRS = 500; | |
| 67 // GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); | |
| 68 | |
| 69 Mat homography = ::loadMat(params.homographyFilename, " "); | 65 Mat homography = ::loadMat(params.homographyFilename, " "); |
| 70 Mat invHomography; | 66 Mat invHomography; |
| 71 if (params.display && !homography.empty()) | 67 if (params.display && !homography.empty()) |
| 72 invHomography = homography.inv(); | 68 invHomography = homography.inv(); |
| 73 | 69 |
| 172 if (frame.size() != videoSize) { | 168 if (frame.size() != videoSize) { |
| 173 cout << "Different frame size " << frameNum << ", breaking ([" << frame.size().width << "x" << frame.size().height << "])" << endl; | 169 cout << "Different frame size " << frameNum << ", breaking ([" << frame.size().width << "x" << frame.size().height << "])" << endl; |
| 174 break; | 170 break; |
| 175 } | 171 } |
| 176 } | 172 } |
| 177 | 173 |
| 178 | |
| 179 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); | 174 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); |
| 180 | 175 |
| 181 if (!prevPts.empty()) { | 176 if (!prevPts.empty()) { |
| 182 currPts.clear(); | 177 currPts.clear(); |
| 183 calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(static_cast<int>(TermCriteria::COUNT)+static_cast<int>(TermCriteria::EPS) /* = 3 */, params.maxNumberTrackingIterations, params.minTrackingError), /* int flags = */ 0, params.minFeatureEigThreshold); | 178 calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(static_cast<int>(TermCriteria::COUNT)+static_cast<int>(TermCriteria::EPS) /* = 3 */, params.maxNumberTrackingIterations, params.minTrackingError), /* int flags = */ 0, params.minFeatureEigThreshold); |
| 186 std::vector<Point2f> trackedPts; | 181 std::vector<Point2f> trackedPts; |
| 187 std::vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin(); | 182 std::vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin(); |
| 188 while (iter != featurePointMatches.end()) { | 183 while (iter != featurePointMatches.end()) { |
| 189 bool deleteFeature = false; | 184 bool deleteFeature = false; |
| 190 | 185 |
| 191 if (status[iter->pointNum]) { | 186 if (status[iter->pointNum] && (mask.at<uchar>(static_cast<int>(round(currPts[iter->pointNum].y)), static_cast<int>(round(currPts[iter->pointNum].x))) != 0)) { |
| 192 iter->feature->addPoint(frameNum, currPts[iter->pointNum], homography); | 187 iter->feature->addPoint(frameNum, currPts[iter->pointNum], homography); |
| 193 | 188 |
| 194 deleteFeature = iter->feature->isDisplacementSmall(params.nDisplacements, minTotalFeatureDisplacement) | 189 deleteFeature = iter->feature->isDisplacementSmall(params.nDisplacements, minTotalFeatureDisplacement) |
| 195 || !iter->feature->isMotionSmooth(params.accelerationBound, params.deviationBound); | 190 || !iter->feature->isMotionSmooth(params.accelerationBound, params.deviationBound); |
| 196 if (deleteFeature) | 191 if (deleteFeature) |
| 197 iter->feature->shorten(); | 192 iter->feature->shorten(); |
| 198 } else | 193 } else |
| 217 saveFeatures(lostFeatures, *trajectoryDB, "positions", "velocities"); | 212 saveFeatures(lostFeatures, *trajectoryDB, "positions", "velocities"); |
| 218 | 213 |
| 219 if (params.display) { | 214 if (params.display) { |
| 220 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) | 215 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) |
| 221 fp.feature->draw(frame, invHomography, Colors::red()); | 216 fp.feature->draw(frame, invHomography, Colors::red()); |
| 222 // object detection | |
| 223 // vector<Rect> locations; | |
| 224 // hog.detectMultiScale(frame, locations, 0, Size(8,8), Size(32,32), 1.05, 2); | |
| 225 // BOOST_FOREACH(Rect r, locations) | |
| 226 // rectangle(frame, r.tl(), r.br(), cv::Scalar(0,255,0), 3); | |
| 227 } | 217 } |
| 228 } | 218 } |
| 229 | 219 |
| 230 // adding new features, using mask around existing feature positions | 220 // adding new features, using mask around existing feature positions |
| 231 Mat featureMask = mask.clone(); | 221 Mat featureMask = mask.clone(); |
| 232 for (unsigned int n=0;n<currPts.size(); n++) | 222 for (unsigned int n=0;n<currPts.size(); n++) |
| 233 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++) | 223 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++) |
| 234 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) | 224 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) |
| 235 featureMask.at<uchar>(i,j)=0; | 225 featureMask.at<uchar>(i,j)=0; |
| 236 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.blockSize, params.useHarrisDetector, params.k); | 226 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.blockSize, params.useHarrisDetector, params.k); |
| 237 BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) { | 227 BOOST_FOREACH(Point2f p, newPts) { |
| 238 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p, homography)); | 228 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p, homography)); |
| 239 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); | 229 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); |
| 240 currPts.push_back(p); | 230 currPts.push_back(p); |
| 241 } | 231 } |
| 242 | 232 |
