Skip to content

Commit

Permalink
adding support for Ceres / cleaning up code some more
Browse files Browse the repository at this point in the history
  • Loading branch information
snavely committed Dec 29, 2013
1 parent 1679fc2 commit 886b7c6
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 146 deletions.
34 changes: 17 additions & 17 deletions src/BaseApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ class BaseApp
/* Output routines */

/* IO routines */
void ReadGeometricConstraints(char *filename);
void WriteGeometricConstraints(char *filename);
void WriteTracksForSameer(char *filename);
void ReadGeometricConstraints(const char *filename);
void WriteGeometricConstraints(const char *filename);
void WriteTracks(char *filename);
void WriteTracks2(char *filename);
void ReadCameraConstraints();
void ReadPointConstraints();
void ReadIntrinsicsFile();
Expand All @@ -405,12 +405,12 @@ class BaseApp
void ReadKeyColors();

/* Read in information about the world */
void ReadBundleFile(char *filename);
void ReloadBundleFile (char *filename);
void ReadBundleFile(const char *filename);
void ReloadBundleFile (const char *filename);

/* Read/write line segments */
void ReadLines3D(char *filename);
void WriteLines3D(char *filename);
void ReadLines3D(const char *filename);
void WriteLines3D(const char *filename);

/* Clear the current model */
void ClearModel();
Expand Down Expand Up @@ -448,14 +448,14 @@ class BaseApp

#ifndef __DEMO__
/* Write point files to a ply file */
void DumpPointsToPly(char *output_directory, char *filename,
void DumpPointsToPly(const char *output_directory, const char *filename,
int num_points, int num_cameras,
v3_t *points, v3_t *colors, camera_params_t *cameras
/*bool reflect = true*/);

/* Dump an output file containing information about the current
* state of the world */
void DumpOutputFile(char *output_dir, char *filename,
void DumpOutputFile(const char *output_dir, const char *filename,
int num_images, int num_cameras, int num_points,
int *added_order,
camera_params_t *cameras, v3_t *points, v3_t *colors,
Expand Down Expand Up @@ -578,14 +578,14 @@ class BaseApp
bool m_bundle_provided; /* Was a bundle adjustment file given? */
char *m_bundle_file; /* Bundle file */

char *m_match_directory; /* Which directory are the matches
* stored in? */
char *m_match_index_dir; /* Which directory are match indexes
* stored in? */
char *m_match_table; /* File where match table is stored */
char *m_key_directory;
char *m_image_directory;
char *m_sift_binary; /* Where can we find the sift binary? */
const char *m_match_directory; /* Which directory are the matches
* stored in? */
const char *m_match_index_dir; /* Which directory are match indexes
* stored in? */
const char *m_match_table; /* File where match table is stored */
const char *m_key_directory;
const char *m_image_directory;
const char *m_sift_binary; /* Where can we find the sift binary? */

bool m_estimate_up_vector_szeliski; /* Estimate the up vector
* using Rick's method? */
Expand Down
7 changes: 4 additions & 3 deletions src/BaseGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void BaseApp::WriteTrackPairs(const char *filename)
fclose(f);
}

void BaseApp::ReadGeometricConstraints(char *filename) {
void BaseApp::ReadGeometricConstraints(const char *filename) {
FILE *f = fopen(filename, "r");

int num_images; // = GetNumImages();
Expand Down Expand Up @@ -270,7 +270,7 @@ void BaseApp::ReadGeometricConstraints(char *filename) {
// WriteGeometricConstraints("constraints_test.txt");
}

void BaseApp::WriteGeometricConstraints(char *filename) {
void BaseApp::WriteGeometricConstraints(const char *filename) {
FILE *f = fopen(filename, "w");

if (f == NULL) {
Expand Down Expand Up @@ -392,7 +392,8 @@ void BaseApp::WriteTracks(char *filename)
fclose(f);
}

void BaseApp::WriteTracksForSameer(char *filename) {
void BaseApp::WriteTracks2(char *filename)
{
FILE *f = fopen(filename, "w");

if (f == NULL) {
Expand Down
73 changes: 54 additions & 19 deletions src/Bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,9 @@ void BundlerApp::ReRunSFM(double *S, double *U, double *V, double *W)
num_images, num_init_cams, num_pts,
added_order, cameras, init_pts, colors, pt_views);

RunSFM(num_pts, num_init_cams, 0, false, cameras, init_pts, added_order,
colors, pt_views, 0.0 /*eps2*/, S, U, V, W);
RunSFM(num_pts, num_init_cams, 0, false, cameras,
init_pts, added_order, colors, pt_views,
0, 0, 0, 0.0 /*eps2*/, S, U, V, W);

/* Save the camera parameters and points */

Expand Down Expand Up @@ -530,13 +531,50 @@ static int compare_doubles(const void *d1, const void *d2)
return 0;
}


double BundlerApp::RunSFM(int num_pts, int num_cameras, int start_camera,
bool fix_points, camera_params_t *init_camera_params,
bool fix_points,
camera_params_t *init_camera_params,
v3_t *init_pts, int *added_order, v3_t *colors,
std::vector<ImageKeyVector> &pt_views, double eps2,
std::vector<ImageKeyVector> &pt_views,
int max_iter, int max_iter2,
int verbosity, double eps2,
double *S, double *U, double *V, double *W,
bool remove_outliers)
bool remove_outliers, bool final_bundle,
bool write_intermediate)
{
#ifdef __USE_CERES__
if (!m_use_ceres) {
return
RunSFM_SBA(num_pts, num_cameras, start_camera, fix_points,
init_camera_params, init_pts,
added_order, colors, pt_views, eps2, S, U, V, W,
remove_outliers);
} else { /* use_ceres */
return
RunSFM_Ceres(num_pts, num_cameras, start_camera, fix_points,
init_camera_params, init_pts, added_order, colors, pt_views,
max_iter, max_iter2, verbosity, eps2, S, U, V, W,
remove_outliers, final_bundle, write_intermediate);
}
#else
/* Just use SBA */
return
RunSFM_SBA(num_pts, num_cameras, start_camera, fix_points,
init_camera_params, init_pts,
added_order, colors, pt_views,
max_iter, max_iter2, verbosity, eps2, S, U, V, W,
remove_outliers, final_bundle, write_intermediate);
#endif
}

double BundlerApp::RunSFM_SBA(int num_pts, int num_cameras, int start_camera,
bool fix_points,
camera_params_t *init_camera_params,
v3_t *init_pts, int *added_order, v3_t *colors,
std::vector<ImageKeyVector> &pt_views,
double eps2,
double *S, double *U, double *V, double *W,
bool remove_outliers)
{
#define MIN_POINTS 20
int num_outliers = 0;
Expand All @@ -547,6 +585,8 @@ double BundlerApp::RunSFM(int num_pts, int num_cameras, int start_camera,
int *remap = new int [num_pts];
v3_t *nz_pts = new v3_t[num_pts];

const int MIN_OUTLIERS = 40;

do {
if (num_pts - total_outliers < MIN_POINTS) {
printf("[RunSFM] Too few points remaining, exiting!\n");
Expand Down Expand Up @@ -863,7 +903,7 @@ double BundlerApp::RunSFM(int num_pts, int num_cameras, int start_camera,

if (!remove_outliers) break;

} while (num_outliers > 0);
} while (num_outliers > MIN_OUTLIERS);

delete [] remap;
delete [] nz_pts;
Expand Down Expand Up @@ -1862,11 +1902,9 @@ void BundlerApp::EstimateIgnoredCameras(int &curr_num_cameras,
}
}

#if 1
/* TEST */
RunSFM(curr_num_pts, curr_num_cameras, 0, true,
cameras, points, added_order, colors, pt_views, 1.0e-20 /*eps*/);
#endif
cameras, points, added_order, colors, pt_views,
1.0e-20 /*eps*/);

int pt_count =
BundleAdjustAddAllNewPoints(curr_num_pts, curr_num_cameras,
Expand Down Expand Up @@ -1916,11 +1954,8 @@ void BundlerApp::EstimateIgnoredCameras(int &curr_num_cameras,
#endif
}

#if 1
/* TEST */
RunSFM(curr_num_pts, curr_num_cameras, 0, true,
cameras, points, added_order, colors, pt_views, 1.0e-20);
#endif
cameras, points, added_order, colors, pt_views, 1.0e-20);

pt_count =
BundleAdjustAddAllNewPoints(curr_num_pts, curr_num_cameras,
Expand Down Expand Up @@ -2103,8 +2138,8 @@ void BundlerApp::BundleAdjust()
double *S = new double[2 * 2 * cnp * cnp];
double error0;
error0 = RunSFM(curr_num_pts, 2, 0, false,
cameras, points, added_order, colors, pt_views, 0.0,
S, NULL, NULL, NULL, !m_fix_necker);
cameras, points, added_order, colors, pt_views,
0, 0, 0, 0.0, S, NULL, NULL, NULL, !m_fix_necker);

delete [] S;

Expand Down Expand Up @@ -2332,7 +2367,7 @@ void BundlerApp::BundleAdjust()

/* Run sfm again to update parameters */
RunSFM(curr_num_pts, round + 1, 0, false,
cameras, points, added_order, colors, pt_views);
cameras, points, added_order, colors, pt_views);

/* Remove bad points and cameras */
RemoveBadPointsAndCameras(curr_num_pts, curr_num_cameras + 1,
Expand Down Expand Up @@ -3528,7 +3563,7 @@ void BundlerApp::BundleInitializeImageFullBundle(int image_idx, int parent_idx,
if (!m_skip_full_bundle) {
/* Run sfm again to update parameters */
RunSFM(num_points, num_cameras + 1, 0, false,
cameras, points, added_order, colors, pt_views);
cameras, points, added_order, colors, pt_views);
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/Bundle2PMVS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct

} point_t;

void ReadListFile(char *list_file, std::vector<std::string> &files)
void ReadListFile(const char *list_file, std::vector<std::string> &files)
{
FILE *f = fopen(list_file, "r");

Expand All @@ -41,7 +41,7 @@ void ReadListFile(char *list_file, std::vector<std::string> &files)
fclose(f);
}

void ReadBundleFile(char *bundle_file,
void ReadBundleFile(const char *bundle_file,
std::vector<camera_params_t> &cameras,
std::vector<point_t> &points, double &bundle_version)
{
Expand Down Expand Up @@ -141,7 +141,8 @@ void ReadBundleFile(char *bundle_file,
fclose(f);
}

void WritePMVS(const char *output_path, char *list_file, char *bundle_file,
void WritePMVS(const char *output_path,
const char *list_file, const char *bundle_file,
std::vector<std::string> images,
std::vector<camera_params_t> &cameras)
{
Expand Down Expand Up @@ -261,9 +262,9 @@ int main(int argc, char **argv)
return 1;
}

char *list_file = argv[1];
char *bundle_file = argv[2];
char *output_path = "pmvs";
const char *list_file = argv[1];
const char *bundle_file = argv[2];
const char *output_path = "pmvs";

if (argc == 4)
output_path = argv[3];
Expand Down
4 changes: 2 additions & 2 deletions src/BundleCeres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,8 @@ double BundlerApp::RunSFM_Ceres(int num_pts, int num_cameras,
printf("[RunSFM] %d points completely removed\n", num_dead);

RemoveBadPointsAndCameras(num_pts, num_cameras,
added_order, init_camera_params, init_pts, colors,
pt_views);
added_order, init_camera_params,
init_pts, colors, pt_views);
} else if (round == 0) {
RemoveBadPointsAndCameras(num_pts, num_cameras,
added_order, init_camera_params,
Expand Down
20 changes: 13 additions & 7 deletions src/BundleFast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,20 @@ void BundlerApp::BundleAdjustFast()
if (max_matches < m_min_max_matches)
break; /* No more connections */

/* Find all images with 90% of the matches of the maximum */
/* Find all images with 75% of the matches of the maximum
* (unless overruled by m_num_points_add_camera) */
std::vector<ImagePair> image_set;

if (false && max_matches < 48) {
if (false && max_matches < 48) { /* disabling this */
image_set.push_back(ImagePair(max_cam, parent_idx));
} else {
// int nMatches = MIN(100, iround(0.75 /* 0.9 */ * max_matches));
int nMatches = iround(0.75 /* 0.9 */ * max_matches);
int nMatches = iround(0.75 * max_matches);

if (m_num_matches_add_camera > 0) {
/* Alternate threshold based on user parameter */
nMatches = std::min(nMatches, m_num_matches_add_camera);
}

image_set =
FindCamerasWithNMatches(nMatches,
curr_num_cameras, curr_num_pts,
Expand Down Expand Up @@ -312,10 +318,11 @@ void BundlerApp::BundleAdjustFast()
bool success = false;
camera_params_t camera_new =
BundleInitializeImage(m_image_data[next_idx],
next_idx, curr_num_cameras + image_count,
next_idx,
curr_num_cameras + image_count,
curr_num_cameras, curr_num_pts,
added_order, points,
NULL /*cameras + parent_idx*/, cameras,
NULL, cameras,
pt_views, &success);

if (success) {
Expand Down Expand Up @@ -497,7 +504,6 @@ void BundlerApp::BundleAdjustFast()
#else
pdata.m_views = pt_views[i];
#endif
// pdata.m_views = pt_views[i];

m_point_data.push_back(pdata);
}
Expand Down
Loading

0 comments on commit 886b7c6

Please sign in to comment.