Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 1014:026f6b3b122c
Moving pull request2
| author | Wendlasida |
|---|---|
| date | Fri, 01 Jun 2018 17:32:52 -0400 |
| parents | 6c5ce3ec497e |
| children | 9fb82fe0156f |
comparison
equal
deleted
inserted
replaced
| 1013:d6f121ded971 | 1014:026f6b3b122c |
|---|---|
| 14 #include "opencv2/highgui/highgui.hpp" | 14 #include "opencv2/highgui/highgui.hpp" |
| 15 #include "opencv2/objdetect/objdetect.hpp" | 15 #include "opencv2/objdetect/objdetect.hpp" |
| 16 #include "opencv2/calib3d/calib3d.hpp" | 16 #include "opencv2/calib3d/calib3d.hpp" |
| 17 | 17 |
| 18 #include <boost/foreach.hpp> | 18 #include <boost/foreach.hpp> |
| 19 #include <boost/filesystem.hpp> | |
| 19 | 20 |
| 20 #include <iostream> | 21 #include <iostream> |
| 21 #include <vector> | 22 #include <vector> |
| 22 #include <ctime> | 23 #include <ctime> |
| 23 #include <cmath> | 24 #include <cmath> |
| 24 #include <memory> | 25 #include <memory> |
| 25 #include <limits> | 26 #include <limits> |
| 27 | |
| 28 namespace fs = boost::filesystem; // soon std | |
| 26 | 29 |
| 27 using namespace std; | 30 using namespace std; |
| 28 using namespace cv; | 31 using namespace cv; |
| 29 | 32 |
| 30 void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) { | 33 void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) { |
| 61 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db, positionsTableName, velocitiesTableName); | 64 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db, positionsTableName, velocitiesTableName); |
| 62 features.clear(); | 65 features.clear(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 void trackFeatures(const KLTFeatureTrackingParameters& params) { | 68 void trackFeatures(const KLTFeatureTrackingParameters& params) { |
| 66 Mat homography = ::loadMat(params.homographyFilename, " "); | 69 Mat homography = ::loadMat(::getRelativeFilename(params.parentDirname, params.homographyFilename), " "); |
| 70 | |
| 67 Mat invHomography; | 71 Mat invHomography; |
| 68 if (params.display && !homography.empty()) | 72 if (params.display && !homography.empty()) |
| 69 invHomography = homography.inv(); | 73 invHomography = homography.inv(); |
| 70 | 74 |
| 71 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; | 75 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; |
| 88 | 92 |
| 89 if(params.videoFilename.empty()) { | 93 if(params.videoFilename.empty()) { |
| 90 cout << "Empty video filename. Exiting." << endl; | 94 cout << "Empty video filename. Exiting." << endl; |
| 91 exit(0); | 95 exit(0); |
| 92 } | 96 } |
| 93 | 97 |
| 94 VideoCapture capture(params.videoFilename); | 98 VideoCapture capture(::getRelativeFilename(params.parentDirname, params.videoFilename)); |
| 95 if(!capture.isOpened()) { | 99 if(!capture.isOpened()) { |
| 96 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; | 100 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; |
| 97 exit(0); | 101 exit(0); |
| 98 } | 102 } |
| 99 | 103 |
| 111 ", nframes=" << nFrames << endl; | 115 ", nframes=" << nFrames << endl; |
| 112 | 116 |
| 113 Mat map1, map2; | 117 Mat map1, map2; |
| 114 Mat intrinsicCameraMatrix, newIntrinsicCameraMatrix; | 118 Mat intrinsicCameraMatrix, newIntrinsicCameraMatrix; |
| 115 if (params.undistort) { | 119 if (params.undistort) { |
| 116 intrinsicCameraMatrix = ::loadMat(params.intrinsicCameraFilename, " "); | 120 intrinsicCameraMatrix = ::loadMat(::getRelativeFilename(params.parentDirname, params.intrinsicCameraFilename), " "); |
| 117 Size undistortedVideoSize = Size(static_cast<int>(round(videoSize.width*params.undistortedImageMultiplication)), static_cast<int>(round(videoSize.height*params.undistortedImageMultiplication))); | 121 Size undistortedVideoSize = Size(static_cast<int>(round(videoSize.width*params.undistortedImageMultiplication)), static_cast<int>(round(videoSize.height*params.undistortedImageMultiplication))); |
| 118 newIntrinsicCameraMatrix = getDefaultNewCameraMatrix(intrinsicCameraMatrix, undistortedVideoSize, true);// for some reason, it's double type //getOptimalNewCameraMatrix(intrinsicCameraMatrix, params.distortionCoefficients, videoSize, 1, undistortedVideoSize);//, 0, true); | 122 newIntrinsicCameraMatrix = getDefaultNewCameraMatrix(intrinsicCameraMatrix, undistortedVideoSize, true);// for some reason, it's double type //getOptimalNewCameraMatrix(intrinsicCameraMatrix, params.distortionCoefficients, videoSize, 1, undistortedVideoSize);//, 0, true); |
| 119 initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, Mat::eye(3,3, CV_32FC1) /* 0 ?*/, newIntrinsicCameraMatrix, undistortedVideoSize, CV_32FC1, map1, map2); | 123 initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, Mat::eye(3,3, CV_32FC1) /* 0 ?*/, newIntrinsicCameraMatrix, undistortedVideoSize, CV_32FC1, map1, map2); |
| 120 | 124 |
| 121 cout << "Undistorted width=" << undistortedVideoSize.width << | 125 cout << "Undistorted width=" << undistortedVideoSize.width << |
| 122 ", height=" << undistortedVideoSize.height << endl; | 126 ", height=" << undistortedVideoSize.height << endl; |
| 123 } | 127 } |
| 124 | 128 |
| 125 Mat mask = imread(params.maskFilename, 0); | 129 Mat mask = imread(::getRelativeFilename(params.parentDirname, params.maskFilename), 0); |
| 126 if (mask.empty()) { | 130 if (mask.empty()) { |
| 127 cout << "Mask filename " << params.maskFilename << " could not be opened." << endl; | 131 cout << "Mask filename " << params.maskFilename << " could not be opened." << endl; |
| 128 mask = Mat::ones(videoSize, CV_8UC1); | 132 mask = Mat::ones(videoSize, CV_8UC1); |
| 129 } | 133 } |
| 130 | 134 |
| 131 std::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | 135 std::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
| 132 trajectoryDB->connect(params.databaseFilename.c_str()); | 136 trajectoryDB->connect(::getRelativeFilename(params.parentDirname, params.databaseFilename).c_str()); |
| 133 trajectoryDB->createTable("positions"); | 137 trajectoryDB->createTable("positions"); |
| 134 trajectoryDB->createTable("velocities"); | 138 trajectoryDB->createTable("velocities"); |
| 135 trajectoryDB->beginTransaction(); | 139 trajectoryDB->beginTransaction(); |
| 136 | 140 |
| 137 std::vector<Point2f> prevPts, currPts, newPts, undistortedPts; // all points but undistortedPts are in image space | 141 std::vector<Point2f> prevPts, currPts, newPts, undistortedPts; // all points but undistortedPts are in image space |
| 262 trajectoryDB->disconnect(); | 266 trajectoryDB->disconnect(); |
| 263 } | 267 } |
| 264 | 268 |
| 265 void groupFeatures(const KLTFeatureTrackingParameters& params) { | 269 void groupFeatures(const KLTFeatureTrackingParameters& params) { |
| 266 std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | 270 std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
| 267 bool success = trajectoryDB->connect(params.databaseFilename.c_str()); | 271 bool success = trajectoryDB->connect(::getRelativeFilename(params.parentDirname, params.databaseFilename).c_str()); |
| 268 trajectoryDB->createObjectTable("objects", "objects_features"); | 272 trajectoryDB->createObjectTable("objects", "objects_features"); |
| 269 unsigned int savedObjectId=0; | 273 unsigned int savedObjectId=0; |
| 270 | 274 |
| 271 trajectoryDB->createInstants("table"); | 275 trajectoryDB->createInstants("table"); |
| 272 //trajectoryDB->createIndex("positions","trajectory_id"); // does not seem to make loading features faster | 276 //trajectoryDB->createIndex("positions","trajectory_id"); // does not seem to make loading features faster |
| 337 trajectoryDB->disconnect(); | 341 trajectoryDB->disconnect(); |
| 338 } | 342 } |
| 339 | 343 |
| 340 void loadingTimes(const KLTFeatureTrackingParameters& params) { | 344 void loadingTimes(const KLTFeatureTrackingParameters& params) { |
| 341 std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | 345 std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
| 342 bool success = trajectoryDB->connect(params.databaseFilename.c_str()); | 346 bool success = trajectoryDB->connect(::getRelativeFilename(params.parentDirname, params.databaseFilename).c_str()); |
| 343 | 347 |
| 344 vector<std::shared_ptr<Trajectory<Point2f> > > trajectories; | 348 vector<std::shared_ptr<Trajectory<Point2f> > > trajectories; |
| 345 //cout << trajectories.size() << endl; | 349 //cout << trajectories.size() << endl; |
| 346 std::clock_t c_start = std::clock(); | 350 std::clock_t c_start = std::clock(); |
| 347 success = trajectoryDB->read(trajectories, "positions"); | 351 success = trajectoryDB->read(trajectories, "positions"); |
