Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 509:935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 23 May 2014 16:27:26 -0400 |
| parents | 081a9da6f85b |
| children | 018653d1db3c |
comparison
equal
deleted
inserted
replaced
| 508:6f7fa0093162 | 509:935430b1d408 |
|---|---|
| 21 #include <boost/filesystem.hpp> | 21 #include <boost/filesystem.hpp> |
| 22 | 22 |
| 23 #include <iostream> | 23 #include <iostream> |
| 24 #include <vector> | 24 #include <vector> |
| 25 #include <ctime> | 25 #include <ctime> |
| 26 #include <cmath> | |
| 26 | 27 |
| 27 using namespace std; | 28 using namespace std; |
| 28 using namespace cv; | 29 using namespace cv; |
| 29 namespace fs = boost::filesystem; | 30 namespace fs = boost::filesystem; |
| 30 | 31 |
| 75 Mat intrinsicCameraMatrix = ::loadMat(params.intrinsicCameraFilename, " "); | 76 Mat intrinsicCameraMatrix = ::loadMat(params.intrinsicCameraFilename, " "); |
| 76 //cout << intrinsicCameraMatrix << endl; | 77 //cout << intrinsicCameraMatrix << endl; |
| 77 | 78 |
| 78 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; | 79 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; |
| 79 Size window = Size(params.windowSize, params.windowSize); | 80 Size window = Size(params.windowSize, params.windowSize); |
| 81 | |
| 82 int interpolationMethod = -1; | |
| 83 if (params.interpolationMethod == 0) | |
| 84 interpolationMethod = INTER_NEAREST; | |
| 85 else if (params.interpolationMethod == 1) | |
| 86 interpolationMethod = INTER_LINEAR; | |
| 87 else if (params.interpolationMethod == 2) | |
| 88 interpolationMethod = INTER_CUBIC; | |
| 89 else if (params.interpolationMethod == 3) | |
| 90 interpolationMethod = INTER_LANCZOS4; | |
| 91 else | |
| 92 cout << "Unsupported option " << interpolationMethod << " for interpolation method" << endl; | |
| 80 | 93 |
| 81 // BruteForceMatcher<Hamming> descMatcher; | 94 // BruteForceMatcher<Hamming> descMatcher; |
| 82 // vector<DMatch> matches; | 95 // vector<DMatch> matches; |
| 83 | 96 |
| 84 boost::shared_ptr<InputFrameProviderIface> capture; | 97 boost::shared_ptr<InputFrameProviderIface> capture; |
| 93 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; | 106 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; |
| 94 exit(0); | 107 exit(0); |
| 95 } | 108 } |
| 96 | 109 |
| 97 Size videoSize = capture->getSize(); | 110 Size videoSize = capture->getSize(); |
| 98 //cout << capture->getSize() << " " << params.undistortedImageMultiplication*videoSize << endl; | |
| 99 unsigned int nFrames = capture->getNbFrames(); | 111 unsigned int nFrames = capture->getNbFrames(); |
| 100 cout << "Video " << params.videoFilename << | 112 cout << "Video " << params.videoFilename << |
| 101 ": width=" << videoSize.width << | 113 ": width=" << videoSize.width << |
| 102 ", height=" << videoSize.height << | 114 ", height=" << videoSize.height << |
| 103 ", nframes=" << nFrames << endl; | 115 ", nframes=" << nFrames << endl; |
| 104 | 116 |
| 105 Mat newIntrinsicCameraMatrix = intrinsicCameraMatrix.clone(); | 117 Mat newIntrinsicCameraMatrix = intrinsicCameraMatrix.clone(); |
| 106 Size newVideoSize = videoSize; | 118 Mat map1, map2; |
| 107 if (params.undistort) { | 119 if (params.undistort) { |
| 108 newVideoSize = Size(static_cast<int>(videoSize.width*params.undistortedImageMultiplication), static_cast<int>(videoSize.height*params.undistortedImageMultiplication)); | 120 videoSize = Size(static_cast<int>(round(videoSize.width*params.undistortedImageMultiplication)), static_cast<int>(round(videoSize.height*params.undistortedImageMultiplication))); |
| 109 newIntrinsicCameraMatrix.at<float>(0,2) = newVideoSize.width/2.; | 121 newIntrinsicCameraMatrix.at<float>(0,2) = videoSize.width/2.; |
| 110 newIntrinsicCameraMatrix.at<float>(1,2) = newVideoSize.height/2.; | 122 newIntrinsicCameraMatrix.at<float>(1,2) = videoSize.height/2.; |
| 123 initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, Mat::eye(3,3, CV_32FC1), newIntrinsicCameraMatrix, videoSize, CV_32FC1, map1, map2); | |
| 124 | |
| 125 cout << "Undistorted width=" << videoSize.width << | |
| 126 ", height=" << videoSize.height << endl; | |
| 111 } | 127 } |
| 112 | 128 |
| 113 Mat map1, map2; | |
| 114 Mat R = Mat::eye(3,3, CV_32FC1); | |
| 115 if (params.undistort) | |
| 116 initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, R, newIntrinsicCameraMatrix, newVideoSize, CV_32FC1, map1, map2); | |
| 117 | |
| 118 // todo mask in new size | |
| 119 Mat mask = imread(params.maskFilename, 0); | 129 Mat mask = imread(params.maskFilename, 0); |
| 120 if (mask.empty()) { | 130 if (mask.empty()) { |
| 121 cout << "Mask filename " << params.maskFilename << " could not be opened." << endl; | 131 cout << "Mask filename " << params.maskFilename << " could not be opened." << endl; |
| 122 mask = Mat::ones(newVideoSize, CV_8UC1); | 132 mask = Mat::ones(videoSize, CV_8UC1); |
| 123 } | 133 } |
| 124 | 134 |
| 125 boost::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); | 135 boost::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); |
| 126 //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); | 136 //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>(); |
| 127 trajectoryDB->connect(params.databaseFilename.c_str()); | 137 trajectoryDB->connect(params.databaseFilename.c_str()); |
| 148 | 158 |
| 149 capture->setFrameNumber(params.frame1); | 159 capture->setFrameNumber(params.frame1); |
| 150 for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) { | 160 for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) { |
| 151 bool success = capture->getNextFrame(frame); | 161 bool success = capture->getNextFrame(frame); |
| 152 if (params.undistort) { | 162 if (params.undistort) { |
| 153 remap(frame, undistortedFrame, map1, map2, INTER_LINEAR, BORDER_CONSTANT, 0.); | 163 remap(frame, undistortedFrame, map1, map2, interpolationMethod, BORDER_CONSTANT, 0.); |
| 154 frame = undistortedFrame; | 164 frame = undistortedFrame; |
| 155 } | 165 } |
| 156 //if (!success || frame.empty() || frame.size() != videoSize) | 166 //if (!success || frame.empty() || frame.size() != videoSize) |
| 157 //break; | 167 //break; |
| 158 | 168 |
