Mercurial Hosting > traffic-intelligence
annotate c/feature-based-tracking.cpp @ 132:45c64e68053c
added drawing function for features
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 17 Aug 2011 19:03:25 -0400 |
parents | 2a6e7a9a5c53 |
children | 63dd4355b6d1 |
rev | line source |
---|---|
130
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
1 #include "Feature.hpp" |
124
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
2 #include "Parameters.hpp" |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
3 #include "cvutils.hpp" |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
4 #include "utils.hpp" |
117
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
5 |
124
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
6 #include "src/Trajectory.h" |
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
7 |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
8 #include "opencv2/core/core.hpp" |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
9 #include "opencv2/highgui/highgui.hpp" |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
10 //#include "opencv2/imgproc/imgproc.hpp" |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
11 #include "opencv2/features2d/features2d.hpp" |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
12 |
125
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
13 #include <boost/shared_ptr.hpp> |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
14 #include <boost/foreach.hpp> |
125
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
15 |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
16 #include <iostream> |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
17 //#include <list> |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
18 #include <vector> |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
19 |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
20 using namespace std; |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
21 using namespace cv; |
117
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
22 |
123
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
23 void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query, std::vector<cv::DMatch>& matches, Mat& img) { |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
24 for (int i = 0; i < (int)matches.size(); i++) |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
25 { |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
26 Point2f pt_new = query[matches[i].queryIdx].pt; |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
27 Point2f pt_old = train[matches[i].trainIdx].pt; |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
28 Point2f dist = pt_new - pt_old; |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
29 if (norm(dist) < 20) { |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
30 line(img, pt_new, pt_old, Scalar(125, 255, 125), 1); |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
31 circle(img, pt_old, 2, Scalar(255, 0, 125), 1); |
123
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
32 } |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
33 } |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
34 } |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
35 |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
36 void drawOpticalFlow(const vector<Point2f>& prevPts, const vector<Point2f>& currPts, const vector<uchar> status, Mat& img) { |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
37 for (unsigned int i=0; i<status.size(); i++) { |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
38 if (status[i]) { |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
39 line(img, prevPts[i], currPts[i], Scalar(125, 255, 125), 1); |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
40 circle(img, prevPts[i], 2, Scalar(255, 0, 125), 1); |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
41 } |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
42 } |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
43 } |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
44 |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
45 struct FeaturePointMatch { |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
46 FeatureTrajectoryPtr feature; |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
47 int pointNum; |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
48 |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
49 FeaturePointMatch(FeatureTrajectoryPtr _feature, const int& _pointNum): |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
50 feature(_feature), pointNum(_pointNum) {} |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
51 }; |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
52 |
117
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
53 int main(int argc, char *argv[]) { |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
54 // BriefDescriptorExtractor brief(32); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
55 // const int DESIRED_FTRS = 500; |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
56 // GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); |
125
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
57 |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
58 VideoCapture capture; |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
59 Mat frame, currentFrameBW, previousFrameBW; |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
60 |
124
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
61 KLTFeatureTrackingParameters params; |
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
62 params.frame1 = 0; |
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
63 params.nFrames = -1; |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
64 params.maxNFeatures = 1000; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
65 params.featureQuality = 0.1; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
66 params.minFeatureDistanceKLT = 3; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
67 params.windowSize = 3; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
68 params.useHarrisDetector = false; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
69 params.k = 0.4; |
130
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
70 //GoodFeaturesToTrackDetector detector(params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, params.windowSize, params.useHarrisDetector, params.k); |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
71 |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
72 params.pyramidLevel = 3; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
73 params.maxNumberTrackingIterations = 20; // 30 |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
74 params.minTrackingError = 0.3; // 0.01 |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
75 params.derivLambda = 0.5; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
76 Size window = Size(params.windowSize, params.windowSize); |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
77 |
125
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
78 BruteForceMatcher<Hamming> descMatcher; |
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
79 vector<DMatch> matches; |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
80 Size videoSize; |
125
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
81 |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
82 if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
83 capture.open(argc == 2 ? argv[1][0] - '0' : 0); |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
84 else if( argc >= 2 ) |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
85 { |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
86 capture.open(argv[1]); |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
87 if( capture.isOpened() ) |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
88 videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
89 cout << "Video " << argv[1] << |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
90 ": width=" << videoSize.width << |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
91 ", height=" << videoSize.height << |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
92 ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl; |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
93 if( argc > 2 && isdigit(argv[2][0]) ) // could be used to reach first frame, dumping library messages to log file (2> /tmp/log.txt) |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
94 { |
124
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
95 sscanf(argv[2], "%d", ¶ms.frame1); |
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
96 cout << "seeking to frame #" << params.frame1 << endl; |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
97 //cap.set(CV_CAP_PROP_POS_FRAMES, pos); |
124
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
98 for (int i=0; i<params.frame1; i++) |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
99 capture >> frame; |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
100 } |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
101 } |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
102 |
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
103 // capture.open(atoi(argv[1])); |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
104 if (!capture.isOpened()) |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
105 { |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
106 //help(argv); |
122
654f1c748644
work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
121
diff
changeset
|
107 cout << "capture device " << argv[1] << " failed to open!" << endl; |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
108 return 1; |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
109 } |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
110 |
124
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
111 vector<KeyPoint> prevKpts, currKpts; |
130
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
112 vector<Point2f> prevPts, currPts, newPts; |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
113 vector<uchar> status; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
114 vector<float> errors; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
115 Mat prevDesc, currDesc; |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
116 |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
117 vector<FeatureTrajectoryPtr> features; |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
118 vector<FeaturePointMatch> featurePointMatches; |
130
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
119 |
124
1e68e18b1aa5
renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
123
diff
changeset
|
120 // TODO structure de donnee paires pointeur trajectory, numero de keypoint |
125
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
121 int key = '?'; |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
122 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
123 capture >> frame; |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
124 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; |
123
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
125 while (frame.empty()) |
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
126 capture >> frame;//break; |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
127 |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
128 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); |
130
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
129 |
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
130 // "normal" feature detectors: detect features here |
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
131 // detector.detect(currentFrameBW, currKpts); // see video_homography c++ sample |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
132 |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
133 if (!prevPts.empty()) { |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
134 //::keyPoints2Points(prevKpts, prevPts); |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
135 currPts.clear(); |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
136 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 |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
137 |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
138 vector<Point2f> trackedPts; |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
139 vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin(); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
140 while (iter != featurePointMatches.end()) { |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
141 if (status[iter->pointNum]) { |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
142 iter->feature->addPoint(frameNum, currPts[iter->pointNum]); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
143 trackedPts.push_back(currPts[iter->pointNum]); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
144 iter->pointNum = trackedPts.size()-1; |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
145 iter++; |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
146 } else { |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
147 // save feature |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
148 iter = featurePointMatches.erase(iter); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
149 } |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
150 } |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
151 currPts = trackedPts; |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
152 assert(currPts.size() == featurePointMatches.size()); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
153 |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
154 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches) |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
155 fp.feature->draw(frame, Colors::red()); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
156 //drawOpticalFlow(prevPts, currPts, status, frame); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
157 |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
158 // cout << matches.size() << " matches" << endl; |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
159 // descMatcher.match(currDesc, prevDesc, matches); |
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
160 // cout << matches.size() << " matches" << endl; |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
161 //drawMatchesRelative(prevKpts, currKpts, matches, frame); |
125
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
162 } |
28907fde9855
work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
124
diff
changeset
|
163 |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
164 // adding new features, using mask around existing feature positions |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
165 Mat featureMask = Mat::ones(videoSize, CV_8UC1); |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
166 for (unsigned int n=0;n<currPts.size(); n++) |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
167 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++) |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
168 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
169 featureMask.at<uchar>(i,j)=0; |
130
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
170 goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.windowSize, params.useHarrisDetector, params.k); |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
171 BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) { |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
172 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p)); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
173 //features.push_back(f); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
174 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size())); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
175 currPts.push_back(p); |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
176 } |
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
177 // currPts.insert(currPts.end(), newPts.begin(), newPts.end()); |
130
2a6e7a9a5c53
changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
128
diff
changeset
|
178 //::keyPoints2Points(currKpts, currPts, false); |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
179 |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
180 //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
181 |
123
df3bdd8e50ba
displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
122
diff
changeset
|
182 imshow("frame", frame); |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
183 imshow("mask", featureMask*256); |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
125
diff
changeset
|
184 previousFrameBW = currentFrameBW.clone(); |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
185 prevPts = currPts; |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
186 //prevKpts = currKpts; |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
187 //currDesc.copyTo(prevDesc); |
132
45c64e68053c
added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
130
diff
changeset
|
188 key = waitKey(0); |
121
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
189 } |
c4d4b5b93add
copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
119
diff
changeset
|
190 |
117
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
191 return 0; |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
192 } |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
193 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
194 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
195 /* ------------------ DOCUMENTATION ------------------ */ |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
196 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
197 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
198 /*! \mainpage |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
199 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
200 This project is a collection of software tools for transportation called Traffic Intelligence. Other documents are: |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
201 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
202 - \ref feature_based_tracking |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
203 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
204 The code is partially self-described using the doxygen tool and comment formatting. The documentation can be extracted using doxygen, typing \c doxygen in the main directory (or <tt>make doc</tt> on a system with the Makefile tool installed). |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
205 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
206 */ |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
207 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
208 /*! \page feature_based_tracking Feature-based Tracking: User Manual |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
209 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
210 This document describes a software tool for object tracking in video data, developed for road traffic monitoring and safety diagnosis. It is part of a larger collection of software tools for transportation called Traffic Intelligence. |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
211 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
212 The tool relies on feature-based tracking, a robust object tracking methods, particularly suited for the extraction of traffic data such as trajectories and speeds. The best description of this method is given in <a href="http://nicolas.saunier.confins.net/data/saunier06crv.html">this paper</a>. The program has a command line interface and this document will shortly explain how to use the tool. Keep in mind this is a work in progress and major changes are continuously being made. |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
213 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
214 \section License |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
215 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
216 The code is licensed under the MIT open source license (http://www.opensource.org/licenses/mit-license). |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
217 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
218 If you make use of this piece of software, please cite one of my paper, e.g. N. Saunier, T. Sayed and K. Ismail. Large Scale Automated Analysis of Vehicle Interactions and Collisions. Transportation Research Record: Journal of the Transportation Research Board, 2147:42-50, 2010. I would be very happy in any case to know about any use of the code, and to discuss any opportunity for collaboration. |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
219 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
220 Contact me at nicolas.saunier@polymtl.ca and learn more about my work at http://nicolas.saunier.confins.net. |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
221 |
fea680fb03ee
created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
222 */ |