Mercurial Hosting > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 231:249d65ff6c35
merged modifications for windows
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 02 Jul 2012 23:49:39 -0400 |
parents | bc4ea09b1743 23da16442433 |
children | 2d34060db2e9 |
comparison
equal
deleted
inserted
replaced
230:bc4ea09b1743 | 231:249d65ff6c35 |
---|---|
74 | 74 |
75 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; | 75 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; |
76 Size window = Size(params.windowSize, params.windowSize); | 76 Size window = Size(params.windowSize, params.windowSize); |
77 | 77 |
78 // BruteForceMatcher<Hamming> descMatcher; | 78 // BruteForceMatcher<Hamming> descMatcher; |
79 // std::vector<DMatch> matches; | 79 // vector<DMatch> matches; |
80 | |
81 VideoCapture capture; | |
80 Size videoSize; | 82 Size videoSize; |
81 | 83 unsigned int nFrames = 0; |
82 // if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter | |
83 // capture.open(argc == 2 ? argv[1][0] - '0' : 0); | |
84 // else if( argc >= 2 ) | |
85 // { | |
86 // capture.open(argv[1]); | |
87 // if( capture.isOpened() ) | |
88 // videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); | |
89 // cout << "Video " << argv[1] << | |
90 // ": width=" << videoSize.width << | |
91 // ", height=" << videoSize.height << | |
92 // ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl; | |
93 // if( argc > 2 && isdigit(argv[2][0]) ) // could be used to reach first frame, dumping library messages to log file (2> /tmp/log.txt) | |
94 // { | |
95 // sscanf(argv[2], "%d", ¶ms.frame1); | |
96 // cout << "seeking to frame #" << params.frame1 << endl; | |
97 // //cap.set(CV_CAP_PROP_POS_FRAMES, pos); | |
98 // for (int i=0; i<params.frame1; i++) | |
99 // capture >> frame; | |
100 // } | |
101 // } | |
102 | |
103 VideoCapture capture; | |
104 capture.open(params.videoFilename); | 84 capture.open(params.videoFilename); |
105 if(capture.isOpened()) { | 85 if(capture.isOpened()) { |
106 videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); | 86 videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); |
87 nFrames = capture.get(CV_CAP_PROP_FRAME_COUNT); | |
107 cout << "Video " << params.videoFilename << | 88 cout << "Video " << params.videoFilename << |
108 ": width=" << videoSize.width << | 89 ": width=" << videoSize.width << |
109 ", height=" << videoSize.height << | 90 ", height=" << videoSize.height << |
110 ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl; | 91 ", nframes=" << nFrames << endl; |
111 } else { | 92 } else { |
112 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; | 93 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; |
113 exit(0); | 94 exit(0); |
114 } | 95 } |
115 // if (!capture.isOpened()) | 96 // if (!capture.isOpened()) |
143 hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); | 124 hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); |
144 | 125 |
145 int key = '?'; | 126 int key = '?'; |
146 unsigned int savedFeatureId=0; | 127 unsigned int savedFeatureId=0; |
147 Mat frame, currentFrameBW, previousFrameBW; | 128 Mat frame, currentFrameBW, previousFrameBW; |
148 for (int frameNum = params.frame1; ((frameNum-params.frame1 < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { | 129 |
130 unsigned int lastFrameNum = params.frame1+nFrames; | |
131 if (params.nFrames > 0) | |
132 lastFrameNum = MIN(params.frame1+params.nFrames, nFrames); | |
133 | |
134 capture.set(CV_CAP_PROP_POS_FRAMES, params.frame1); | |
135 for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) { | |
149 capture >> frame; | 136 capture >> frame; |
150 cout << frameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; | 137 if (frameNum%50 ==0) |
151 int emptyFrameNum = 0; | 138 cout << "frame " << frameNum << endl; |
152 while (frame.empty()) { | 139 //capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; |
153 cerr << "empty frame " << emptyFrameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES)<< endl; | 140 |
154 capture >> frame;//break; | 141 // int emptyFrameNum = 0; |
155 emptyFrameNum++; | 142 // while (frame.empty()) { |
156 if (emptyFrameNum>=3000) | 143 // cerr << "empty frame " << emptyFrameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES)<< endl; |
157 exit(0); | 144 // capture >> frame;//break; |
158 } | 145 // emptyFrameNum++; |
146 // if (emptyFrameNum>=3000) | |
147 // exit(0); | |
148 // } | |
159 | 149 |
160 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); | 150 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); |
161 // "normal" feature detectors: detect features here | 151 // "normal" feature detectors: detect features here |
162 // detector.detect(currentFrameBW, currKpts); // see video_homography c++ sample | 152 // detector.detect(currentFrameBW, currKpts); // see video_homography c++ sample |
163 | 153 |
247 trajectoryDB->endTransaction(); | 237 trajectoryDB->endTransaction(); |
248 trajectoryDB->disconnect(); | 238 trajectoryDB->disconnect(); |
249 } | 239 } |
250 | 240 |
251 void groupFeatures(const KLTFeatureTrackingParameters& params) { | 241 void groupFeatures(const KLTFeatureTrackingParameters& params) { |
252 cout << "group" << endl; | |
253 | |
254 boost::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | 242 boost::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
255 //TODO write generic methods for blob and list versions TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); | 243 //TODO write generic methods for blob and list versions TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); |
256 bool success = trajectoryDB->connect(params.databaseFilename.c_str()); | 244 bool success = trajectoryDB->connect(params.databaseFilename.c_str()); |
257 trajectoryDB->createObjectTable("objects", "objects_features"); | 245 trajectoryDB->createObjectTable("objects", "objects_features"); |
258 unsigned int savedObjectId=0; | 246 unsigned int savedObjectId=0; |
270 // success = trajectoryDB->read(trajectory, i, "positions"); | 258 // success = trajectoryDB->read(trajectory, i, "positions"); |
271 // } | 259 // } |
272 // c_end = std::clock(); | 260 // c_end = std::clock(); |
273 // cout << "Loaded " << trajectories.size() << " trajectories one by one in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl; | 261 // cout << "Loaded " << trajectories.size() << " trajectories one by one in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl; |
274 | 262 |
275 trajectoryDB->createViewInstants(); | 263 trajectoryDB->createInstants("table"); |
276 int maxTrajectoryLength; | 264 |
277 trajectoryDB->maxTrajectoryLength(maxTrajectoryLength); | 265 unsigned int maxTrajectoryLength = 0; |
278 cout << "max trajectory length " << maxTrajectoryLength << endl; | 266 success = trajectoryDB->maxTrajectoryLength(maxTrajectoryLength); |
279 maxTrajectoryLength = 20; // for tests | 267 if (!success || maxTrajectoryLength == 0) { |
268 cout << "problem with trajectory length " << success << endl; | |
269 exit(0); | |
270 } | |
271 cout << "Longest trajectory: " << maxTrajectoryLength << endl; | |
280 | 272 |
281 FeatureGraph featureGraph(params.mmConnectionDistance, params.mmSegmentationDistance, params.minFeatureTime, params.minNFeaturesPerGroup); | 273 FeatureGraph featureGraph(params.mmConnectionDistance, params.mmSegmentationDistance, params.minFeatureTime, params.minNFeaturesPerGroup); |
282 | 274 |
283 // main loop | 275 // main loop |
284 // TODO version that can be interrupted? | 276 unsigned int frameNum; |
285 for (int frameNum = params.frame1; ((frameNum-params.frame1 < params.nFrames) || (params.nFrames < 0)); frameNum++) { | 277 unsigned int firstFrameNum = -1, lastFrameNum = -1; |
286 std::vector<int> trajectoryIds; | 278 trajectoryDB->firstLastInstants(firstFrameNum, lastFrameNum); |
287 success = trajectoryDB->trajectoryIdEndingAt(trajectoryIds, frameNum); // ending | 279 firstFrameNum = MAX(firstFrameNum, params.frame1); |
288 cout << "frame " << frameNum << " " << success << endl; | 280 if (params.nFrames>0) |
281 lastFrameNum = MIN(lastFrameNum,params.frame1+params.nFrames); | |
282 for (frameNum = firstFrameNum; frameNum<lastFrameNum; frameNum ++) { | |
283 vector<int> trajectoryIds; | |
284 success = trajectoryDB->trajectoryIdEndingAt(trajectoryIds, frameNum); | |
285 if (frameNum%100 ==0) | |
286 cout << "frame " << frameNum << endl; | |
287 //success = trajectoryDB->trajectoryIdInInterval(trajectoryIds, frameNum, min(frameNum+queryIntervalLength-1, frameNum+params.nFrames)); // ending | |
288 #if DEBUG | |
289 cout << trajectoryIds.size() << " trajectories " << endl; | 289 cout << trajectoryIds.size() << " trajectories " << endl; |
290 #endif | |
291 // vector<TrajectoryPoint2fPtr> positions, velocities; | |
292 // trajectoryDB->read(positions, trajectoryIds, "positions"); | |
293 // trajectoryDB->read(velocities, trajectoryIds, "velocities"); | |
294 // for (unsigned int i=0; i<trajectoryIds.size(); ++i) { | |
295 // FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(positions[i], velocities[i])); | |
290 BOOST_FOREACH(int trajectoryId, trajectoryIds) { | 296 BOOST_FOREACH(int trajectoryId, trajectoryIds) { |
291 //cout << trajectoryId << " " << endl; | 297 //cout << trajectoryId << " " << endl; |
292 // boost::shared_ptr<Trajectory<cv::Point2f> > trajectory; | 298 // boost::shared_ptr<Trajectory<cv::Point2f> > trajectory; |
293 // success = trajectoryDB->read(trajectory, trajectoryId, "positions"); // velocities | 299 // success = trajectoryDB->read(trajectory, trajectoryId, "positions"); // velocities |
294 FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(trajectoryId, *trajectoryDB, "positions", "velocities")); | 300 FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(trajectoryId, *trajectoryDB, "positions", "velocities")); |
295 // stringstream ss;ss << *ft; cout << ss.str() << endl; | 301 // stringstream ss;ss << *ft; cout << ss.str() << endl; |
296 // cout << ft->getFirstInstant() << " " << ft->getLastInstant() << endl; | 302 // cout << ft->getFirstInstant() << " " << ft->getLastInstant() << endl; |
297 featureGraph.addFeature(ft); | 303 featureGraph.addFeature(ft); |
298 } | 304 } |
299 | 305 |
300 // check for connected components that are old enough (no chance to match with trajectories to be added later | 306 // check for connected components |
301 // we could check only when some features are getting old enough? | 307 int lastInstant = frameNum+params.minFeatureTime-maxTrajectoryLength; |
302 if (frameNum%10 == 0) { | 308 if (lastInstant > 0 && frameNum%10==0) { |
303 featureGraph.connectedComponents(frameNum-maxTrajectoryLength+params.minFeatureTime); | 309 featureGraph.connectedComponents(lastInstant); |
304 std::vector<vector<unsigned int> > featureGroups = featureGraph.getFeatureGroups(); | 310 vector<vector<unsigned int> > featureGroups = featureGraph.getFeatureGroups(); |
305 for (unsigned int i=0; i<featureGroups.size(); ++i) { | 311 for (unsigned int i=0; i<featureGroups.size(); ++i) { |
306 trajectoryDB->writeObject(savedObjectId, featureGroups[i], -1, 1, string("objects"), string("objects_features")); | 312 trajectoryDB->writeObject(savedObjectId, featureGroups[i], -1, 1, string("objects"), string("objects_features")); |
307 savedObjectId++; | 313 savedObjectId++; |
308 } | 314 } |
309 } | 315 } |
310 | 316 |
311 cout << featureGraph.informationString() << endl; | 317 if (frameNum%100 ==0) |
318 cout << featureGraph.informationString() << endl; | |
319 } | |
320 | |
321 // save remaining objects | |
322 featureGraph.connectedComponents(frameNum+maxTrajectoryLength+1); | |
323 vector<vector<unsigned int> > featureGroups = featureGraph.getFeatureGroups(); | |
324 for (unsigned int i=0; i<featureGroups.size(); ++i) { | |
325 trajectoryDB->writeObject(savedObjectId, featureGroups[i], -1, 1, string("objects"), string("objects_features")); | |
326 savedObjectId++; | |
312 } | 327 } |
313 | 328 |
314 trajectoryDB->endTransaction(); | 329 trajectoryDB->endTransaction(); |
315 trajectoryDB->disconnect(); | 330 trajectoryDB->disconnect(); |
316 } | 331 } |