Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 654:045d05cef9d0
updating to c++11 capabilities
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 07 May 2015 16:09:47 +0200 |
| parents | a3add9f751ef |
| children | 39fa1c998b29 |
comparison
equal
deleted
inserted
replaced
| 653:107f1ad02b69 | 654:045d05cef9d0 |
|---|---|
| 14 #include "opencv2/video/tracking.hpp" | 14 #include "opencv2/video/tracking.hpp" |
| 15 #include "opencv2/features2d/features2d.hpp" | 15 #include "opencv2/features2d/features2d.hpp" |
| 16 #include "opencv2/highgui/highgui.hpp" | 16 #include "opencv2/highgui/highgui.hpp" |
| 17 #include "opencv2/objdetect/objdetect.hpp" | 17 #include "opencv2/objdetect/objdetect.hpp" |
| 18 | 18 |
| 19 #include <boost/shared_ptr.hpp> | |
| 20 #include <boost/foreach.hpp> | 19 #include <boost/foreach.hpp> |
| 21 #include <boost/filesystem.hpp> | 20 #include <boost/filesystem.hpp> |
| 22 | 21 |
| 23 #include <iostream> | 22 #include <iostream> |
| 24 #include <vector> | 23 #include <vector> |
| 25 #include <ctime> | 24 #include <ctime> |
| 26 #include <cmath> | 25 #include <cmath> |
| 26 #include <memory> | |
| 27 | 27 |
| 28 using namespace std; | 28 using namespace std; |
| 29 using namespace cv; | 29 using namespace cv; |
| 30 namespace fs = boost::filesystem; | 30 namespace fs = boost::filesystem; |
| 31 | 31 |
| 90 cout << "Unsupported option " << interpolationMethod << " for interpolation method" << endl; | 90 cout << "Unsupported option " << interpolationMethod << " for interpolation method" << endl; |
| 91 | 91 |
| 92 // BruteForceMatcher<Hamming> descMatcher; | 92 // BruteForceMatcher<Hamming> descMatcher; |
| 93 // vector<DMatch> matches; | 93 // vector<DMatch> matches; |
| 94 | 94 |
| 95 boost::shared_ptr<InputFrameProviderIface> capture; | 95 std::shared_ptr<InputFrameProviderIface> capture; |
| 96 if (fs::is_directory(fs::path(params.videoFilename))) | 96 if (fs::is_directory(fs::path(params.videoFilename))) |
| 97 capture = boost::shared_ptr<InputFrameListModule>(new InputFrameListModule(params.videoFilename)); | 97 capture = std::shared_ptr<InputFrameListModule>(new InputFrameListModule(params.videoFilename)); |
| 98 else if(!params.videoFilename.empty()) | 98 else if(!params.videoFilename.empty()) |
| 99 capture = boost::shared_ptr<InputVideoFileModule>(new InputVideoFileModule(params.videoFilename)); | 99 capture = std::shared_ptr<InputVideoFileModule>(new InputVideoFileModule(params.videoFilename)); |
| 100 else | 100 else |
| 101 cout << "No valid input parameters" << endl; | 101 cout << "No valid input parameters" << endl; |
| 102 | 102 |
| 103 if(!capture->isOpen()) { | 103 if(!capture->isOpen()) { |
| 104 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; | 104 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; |
| 129 if (mask.empty()) { | 129 if (mask.empty()) { |
| 130 cout << "Mask filename " << params.maskFilename << " could not be opened." << endl; | 130 cout << "Mask filename " << params.maskFilename << " could not be opened." << endl; |
| 131 mask = Mat::ones(videoSize, CV_8UC1); | 131 mask = Mat::ones(videoSize, CV_8UC1); |
| 132 } | 132 } |
| 133 | 133 |
| 134 boost::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | 134 std::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
| 135 //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); | 135 //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); |
| 136 trajectoryDB->connect(params.databaseFilename.c_str()); | 136 trajectoryDB->connect(params.databaseFilename.c_str()); |
| 137 trajectoryDB->createTable("positions"); | 137 trajectoryDB->createTable("positions"); |
| 138 trajectoryDB->createTable("velocities"); | 138 trajectoryDB->createTable("velocities"); |
| 139 trajectoryDB->beginTransaction(); | 139 trajectoryDB->beginTransaction(); |
| 263 trajectoryDB->endTransaction(); | 263 trajectoryDB->endTransaction(); |
| 264 trajectoryDB->disconnect(); | 264 trajectoryDB->disconnect(); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void groupFeatures(const KLTFeatureTrackingParameters& params) { | 267 void groupFeatures(const KLTFeatureTrackingParameters& params) { |
| 268 boost::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | 268 std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
| 269 //TODO write generic methods for blob and list versions TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); | |
| 270 bool success = trajectoryDB->connect(params.databaseFilename.c_str()); | 269 bool success = trajectoryDB->connect(params.databaseFilename.c_str()); |
| 271 trajectoryDB->createObjectTable("objects", "objects_features"); | 270 trajectoryDB->createObjectTable("objects", "objects_features"); |
| 272 unsigned int savedObjectId=0; | 271 unsigned int savedObjectId=0; |
| 273 | |
| 274 // vector<boost::shared_ptr<Trajectory<Point2f> > > trajectories; | |
| 275 // cout << trajectories.size() << endl; | |
| 276 // std::clock_t c_start = std::clock(); | |
| 277 // success = trajectoryDB->read(trajectories, "positions"); // TODO load velocities as well in a FeatureTrajectory object // attention, velocities lack the first instant | |
| 278 // std::clock_t c_end = std::clock(); | |
| 279 // cout << "Loaded " << trajectories.size() << " trajectories in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl; | |
| 280 | |
| 281 // boost::shared_ptr<Trajectory<cv::Point2f> > trajectory; | |
| 282 // c_start = std::clock(); | |
| 283 // for (unsigned int i = 0; i<trajectories.size(); ++i) { | |
| 284 // success = trajectoryDB->read(trajectory, i, "positions"); | |
| 285 // } | |
| 286 // c_end = std::clock(); | |
| 287 // cout << "Loaded " << trajectories.size() << " trajectories one by one in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl; | |
| 288 | 272 |
| 289 trajectoryDB->createInstants("table"); | 273 trajectoryDB->createInstants("table"); |
| 290 | 274 |
| 291 unsigned int maxTrajectoryLength = 0; | 275 unsigned int maxTrajectoryLength = 0; |
| 292 success = trajectoryDB->maxTrajectoryLength(maxTrajectoryLength); | 276 success = trajectoryDB->maxTrajectoryLength(maxTrajectoryLength); |
| 319 // trajectoryDB->read(velocities, trajectoryIds, "velocities"); | 303 // trajectoryDB->read(velocities, trajectoryIds, "velocities"); |
| 320 // for (unsigned int i=0; i<trajectoryIds.size(); ++i) { | 304 // for (unsigned int i=0; i<trajectoryIds.size(); ++i) { |
| 321 // FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(positions[i], velocities[i])); | 305 // FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(positions[i], velocities[i])); |
| 322 BOOST_FOREACH(int trajectoryId, trajectoryIds) { | 306 BOOST_FOREACH(int trajectoryId, trajectoryIds) { |
| 323 //cout << trajectoryId << " " << endl; | 307 //cout << trajectoryId << " " << endl; |
| 324 // boost::shared_ptr<Trajectory<cv::Point2f> > trajectory; | 308 // std::shared_ptr<Trajectory<cv::Point2f> > trajectory; |
| 325 // success = trajectoryDB->read(trajectory, trajectoryId, "positions"); // velocities | 309 // success = trajectoryDB->read(trajectory, trajectoryId, "positions"); // velocities |
| 326 FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(trajectoryId, *trajectoryDB, "positions", "velocities")); | 310 FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(trajectoryId, *trajectoryDB, "positions", "velocities")); |
| 327 // stringstream ss;ss << *ft; cout << ss.str() << endl; | 311 // stringstream ss;ss << *ft; cout << ss.str() << endl; |
| 328 // cout << ft->getFirstInstant() << " " << ft->getLastInstant() << endl; | 312 // cout << ft->getFirstInstant() << " " << ft->getLastInstant() << endl; |
| 329 featureGraph.addFeature(ft); | 313 featureGraph.addFeature(ft); |
| 360 savedObjectId++; | 344 savedObjectId++; |
| 361 } | 345 } |
| 362 | 346 |
| 363 trajectoryDB->endTransaction(); | 347 trajectoryDB->endTransaction(); |
| 364 trajectoryDB->disconnect(); | 348 trajectoryDB->disconnect(); |
| 365 } | 349 } |
| 350 | |
| 351 void loadingTimes(const KLTFeatureTrackingParameters& params) { | |
| 352 std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | |
| 353 bool success = trajectoryDB->connect(params.databaseFilename.c_str()); | |
| 354 | |
| 355 vector<std::shared_ptr<Trajectory<Point2f> > > trajectories; | |
| 356 //cout << trajectories.size() << endl; | |
| 357 std::clock_t c_start = std::clock(); | |
| 358 success = trajectoryDB->read(trajectories, "positions"); | |
| 359 std::clock_t c_end = std::clock(); | |
| 360 if (!success) | |
| 361 cout << "Issue with db reading" << endl; | |
| 362 cout << "Loaded " << trajectories.size() << " trajectories in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl; | |
| 363 | |
| 364 std::shared_ptr<Trajectory<cv::Point2f> > trajectory; | |
| 365 c_start = std::clock(); | |
| 366 for (unsigned int i = 0; i<trajectories.size(); ++i) { | |
| 367 success = trajectoryDB->read(trajectory, i, "positions"); | |
| 368 } | |
| 369 c_end = std::clock(); | |
| 370 cout << "Loaded " << trajectories.size() << " trajectories one by one in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl; | |
| 371 | |
| 372 trajectoryDB->endTransaction(); | |
| 373 trajectoryDB->disconnect(); | |
| 374 } | |
| 366 | 375 |
| 367 int main(int argc, char *argv[]) { | 376 int main(int argc, char *argv[]) { |
| 368 KLTFeatureTrackingParameters params(argc, argv); | 377 KLTFeatureTrackingParameters params(argc, argv); |
| 369 cout << params.parameterDescription << endl; | 378 cout << params.parameterDescription << endl; |
| 370 | 379 |
| 371 if (params.trackFeatures) { | 380 if (params.trackFeatures) { |
| 372 cout << "The program tracks features" << endl; | 381 cout << "The program tracks features" << endl; |
| 373 trackFeatures(params); | 382 trackFeatures(params); |
| 374 } else if (params.groupFeatures) { | 383 } else if (params.groupFeatures) { |
| 375 cout << "The program groups features" << endl; | 384 cout << "The program groups features" << endl; |
| 376 groupFeatures(params); | 385 groupFeatures(params); |
| 386 } else if (params.loadingTime) { | |
| 387 cout << "The program reports loading times" << endl; | |
| 388 loadingTimes(params); | |
| 377 } else { | 389 } else { |
| 378 cout << "Main option missing or misspelt" << endl; | 390 cout << "Main option missing or misspelt" << endl; |
| 379 } | 391 } |
| 380 | 392 |
| 381 return 0; | 393 return 0; |
