comparison c/feature-based-tracking.cpp @ 498:ef4059f51af9

merged
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 05 May 2014 23:17:46 -0400
parents b96ff16b1c81
children 081a9da6f85b
comparison
equal deleted inserted replaced
497:6ebdd90ce3ee 498:ef4059f51af9
1 #include "Motion.hpp" 1 #include "Motion.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 #include "InputVideoFileModule.h"
6 #include "InputFrameListModule.h"
5 7
6 #include "src/Trajectory.h" 8 #include "src/Trajectory.h"
7 #include "src/TrajectoryDBAccessList.h" 9 #include "src/TrajectoryDBAccessList.h"
8 #include "src/TrajectoryDBAccessBlob.h" 10 #include "src/TrajectoryDBAccessBlob.h"
9 11
15 #include "opencv2/objdetect/objdetect.hpp" 17 #include "opencv2/objdetect/objdetect.hpp"
16 18
17 #include <boost/shared_ptr.hpp> 19 #include <boost/shared_ptr.hpp>
18 #include <boost/foreach.hpp> 20 #include <boost/foreach.hpp>
19 #include <boost/filesystem.hpp> 21 #include <boost/filesystem.hpp>
20
21 #include "InputVideoFileModule.h"
22 #include "InputFrameListModule.h"
23 22
24 #include <iostream> 23 #include <iostream>
25 #include <vector> 24 #include <vector>
26 #include <ctime> 25 #include <ctime>
27 26
131 Mat frame = Mat::zeros(1, 1, CV_8UC1), currentFrameBW, previousFrameBW; 130 Mat frame = Mat::zeros(1, 1, CV_8UC1), currentFrameBW, previousFrameBW;
132 131
133 unsigned int lastFrameNum = nFrames; 132 unsigned int lastFrameNum = nFrames;
134 if (params.nFrames > 0) 133 if (params.nFrames > 0)
135 lastFrameNum = MIN(params.frame1+static_cast<unsigned int>(params.nFrames), nFrames); 134 lastFrameNum = MIN(params.frame1+static_cast<unsigned int>(params.nFrames), nFrames);
136 135
137 capture->setFrameNumber(params.frame1); 136 capture->setFrameNumber(params.frame1);
138 for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) { 137 for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) {
139 bool success = capture->getNextFrame(frame); 138 bool success = capture->getNextFrame(frame);
140 139
141 if (!success || frame.empty() || frame.size() != videoSize) 140 if (!success || frame.empty() || frame.size() != videoSize)
146 145
147 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); 146 cvtColor(frame, currentFrameBW, CV_RGB2GRAY);
148 147
149 if (!prevPts.empty()) { 148 if (!prevPts.empty()) {
150 currPts.clear(); 149 currPts.clear();
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); 150 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);
152 /// \todo try calcOpticalFlowFarneback 151 /// \todo try calcOpticalFlowFarneback
153 152
154 std::vector<Point2f> trackedPts; 153 std::vector<Point2f> trackedPts;
155 std::vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin(); 154 std::vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin();
156 while (iter != featurePointMatches.end()) { 155 while (iter != featurePointMatches.end()) {
199 Mat featureMask = mask.clone(); 198 Mat featureMask = mask.clone();
200 for (unsigned int n=0;n<currPts.size(); n++) 199 for (unsigned int n=0;n<currPts.size(); n++)
201 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++) 200 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++)
202 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) 201 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++)
203 featureMask.at<uchar>(i,j)=0; 202 featureMask.at<uchar>(i,j)=0;
204 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.windowSize, params.useHarrisDetector, params.k); 203 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.blockSize, params.useHarrisDetector, params.k);
205 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++) {
206 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p, homography)); 205 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p, homography));
207 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); 206 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size()));
208 currPts.push_back(p); 207 currPts.push_back(p);
209 } 208 }