Mercurial Hosting > traffic-intelligence
changeset 132:45c64e68053c
added drawing function for features
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 17 Aug 2011 19:03:25 -0400 |
parents | 3a11dba30655 |
children | 63dd4355b6d1 |
files | c/Feature.cpp c/feature-based-tracking.cpp include/Feature.hpp |
diffstat | 3 files changed, 70 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/c/Feature.cpp Wed Aug 17 19:03:11 2011 -0400 +++ b/c/Feature.cpp Wed Aug 17 19:03:25 2011 -0400 @@ -1,15 +1,32 @@ #include "Feature.hpp" #include "opencv2/core/core.hpp" +#include "opencv2/highgui/highgui.hpp" using namespace std; using namespace cv; +FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p) { + addPoint(frameNum, p); +} + void FeatureTrajectory::addPoint(const int& frameNum, const Point2f& p) { positions.add(frameNum, p); computeMotionData(frameNum); } +#ifdef USE_OPENCV +/// \todo add option for anti-aliased drawing, thickness +void FeatureTrajectory::draw(Mat& img, const Scalar& color) const { + Point2f p1 = positions[0]; + for (unsigned int i=1; i<positions.size(); i++) { + Point2f p2 = positions[i]; + line(img, p1, p2, color, 1); + p1 = p2; + } +} +#endif + // protected void FeatureTrajectory::computeMotionData(const int& frameNum) {
--- a/c/feature-based-tracking.cpp Wed Aug 17 19:03:11 2011 -0400 +++ b/c/feature-based-tracking.cpp Wed Aug 17 19:03:25 2011 -0400 @@ -11,6 +11,7 @@ #include "opencv2/features2d/features2d.hpp" #include <boost/shared_ptr.hpp> +#include <boost/foreach.hpp> #include <iostream> //#include <list> @@ -18,7 +19,6 @@ using namespace std; using namespace cv; -using namespace boost; void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) { for (int i = 0; i < (int)matches.size(); i++) @@ -42,11 +42,18 @@ } } +struct FeaturePointMatch { + FeatureTrajectoryPtr feature; + int pointNum; + + FeaturePointMatch(FeatureTrajectoryPtr _feature, const int& _pointNum): + feature(_feature), pointNum(_pointNum) {} +}; + int main(int argc, char *argv[]) { - BriefDescriptorExtractor brief(32); - const int DESIRED_FTRS = 500; - //shared_ptr<FeatureDetector> detector = shared_ptr<FeatureDetector>(new GridAdaptedFeatureDetector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4)); - //GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); + // BriefDescriptorExtractor brief(32); + // const int DESIRED_FTRS = 500; + // GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); VideoCapture capture; Mat frame, currentFrameBW, previousFrameBW; @@ -107,7 +114,8 @@ vector<float> errors; Mat prevDesc, currDesc; - vector<FeatureTrajectory> features; + vector<FeatureTrajectoryPtr> features; + vector<FeaturePointMatch> featurePointMatches; // TODO structure de donnee paires pointeur trajectory, numero de keypoint int key = '?'; @@ -127,14 +135,26 @@ currPts.clear(); calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(3 /*static_cast<int>(TermCriteria::COUNT)+static_cast<int>(TermCriteria::EPS)*/, params.maxNumberTrackingIterations, params.minTrackingError), params.derivLambda, 0); // OPTFLOW_USE_INITIAL_FLOW - drawOpticalFlow(prevPts, currPts, status, frame); - vector<Point2f> trackedPts; - for (unsigned int i=0; i<status.size(); i++) - if (status[i]) - trackedPts.push_back(currPts[i]); + vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin(); + while (iter != featurePointMatches.end()) { + if (status[iter->pointNum]) { + iter->feature->addPoint(frameNum, currPts[iter->pointNum]); + trackedPts.push_back(currPts[iter->pointNum]); + iter->pointNum = trackedPts.size()-1; + iter++; + } else { + // save feature + iter = featurePointMatches.erase(iter); + } + } currPts = trackedPts; - + assert(currPts.size() == featurePointMatches.size()); + + BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) + fp.feature->draw(frame, Colors::red()); + //drawOpticalFlow(prevPts, currPts, status, frame); + // cout << matches.size() << " matches" << endl; // descMatcher.match(currDesc, prevDesc, matches); // cout << matches.size() << " matches" << endl; @@ -148,7 +168,13 @@ for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) featureMask.at<uchar>(i,j)=0; goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.windowSize, params.useHarrisDetector, params.k); - currPts.insert(currPts.end(), newPts.begin(), newPts.end()); + BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) { + FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p)); + //features.push_back(f); + featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); + currPts.push_back(p); + } + // currPts.insert(currPts.end(), newPts.begin(), newPts.end()); //::keyPoints2Points(currKpts, currPts, false); //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location @@ -159,7 +185,7 @@ prevPts = currPts; //prevKpts = currKpts; //currDesc.copyTo(prevDesc); - key = waitKey(2); + key = waitKey(0); } return 0;
--- a/include/Feature.hpp Wed Aug 17 19:03:11 2011 -0400 +++ b/include/Feature.hpp Wed Aug 17 19:03:25 2011 -0400 @@ -1,14 +1,23 @@ #ifndef FEATURE_HPP #define FEATURE_HPP -#include "opencv2/core/core.hpp" - #include "src/Trajectory.h" +#include <boost/shared_ptr.hpp> + +/** Class for feature data + positions, velocities and other statistics to evaluate their quality + before saving. */ class FeatureTrajectory { +public: + FeatureTrajectory(const int& frameNum, const cv::Point2f& p); void addPoint(const int& frameNum, const cv::Point2f& p); +#ifdef USE_OPENCV + void draw(cv::Mat& img, const cv::Scalar& color) const; +#endif + protected: Trajectory<cv::Point2f> positions; Trajectory<cv::Point2f> velocities; @@ -19,4 +28,6 @@ }; +typedef boost::shared_ptr<FeatureTrajectory> FeatureTrajectoryPtr; + #endif