35 std::cout <<
"Number of cameras = " <<
cameras.size() << std::endl;
36 std::cout <<
"Number of tracks = " <<
tracks.size() << std::endl;
55 for (
size_t j = 0;
j <
tracks.size(); ++
j) {
71 Matrix3 R_mat = Matrix3::Zero(3, 3);
92 return Pose3(cRw_openGL, t_openGL);
104 std::ifstream is(filename.c_str(), std::ifstream::in);
106 throw std::runtime_error(
107 "Error in FromBundlerFile: can not find the file!!");
114 is.getline(aux, 500);
117 size_t nrPoses, nrPoints;
118 is >> nrPoses >> nrPoints;
121 for (
size_t i = 0;
i < nrPoses;
i++) {
131 is >> r11 >> r12 >> r13 >> r21 >> r22 >> r23 >> r31 >> r32 >> r33;
134 Rot3 R(r11, r12, r13, r21, r22, r23, r31, r32, r33);
137 if (r11 == 0 && r12 == 0 && r13 == 0) {
138 throw std::runtime_error(
139 "Error in FromBundlerFile: zero rotation matrix");
144 is >> tx >> ty >> tz;
148 sfmData.
cameras.emplace_back(pose, K);
152 sfmData.
tracks.reserve(nrPoints);
153 for (
size_t j = 0;
j < nrPoints;
j++) {
174 for (
size_t k = 0; k < nvisible; k++) {
175 size_t cam_idx = 0, point_idx = 0;
177 is >> cam_idx >> point_idx >> u >>
v;
179 track.
siftIndices.emplace_back(cam_idx, point_idx);
182 sfmData.
tracks.push_back(track);
191 std::ifstream is(filename.c_str(), std::ifstream::in);
193 throw std::runtime_error(
"Error in FromBalFile: can not find the file!!");
199 size_t nrPoses, nrPoints, nrObservations;
200 is >> nrPoses >> nrPoints >> nrObservations;
202 sfmData.
tracks.resize(nrPoints);
205 for (
size_t k = 0; k < nrObservations; k++) {
208 is >> i >>
j >> u >>
v;
209 sfmData.
tracks[
j].measurements.emplace_back(i,
Point2(u, -v));
213 for (
size_t i = 0;
i < nrPoses;
i++) {
216 is >> wx >> wy >> wz;
221 is >> tx >> ty >> tz;
230 sfmData.
cameras.emplace_back(pose, K);
234 for (
size_t j = 0;
j < nrPoints;
j++) {
252 os.open(filename.c_str());
255 cout <<
"Error in writeBAL: can not open the file!!" << endl;
260 size_t nrObservations = 0;
261 for (
size_t j = 0;
j < data.
tracks.size();
j++) {
262 nrObservations += data.
tracks[
j].numberMeasurements();
266 os << data.
cameras.size() <<
" " << data.
tracks.size() <<
" " 267 << nrObservations << endl;
270 for (
size_t j = 0;
j < data.
tracks.size();
j++) {
276 double u0 = data.
cameras[
i].calibration().px();
277 double v0 = data.
cameras[
i].calibration().py();
279 if (u0 != 0 || v0 != 0) {
280 cout <<
"writeBAL has not been tested for calibration with nonzero " 289 Point2 pixelMeasurement(pixelBALx, pixelBALy);
290 os << i <<
" " <<
j <<
" " 291 << pixelMeasurement.x() <<
" " 292 << pixelMeasurement.y() << endl;
298 for (
size_t i = 0;
i < data.
cameras.size();
i++) {
306 os << cameraCalibration.
fx() << endl;
307 os << cameraCalibration.
k1() << endl;
308 os << cameraCalibration.
k2() << endl;
313 for (
size_t j = 0;
j < data.
tracks.size();
j++) {
315 os << point.x() << endl;
316 os << point.y() << endl;
317 os << point.z() << endl;
337 if (nrPoses == dataValues.
cameras.size()) {
338 for (
size_t i = 0;
i < dataValues.
cameras.size();
i++) {
346 if (nrCameras == dataValues.
cameras.size()) {
348 for (
size_t i = 0;
i < nrCameras;
i++) {
354 cout <<
"writeBALfromValues: different number of cameras in " 355 "SfM_dataValues (#cameras " 356 << dataValues.
cameras.size() <<
") and values (#cameras " << nrPoses
357 <<
", #poses " << nrCameras <<
")!!" << endl;
364 if (nrPoints != nrTracks) {
365 cout <<
"writeBALfromValues: different number of points in " 366 "SfM_dataValues (#points= " 367 << nrTracks <<
") and values (#points " << nrPoints <<
")!!" << endl;
370 for (
size_t j = 0;
j < nrTracks;
j++) {
372 if (values.
exists(pointKey)) {
384 return writeBAL(filename, dataValues);
409 std::optional<size_t> fixedPoint)
const {
SfmData readBal(const std::string &filename)
This function parses a "Bundle Adjustment in the Large" (BAL) file and returns the data as a SfmData ...
const SfmCamera & camera(size_t idx) const
The camera pose at frame index idx
const Point3 & translation(OptionalJacobian< 3, 6 > Hself={}) const
get translation
SfmData stores a bunch of SfmTracks.
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
noiseModel::Diagonal::shared_ptr model
const ValueType at(Key j) const
std::vector< SiftIndex > siftIndices
The feature descriptors (optional)
Rot2 R(Rot2::fromAngle(0.1))
NonlinearFactorGraph generalSfmFactors(const SharedNoiseModel &model=noiseModel::Isotropic::Sigma(2, 1.0)) const
Create projection factors using keys i and P(j)
const GaussianFactorGraph factors
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Values initialCamerasAndPointsEstimate(const SfmData &db)
This function creates initial values for cameras and points from db.
const SfmTrack & track(size_t idx) const
The track formed by series of landmark measurements.
static SfmData FromBalFile(const std::string &filename)
Parse a "Bundle Adjustment in the Large" (BAL) file and return result as SfmData instance.
Pose3 openGL2gtsam(const Rot3 &R, double tx, double ty, double tz)
This function converts an openGL camera pose to an GTSAM camera pose.
NonlinearFactorGraph graph
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
const gtsam::Key pointKey
static SfmData FromBundlerFile(const std::string &filename)
Parses a bundler output file and return result as SfmData instance.
void g(const string &key, int i)
std::vector< SfmTrack > tracks
Sparse set of points.
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Rot3 inverse() const
inverse of a rotation
std::pair< size_t, Point2 > SfmMeasurement
A measurement with its camera index.
bool writeBALfromValues(const std::string &filename, const SfmData &data, const Values &values)
This function writes a "Bundle Adjustment in the Large" (BAL) file from a SfmData structure and a val...
static Rot3 Rodrigues(const Vector3 &w)
size_t numberMeasurements() const
Total number of measurements in this track.
Point3 unrotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
rotate point from world to rotated frame
Rot3 openGLFixedRotation()
static Vector3 Logmap(const Rot3 &R, OptionalJacobian< 3, 3 > H={})
double k1() const
distorsion parameter k1
double fx() const
focal length x
Array< int, Dynamic, 1 > v
Data structure for dealing with Structure from Motion data.
std::vector< SfmMeasurement > measurements
The 2D image projections (id,(u,v))
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
bool equals(const Base &camera, double tol=1e-9) const
assert equality up to a tolerance
Values initialCamerasEstimate(const SfmData &db)
This function creates initial values for cameras from db.
Class compose(const Class &g) const
bool equals(const SfmTrack &sfmTrack, double tol=1e-9) const
assert equality up to a tolerance
double k2() const
distorsion parameter k2
std::vector< SfmCamera > cameras
Set of cameras.
static const Pose3 pose(Rot3(Vector3(1, -1, -1).asDiagonal()), Point3(0, 0, 0.5))
Calibration used by Bundler.
ofstream os("timeSchurFactors.csv")
bool equals(const SfmData &sfmData, double tol=1e-9) const
assert equality up to a tolerance
Expression< T > compose(const Expression< T > &t1, const Expression< T > &t2)
Point3 p
3D position of the point
float b
RGB color of the 3D point.
void insert(Key j, const Value &val)
void print(const std::string &s="") const
print
const Rot3 & rotation(OptionalJacobian< 3, 6 > Hself={}) const
get rotation
Pose3 gtsam2openGL(const Rot3 &R, double tx, double ty, double tz)
This function converts a GTSAM camera pose to an openGL camera pose.
Point3 rotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
NonlinearFactorGraph sfmFactorGraph(const SharedNoiseModel &model=noiseModel::Isotropic::Sigma(2, 1.0), std::optional< size_t > fixedCamera=0, std::optional< size_t > fixedPoint=0) const
Create factor graph with projection factors and gauge fix.
a general SFM factor with an unknown calibration
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
std::uint64_t Key
Integer nonlinear key type.
bool writeBAL(const std::string &filename, const SfmData &data)
This function writes a "Bundle Adjustment in the Large" (BAL) file from a SfmData structure...
noiseModel::Base::shared_ptr SharedNoiseModel