#include <algorithm>
#include <string>
#include <vector>
#include "libmv/base/scoped_ptr.h"
#include "libmv/correspondence/matches.h"
#include "libmv/correspondence/feature.h"
#include "libmv/correspondence/klt.h"
#include "libmv/image/image.h"
#include "libmv/image/image_io.h"
#include "libmv/image/image_pyramid.h"
#include "libmv/image/image_sequence_io.h"
#include "libmv/image/cached_image_sequence.h"
#include "libmv/image/pyramid_sequence.h"
#include "third_party/gflags/gflags/gflags.h"
DEFINE_bool(debug_images, true, "Output debug images.");
DEFINE_double(sigma, 0.9, "Blur filter strength.");
DEFINE_int32(pyramid_levels, 4, "Number of levels in the image pyramid.");
using namespace libmv;
using std::sort;
using std::string;
int main(int argc, char **argv) {
gflags::SetUsageMessage("Track a sequence.");
gflags::ParseCommandLineFlags(&argc, &argv, true);
std::vector<string> files;
for (int i = 1; i < argc; ++i) {
files.push_back(argv[i]);
}
sort(files.begin(), files.end());
if (files.size() < 2) {
printf("Not enough files.\n");
return 1;
}
ImageCache cache;
scoped_ptr<ImageSequence> source(ImageSequenceFromFiles(files, &cache));
PyramidSequence *pyramid_sequence =
MakePyramidSequence(source.get(), FLAGS_pyramid_levels, FLAGS_sigma);
KLTContext klt;
Matches matches;
scoped_ptr<ImagePyramid> pyramid(pyramid_sequence->Pyramid(0));
KLTContext::FeatureList features;
klt.DetectGoodFeatures(pyramid->Level(0), &features);
int i = 0;
for (KLTContext::FeatureList::iterator it = features.begin();
it != features.end(); ++it, ++i) {
matches.Insert(0, i, *it);
}
for (size_t i = 1; i < files.size(); ++i) {
printf("Tracking %2zd features in %s\n", features.size(), files[i].c_str());
for (Matches::Features<KLTPointFeature> r =
matches.InImage<KLTPointFeature>(i-1); r; ++r) {
KLTPointFeature *next_position = new KLTPointFeature;
if (klt.TrackFeature(pyramid_sequence->Pyramid(i-1), *r.feature(),
pyramid_sequence->Pyramid(i), next_position)) {
matches.Insert(i, r.track(), next_position);
} else {
delete next_position;
}
}
}
printf( "\n %2d tracks found\n", (int)matches.NumTracks());
return 0;
}