annotate c/feature-based-tracking.cpp @ 191:0e60a306d324

added basic code to identify features and save them (crash)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 08 Dec 2011 18:32:35 -0500
parents 1116f0a1ff31
children a728fce85881
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
0f790de9437e renamed Feature to Motion files and added code to test blob db
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 135
diff changeset
1 #include "Motion.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"
133
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
7 #include "src/TrajectoryDBAccessList.h"
136
0f790de9437e renamed Feature to Motion files and added code to test blob db
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 135
diff changeset
8 #include "src/TrajectoryDBAccessBlob.h"
124
1e68e18b1aa5 renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 123
diff changeset
9
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
10 #include "opencv2/core/core.hpp"
165
50964af05a80 solved issue with header inclusion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 164
diff changeset
11 #include "opencv2/imgproc/imgproc.hpp"
50964af05a80 solved issue with header inclusion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 164
diff changeset
12 #include "opencv2/video/tracking.hpp"
50964af05a80 solved issue with header inclusion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 164
diff changeset
13 #include "opencv2/features2d/features2d.hpp"
121
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
14 #include "opencv2/highgui/highgui.hpp"
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
15
125
28907fde9855 work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 124
diff changeset
16 #include <boost/shared_ptr.hpp>
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
17 #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
18
121
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
19 #include <iostream>
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
20 #include <vector>
174
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
21 #include <ctime>
121
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
22
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
23 using namespace std;
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
24 using namespace cv;
117
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
25
123
df3bdd8e50ba displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 122
diff changeset
26 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
27 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
28 {
df3bdd8e50ba displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 122
diff changeset
29 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
30 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
31 Point2f dist = pt_new - pt_old;
df3bdd8e50ba displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 122
diff changeset
32 if (norm(dist) < 20) {
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
33 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
34 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
35 }
df3bdd8e50ba displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 122
diff changeset
36 }
df3bdd8e50ba displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 122
diff changeset
37 }
df3bdd8e50ba displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 122
diff changeset
38
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
39 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
40 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
41 if (status[i]) {
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
42 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
43 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
44 }
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
45 }
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
46 }
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
47
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
48 struct FeaturePointMatch {
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
49 FeatureTrajectoryPtr feature;
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
50 int 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 FeaturePointMatch(FeatureTrajectoryPtr _feature, const int& _pointNum):
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
53 feature(_feature), pointNum(_pointNum) {}
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
54 };
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
55
142
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 141
diff changeset
56 inline void saveFeatures(vector<FeatureTrajectoryPtr>& features, TrajectoryDBAccess<Point2f>& db, const string& positionsTableName, const string& velocitiesTableName, const unsigned int& minNFeatures = 0) {
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
57 /// \todo smoothing
140
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
58 if (features.size() >= minNFeatures) {
142
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 141
diff changeset
59 BOOST_FOREACH(FeatureTrajectoryPtr f, features) f->write(db, positionsTableName, velocitiesTableName);
140
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
60 features.clear();
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
61 }
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
62 }
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
63
164
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
64 void trackFeatures(const KLTFeatureTrackingParameters& params) {
174
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
65 // BriefDescriptorExtractor brief(32);
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
66 // const int DESIRED_FTRS = 500;
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
67 // GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4);
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
68
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
69 Mat homography = ::loadMat(params.homographyFilename, " ");
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
70 Mat invHomography;
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
71 if (params.display && !homography.empty())
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
72 invHomography = homography.inv();
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
73
137
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
74 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement;
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
75 Size window = Size(params.windowSize, params.windowSize);
122
654f1c748644 work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 121
diff changeset
76
125
28907fde9855 work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 124
diff changeset
77 BruteForceMatcher<Hamming> descMatcher;
28907fde9855 work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 124
diff changeset
78 vector<DMatch> matches;
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
79 Size videoSize;
125
28907fde9855 work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 124
diff changeset
80
137
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
81 // if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
82 // capture.open(argc == 2 ? argv[1][0] - '0' : 0);
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
83 // else if( argc >= 2 )
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
84 // {
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
85 // capture.open(argv[1]);
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
86 // if( capture.isOpened() )
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
87 // videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
88 // cout << "Video " << argv[1] <<
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
89 // ": width=" << videoSize.width <<
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
90 // ", height=" << videoSize.height <<
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
91 // ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl;
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
92 // if( argc > 2 && isdigit(argv[2][0]) ) // could be used to reach first frame, dumping library messages to log file (2> /tmp/log.txt)
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
93 // {
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
94 // sscanf(argv[2], "%d", &params.frame1);
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
95 // cout << "seeking to frame #" << params.frame1 << endl;
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
96 // //cap.set(CV_CAP_PROP_POS_FRAMES, pos);
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
97 // for (int i=0; i<params.frame1; i++)
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
98 // capture >> frame;
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
99 // }
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
100 // }
122
654f1c748644 work on displaying matched features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 121
diff changeset
101
164
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
102 VideoCapture capture;
137
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
103 capture.open(params.videoFilename);
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
104 if(capture.isOpened()) {
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
105 videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
145
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
106 cout << "Video " << params.videoFilename <<
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
107 ": width=" << videoSize.width <<
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
108 ", height=" << videoSize.height <<
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
109 ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl;
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
110 } else {
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
111 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl;
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
112 exit(0);
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
113 }
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
114 // if (!capture.isOpened())
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
115 // {
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
116 // //help(argv);
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
117 // cout << "capture device " << argv[1] << " failed to open!" << endl;
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
118 // return 1;
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
119 // }
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
120
146
7150427c665e added loading of mask
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 145
diff changeset
121 Mat mask = imread(params.maskFilename, 0);
7150427c665e added loading of mask
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 145
diff changeset
122 if (mask.empty())
7150427c665e added loading of mask
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 145
diff changeset
123 mask = Mat::ones(videoSize, CV_8UC1);
7150427c665e added loading of mask
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 145
diff changeset
124
140
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
125 boost::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>());
136
0f790de9437e renamed Feature to Motion files and added code to test blob db
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 135
diff changeset
126 //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>();
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
127 trajectoryDB->connect(params.databaseFilename.c_str());
142
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 141
diff changeset
128 trajectoryDB->createTable("positions");
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 141
diff changeset
129 trajectoryDB->createTable("velocities");
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 141
diff changeset
130 trajectoryDB->beginTransaction();
133
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
131
124
1e68e18b1aa5 renaming and working on klt
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 123
diff changeset
132 vector<KeyPoint> prevKpts, currKpts;
130
2a6e7a9a5c53 changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 128
diff changeset
133 vector<Point2f> prevPts, currPts, newPts;
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
134 vector<uchar> status;
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
135 vector<float> errors;
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
136 Mat prevDesc, currDesc;
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
137
140
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
138 vector<FeatureTrajectoryPtr> lostFeatures;
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
139 vector<FeaturePointMatch> featurePointMatches;
163
cde87a07eb58 added graph structures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 154
diff changeset
140
133
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
141 int key = '?';
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
142 unsigned int savedFeatureId=0;
164
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
143 Mat frame, currentFrameBW, previousFrameBW;
141
6f10a227486c modifications to get nframes option working on the command line
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 140
diff changeset
144 for (int frameNum = params.frame1; ((frameNum-params.frame1 < 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
145 capture >> frame;
134
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
146 cout << frameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl;
145
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
147 int emptyFrameNum = 0;
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
148 while (frame.empty()) {
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
149 cerr << "empty frame " << emptyFrameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES)<< endl;
123
df3bdd8e50ba displays tracking from video and webcam
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 122
diff changeset
150 capture >> frame;//break;
145
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
151 emptyFrameNum++;
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
152 if (emptyFrameNum>=3000)
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
153 exit(0);
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
154 }
121
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
155
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
156 cvtColor(frame, currentFrameBW, CV_RGB2GRAY);
130
2a6e7a9a5c53 changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 128
diff changeset
157
2a6e7a9a5c53 changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 128
diff changeset
158 // "normal" feature detectors: detect features here
2a6e7a9a5c53 changed to goodFeaturesToTrack instead of generic detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 128
diff changeset
159 // 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
160
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
161 if (!prevPts.empty()) {
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
162 //::keyPoints2Points(prevKpts, prevPts);
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
163 currPts.clear();
137
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
164 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), 0.5 /* unused */, 0); // OPTFLOW_USE_INITIAL_FLOW
154
668710d4c773 updated computeTranslation with cv2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 147
diff changeset
165 /// \todo try calcOpticalFlowFarneback
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
166
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
167 vector<Point2f> trackedPts;
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
168 vector<FeaturePointMatch>::iterator iter = featurePointMatches.begin();
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
169 while (iter != featurePointMatches.end()) {
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 134
diff changeset
170 bool deleteFeature = false;
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 134
diff changeset
171
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
172 if (status[iter->pointNum]) {
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
173 iter->feature->addPoint(frameNum, currPts[iter->pointNum], homography);
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 134
diff changeset
174
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 175
diff changeset
175 deleteFeature |= iter->feature->isDisplacementSmall(params.nDisplacements, minTotalFeatureDisplacement)
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 175
diff changeset
176 || !iter->feature->isMotionSmooth(params.accelerationBound, params.deviationBound);
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
177 if (deleteFeature)
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
178 iter->feature->shorten();
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
179 } else
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
180 deleteFeature = true;
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 134
diff changeset
181
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 134
diff changeset
182 if (deleteFeature) {
134
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
183 if (iter->feature->length() >= params.minFeatureTime) {
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
184 iter->feature->setId(savedFeatureId);
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
185 savedFeatureId++;
140
8de5e8256224 added function to save vectors of features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
186 lostFeatures.push_back(iter->feature);
134
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
187 }
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
188 iter = featurePointMatches.erase(iter);
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
189 } else {
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
190 trackedPts.push_back(currPts[iter->pointNum]);
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
191 iter->pointNum = trackedPts.size()-1;
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 134
diff changeset
192 iter++;
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
193 }
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
194 }
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
195 currPts = trackedPts;
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
196 assert(currPts.size() == featurePointMatches.size());
142
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 141
diff changeset
197 saveFeatures(lostFeatures, *trajectoryDB, "positions", "velocities");
134
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
198
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
199 if (params.display) {
134
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
200 BOOST_FOREACH(FeaturePointMatch fp, featurePointMatches)
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
201 fp.feature->draw(frame, invHomography, Colors::red());
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
202 }
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
203 //drawOpticalFlow(prevPts, currPts, status, frame);
134
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
204
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
205 // cout << matches.size() << " matches" << endl;
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
206 // descMatcher.match(currDesc, prevDesc, matches);
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
207 // cout << matches.size() << " matches" << endl;
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
208 //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
209 }
28907fde9855 work on klt tracker (problem on computer at poly)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 124
diff changeset
210
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
211 // adding new features, using mask around existing feature positions
146
7150427c665e added loading of mask
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 145
diff changeset
212 Mat featureMask = mask.clone();
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
213 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
214 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
215 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
216 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
217 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
218 BOOST_FOREACH(Point2f p, newPts) { //for (unsigned int i=0; i<newPts.size(); i++) {
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 146
diff changeset
219 FeatureTrajectoryPtr f = FeatureTrajectoryPtr(new FeatureTrajectory(frameNum, p, homography));
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
220 featurePointMatches.push_back(FeaturePointMatch(f, currPts.size()));
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
221 currPts.push_back(p);
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
222 }
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 130
diff changeset
223 // 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
224 //::keyPoints2Points(currKpts, currPts, false);
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
225
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
226 //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
227
134
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
228 if (params.display) {
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
229 imshow("frame", frame);
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
230 imshow("mask", featureMask*256);
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
231 key = waitKey(2);
a617d0808bbc added test on feature length and display control
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
232 }
127
d19d6e63dd77 simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 125
diff changeset
233 previousFrameBW = currentFrameBW.clone();
128
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
234 prevPts = currPts;
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
235 //prevKpts = currKpts;
536510f60854 new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 127
diff changeset
236 //currDesc.copyTo(prevDesc);
121
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
237 }
c4d4b5b93add copied the video_homography opencv sample
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 119
diff changeset
238
142
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 141
diff changeset
239 trajectoryDB->endTransaction();
136
0f790de9437e renamed Feature to Motion files and added code to test blob db
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 135
diff changeset
240 trajectoryDB->disconnect();
164
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
241 }
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
242
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
243 void groupFeatures(const KLTFeatureTrackingParameters& params) {
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
244 cout << "group" << endl;
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
245
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
246 boost::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>());
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
247 //TODO write generic methods for blob and list versions TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>();
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
248 bool success = trajectoryDB->connect(params.databaseFilename.c_str());
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
249 // vector<boost::shared_ptr<Trajectory<Point2f> > > trajectories;
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
250 // cout << trajectories.size() << endl;
174
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
251 // std::clock_t c_start = std::clock();
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
252 // success = trajectoryDB->read(trajectories, "positions"); // TODO load velocities as well in a FeatureTrajectory object // attention, velocities lack the first instant
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
253 // std::clock_t c_end = std::clock();
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
254 // cout << "Loaded " << trajectories.size() << " trajectories in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl;
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
255
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
256 // boost::shared_ptr<Trajectory<cv::Point2f> > trajectory;
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
257 // c_start = std::clock();
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
258 // for (unsigned int i = 0; i<trajectories.size(); ++i) {
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
259 // success = trajectoryDB->read(trajectory, i, "positions");
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
260 // }
174
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
261 // c_end = std::clock();
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
262 // cout << "Loaded " << trajectories.size() << " trajectories one by one in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl;
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
263
175
a234a5e818f3 using single view for frame_numbers and getting max trajectory length
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 174
diff changeset
264 trajectoryDB->createViewInstants();
a234a5e818f3 using single view for frame_numbers and getting max trajectory length
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 174
diff changeset
265 int maxTrajectoryLength;
a234a5e818f3 using single view for frame_numbers and getting max trajectory length
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 174
diff changeset
266 trajectoryDB->maxTrajectoryLength(maxTrajectoryLength);
a234a5e818f3 using single view for frame_numbers and getting max trajectory length
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 174
diff changeset
267 cout << "max trajectory length " << maxTrajectoryLength << endl;
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
268 maxTrajectoryLength = 20; // for tests
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
269
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
270 FeatureGraph featureGraph(params.mmConnectionDistance, params.mmSegmentationDistance, params.minFeatureTime, params.minNFeaturesPerGroup);
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
271
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
272 // main loop
186
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
273 // TODO version that can be interrupted?
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
274 for (int frameNum = params.frame1; ((frameNum-params.frame1 < params.nFrames) || (params.nFrames < 0)); frameNum++) {
174
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
275 vector<int> trajectoryIds;
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
276 success = trajectoryDB->trajectoryIdEndingAt(trajectoryIds, frameNum); // ending
174
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
277 cout << "frame " << frameNum << " " << success << endl;
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
278 cout << trajectoryIds.size() << " trajectories " << endl;
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
279 BOOST_FOREACH(int trajectoryId, trajectoryIds) {
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
280 //cout << trajectoryId << " " << endl;
177
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
281 // boost::shared_ptr<Trajectory<cv::Point2f> > trajectory;
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
282 // success = trajectoryDB->read(trajectory, trajectoryId, "positions"); // velocities
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
283 FeatureTrajectoryPtr ft = FeatureTrajectoryPtr(new FeatureTrajectory(trajectoryId, *trajectoryDB, "positions", "velocities"));
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
284 // stringstream ss;ss << *ft; cout << ss.str() << endl;
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
285 // cout << ft->getFirstInstant() << " " << ft->getLastInstant() << endl;
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
286 featureGraph.addFeature(ft);
174
ec9734015d53 tested loading trajectory by id numbers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 172
diff changeset
287 }
172
e508bb0cbb64 modified comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 169
diff changeset
288
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
289 // check for connected components that are old enough (no chance to match with trajectories to be added later
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
290 // we could check only when some features are getting old enough?
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
291 if (frameNum%10 == 0) {
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
292 vector<vector<FeatureGraph::vertex_descriptor> > objects = featureGraph.connectedComponents(frameNum-maxTrajectoryLength+params.minFeatureTime);
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
293 cout << objects.size() << " objects" << endl;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
294
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
295 if (!objects.empty()) {
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
296 vector<vector<unsigned int> > featureGroups = featureGraph.getFeatureGroups(objects);
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
297 }
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
298 }
172
e508bb0cbb64 modified comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 169
diff changeset
299
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
300 cout << featureGraph.informationString() << endl;
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
301 }
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
302
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
303 trajectoryDB->endTransaction();
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
304 trajectoryDB->disconnect();
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
305 }
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
306
164
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
307 int main(int argc, char *argv[]) {
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
308 KLTFeatureTrackingParameters params(argc, argv);
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
309 cout << params.parameterDescription << endl;
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
310
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
311 if (params.trackFeatures)
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
312 trackFeatures(params);
169
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
313 else if (params.groupFeatures)
5f705809d37a created groupFeatures function
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 168
diff changeset
314 groupFeatures(params);
164
76610dcf3b8d added test code to read trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
315
117
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
316 return 0;
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
317 }
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
318
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
319 /* ------------------ DOCUMENTATION ------------------ */
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
320
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
321
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
322 /*! \mainpage
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
323
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
324 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
325
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
326 - \ref feature_based_tracking
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
327
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
328 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
329
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
330 */
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
331
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
332 /*! \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
333
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
334 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
335
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
336 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
337
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
338 \section License
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
339
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
340 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
341
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
342 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
343
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
344 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
345
fea680fb03ee created main feature based tracking file and minimum doxygen documentation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
346 */