#include <list>
#include <string>
#include "libmv/correspondence/import_matches_txt.h"
#include "libmv/correspondence/tracker.h"
#include "libmv/logging/logging.h"
#include "libmv/reconstruction/euclidean_reconstruction.h"
#include "libmv/reconstruction/export_blender.h"
#include "libmv/reconstruction/export_ply.h"
using namespace libmv;
DEFINE_string(i, "matches.txt", "Matches input file");
DEFINE_string(o, "reconstruction.py", "Reconstruction output file");
DEFINE_int32(w, 0, "Image width (px)");
DEFINE_int32(h, 0, "Image height (px)");
DEFINE_double(f, 50,
"Focale length for all the cameras (px)");
DEFINE_double(u0, 0,
"Principal point u coordinate (px)");
DEFINE_double(v0, 0,
"Principal point v coordinate (px)");
void GetFilePathExtention(const std::string &file,
std::string *path_name,
std::string *ext) {
size_t dot_pos = file.rfind (".");
if (dot_pos != std::string::npos) {
*path_name = file.substr(0, dot_pos);
*ext = file.substr(dot_pos + 1, file.length() - dot_pos - 1);
} else {
*path_name = file;
*ext = "";
}
}
int main (int argc, char *argv[]) {
std::string usage ="Estimate the camera trajectory using matches.\n";
usage += "Usage: " + std::string(argv[0]) + " -i INFILE.txt -o OUTFILE.ply.";
gflags::SetUsageMessage(usage);
gflags::ParseCommandLineFlags(&argc, &argv, true);
tracker::FeaturesGraph fg;
FeatureSet *fs = fg.CreateNewFeatureSet();
VLOG(0) << "Loading Matches file..." << std::endl;
ImportMatchesFromTxt(FLAGS_i, &fg.matches_, fs);
VLOG(0) << "Loading Matches file...[DONE]." << std::endl;
int w = FLAGS_w, h = FLAGS_h;
if (FLAGS_u0 > 0)
w = 2 * (FLAGS_u0 + 0.5);
if (FLAGS_v0 > 0)
h = 2 * (FLAGS_v0 + 0.5);
VLOG(0) << "Euclidean Reconstruction From Video..." << std::endl;
std::list<Reconstruction *> reconstructions;
EuclideanReconstructionFromVideo(fg.matches_,
w, h,
FLAGS_f,
&reconstructions);
VLOG(0) << "Euclidean Reconstruction From Video...[DONE]" << std::endl;
VLOG(0) << "Exporting Reconstructions..." << std::endl;
std::string file_path_name, file_ext;
GetFilePathExtention(FLAGS_o, &file_path_name, &file_ext);
std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(), ::tolower);
int i = 0;
std::list<Reconstruction *>::iterator iter = reconstructions.begin();
if (file_ext == "ply") {
for (; iter != reconstructions.end(); ++iter) {
std::stringstream s;
if (reconstructions.size() > 1)
s << file_path_name << "-" << i << ".ply";
else
s << FLAGS_o;
ExportToPLY(**iter, s.str());
}
} else if (file_ext == "py") {
for (; iter != reconstructions.end(); ++iter) {
std::stringstream s;
if (reconstructions.size() > 1)
s << file_path_name << "-" << i << ".py";
else
s << FLAGS_o;
ExportToBlenderScript(**iter, s.str());
}
}
VLOG(0) << "Exporting Reconstructions...[DONE]" << std::endl;
VLOG(0) << "Cleaning." << std::endl;
iter = reconstructions.begin();
for (; iter != reconstructions.end(); ++iter) {
(*iter)->ClearCamerasMap();
(*iter)->ClearStructuresMap();
delete *iter;
}
reconstructions.clear();
fg.DeleteAndClear();
return 0;
}