Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 134:a617d0808bbc
added test on feature length and display control
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 17 Aug 2011 23:21:26 -0400 |
| parents | 63dd4355b6d1 |
| children | 32d2722d4028 |
comparison
equal
deleted
inserted
replaced
| 133:63dd4355b6d1 | 134:a617d0808bbc |
|---|---|
| 58 | 58 |
| 59 VideoCapture capture; | 59 VideoCapture capture; |
| 60 Mat frame, currentFrameBW, previousFrameBW; | 60 Mat frame, currentFrameBW, previousFrameBW; |
| 61 | 61 |
| 62 KLTFeatureTrackingParameters params; | 62 KLTFeatureTrackingParameters params; |
| 63 params.display = false; | |
| 63 params.frame1 = 0; | 64 params.frame1 = 0; |
| 64 params.nFrames = 50; | 65 params.nFrames = -1; |
| 65 params.maxNFeatures = 1000; | 66 params.maxNFeatures = 1000; |
| 66 params.featureQuality = 0.1; | 67 params.featureQuality = 0.1; |
| 67 params.minFeatureDistanceKLT = 3; | 68 params.minFeatureDistanceKLT = 3; |
| 68 params.windowSize = 3; | 69 params.windowSize = 3; |
| 69 params.useHarrisDetector = false; | 70 params.useHarrisDetector = false; |
| 72 | 73 |
| 73 params.pyramidLevel = 3; | 74 params.pyramidLevel = 3; |
| 74 params.maxNumberTrackingIterations = 20; // 30 | 75 params.maxNumberTrackingIterations = 20; // 30 |
| 75 params.minTrackingError = 0.3; // 0.01 | 76 params.minTrackingError = 0.3; // 0.01 |
| 76 params.derivLambda = 0.5; | 77 params.derivLambda = 0.5; |
| 78 params.minFeatureTime = 20; | |
| 79 | |
| 77 Size window = Size(params.windowSize, params.windowSize); | 80 Size window = Size(params.windowSize, params.windowSize); |
| 78 | 81 |
| 79 BruteForceMatcher<Hamming> descMatcher; | 82 BruteForceMatcher<Hamming> descMatcher; |
| 80 vector<DMatch> matches; | 83 vector<DMatch> matches; |
| 81 Size videoSize; | 84 Size videoSize; |
| 126 // TODO structure de donnee paires pointeur trajectory, numero de keypoint | 129 // TODO structure de donnee paires pointeur trajectory, numero de keypoint |
| 127 int key = '?'; | 130 int key = '?'; |
| 128 unsigned int savedFeatureId=0; | 131 unsigned int savedFeatureId=0; |
| 129 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { | 132 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { |
| 130 capture >> frame; | 133 capture >> frame; |
| 131 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; | 134 cout << frameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; |
| 132 while (frame.empty()) | 135 while (frame.empty()) |
| 133 capture >> frame;//break; | 136 capture >> frame;//break; |
| 134 | 137 |
| 135 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); | 138 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); |
| 136 | 139 |
| 149 iter->feature->addPoint(frameNum, currPts[iter->pointNum]); | 152 iter->feature->addPoint(frameNum, currPts[iter->pointNum]); |
| 150 trackedPts.push_back(currPts[iter->pointNum]); | 153 trackedPts.push_back(currPts[iter->pointNum]); |
| 151 iter->pointNum = trackedPts.size()-1; | 154 iter->pointNum = trackedPts.size()-1; |
| 152 iter++; | 155 iter++; |
| 153 } else { | 156 } else { |
| 154 iter->feature->setId(savedFeatureId); | 157 if (iter->feature->length() >= params.minFeatureTime) { |
| 155 savedFeatureId++; | 158 iter->feature->setId(savedFeatureId); |
| 156 iter->feature->write(trajectoryDB); | 159 savedFeatureId++; |
| 160 iter->feature->write(trajectoryDB); | |
| 161 } | |
| 157 iter = featurePointMatches.erase(iter); | 162 iter = featurePointMatches.erase(iter); |
| 158 } | 163 } |
| 159 } | 164 } |
| 160 currPts = trackedPts; | 165 currPts = trackedPts; |
| 161 assert(currPts.size() == featurePointMatches.size()); | 166 assert(currPts.size() == featurePointMatches.size()); |
| 162 | 167 |
| 163 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) | 168 if (params.display) |
| 164 fp.feature->draw(frame, Colors::red()); | 169 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) |
| 170 fp.feature->draw(frame, Colors::red()); | |
| 165 //drawOpticalFlow(prevPts, currPts, status, frame); | 171 //drawOpticalFlow(prevPts, currPts, status, frame); |
| 166 | 172 |
| 167 // cout << matches.size() << " matches" << endl; | 173 // cout << matches.size() << " matches" << endl; |
| 168 // descMatcher.match(currDesc, prevDesc, matches); | 174 // descMatcher.match(currDesc, prevDesc, matches); |
| 169 // cout << matches.size() << " matches" << endl; | 175 // cout << matches.size() << " matches" << endl; |
| 170 //drawMatchesRelative(prevKpts, currKpts, matches, frame); | 176 //drawMatchesRelative(prevKpts, currKpts, matches, frame); |
| 171 } | 177 } |
| 186 // currPts.insert(currPts.end(), newPts.begin(), newPts.end()); | 192 // currPts.insert(currPts.end(), newPts.begin(), newPts.end()); |
| 187 //::keyPoints2Points(currKpts, currPts, false); | 193 //::keyPoints2Points(currKpts, currPts, false); |
| 188 | 194 |
| 189 //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location | 195 //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location |
| 190 | 196 |
| 191 imshow("frame", frame); | 197 if (params.display) { |
| 192 imshow("mask", featureMask*256); | 198 imshow("frame", frame); |
| 199 imshow("mask", featureMask*256); | |
| 200 key = waitKey(2); | |
| 201 } | |
| 193 previousFrameBW = currentFrameBW.clone(); | 202 previousFrameBW = currentFrameBW.clone(); |
| 194 prevPts = currPts; | 203 prevPts = currPts; |
| 195 //prevKpts = currKpts; | 204 //prevKpts = currKpts; |
| 196 //currDesc.copyTo(prevDesc); | 205 //currDesc.copyTo(prevDesc); |
| 197 key = waitKey(2); | |
| 198 } | 206 } |
| 199 | 207 |
| 200 trajectoryDB.disconnect(); | 208 trajectoryDB.disconnect(); |
| 201 return 0; | 209 return 0; |
| 202 } | 210 } |
