使用ceres求解BA,需要用到的.h文件已经在源文件中注释

#include < iostream>
#include <ceres/ceres.h>
#include “common.h”
#include “SnavelyReprojectionError.h”

using namespace std;

void SolveBA(BALProblem &bal_problem);

int main(int argc, char **argv) {
if (argc != 2) {
cout << “usage: bundle_adjustment_ceres bal_data.txt” << endl;
return 1;
}
//
BALProblem bal_problem(argv[1]);读取第一个相机信息
bal_problem.Normalize();
bal_problem.Perturb(0.1, 0.5, 0.5);
bal_problem.WriteToPLYFile(“initial.ply”);//把优化前后的问题写为.ply点云文件,使用Meshlab可以打开点云
SolveBA(bal_problem);
bal_problem.WriteToPLYFile(“final.ply”);
//
return 0;
}

void SolveBA(BALProblem &bal_problem) {
const int point_block_size = bal_problem.point_block_size();
const int camera_block_size = bal_problem.camera_block_size();
读取points和camera
double *points = bal_problem.mutable_points();
double *cameras = bal_problem.mutable_cameras();
//
// Observations is 2 * num_observations long array observations
// [u_1, u_2, … u_n], where each u_i is two dimensional, the x
// and y position of the observation.
const double *observations = bal_problem.observations();读取observations,与points是一一对应的,他是一个数组,存储u,v值。
ceres::Problem problem;
//
for (int i = 0; i < bal_problem.num_observations(); ++i) {
ceres::CostFunction *cost_function;
//
// Each Residual block takes a point and a camera as input
// and outputs a 2 dimensional Residual
cost_function = SnavelyReprojectionError::Create(observations[2 * i + 0], observations[2 * i + 1]);
//
// If enabled use Huber’s loss function.
ceres::LossFunction *loss_function = new ceres::HuberLoss(1.0);
//
// Each observation corresponds to a pair of a camera and a point
// which are identified by camera_index()[i] and point_index()[i]
// respectively.
double *camera = cameras + camera_block_size * bal_problem.camera_index()[i];
double *point = points + point_block_size * bal_problem.point_index()[i];
//
problem.AddResidualBlock(cost_function, loss_function, camera, point);camera和point填充到bool operator中
}
//
// show some information here …
std::cout << “bal problem file loaded…” << std::endl;
std::cout << "bal problem have " << bal_problem.num_cameras() << " cameras and "
<< bal_problem.num_points() << " points. " << std::endl;
std::cout << "Forming " << bal_problem.num_observations() << " observations. " << std::endl;
//
std::cout << "Solving ceres BA … " << endl;
ceres::Solver::Options options;
options.linear_solver_type = ceres::LinearSolverType::SPARSE_SCHUR;
options.minimizer_progress_to_stdout = true;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
std::cout << summary.FullReport() << “\n”;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值