安装
- 安装依赖项
sudo apt-get install cmake
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libeigen3-dev
sudo apt-get install libsuitesparse-dev
- 下载安装
git clone https://github.com/ceres-solver/ceres-solver.git
mkdir ceres-bin
cd ceres-bin
cmake ..
sudo make install
- cmakelist.txt配置
方法一:
add_compile_options(-std=c++14)
find_package(Ceres REQUIRED)
include_directories(${Ceres_INCLUDE_DIRS})
add_executable(ceres_1 ${ceres_1_src})
target_link_libraries(ceres_1 ${Ceres_INCLUDE_DIRS})
方法2:
如果使用方式一配置编译时出现如下错误:
使用如下方式配置:
add_compile_options(-std=c++14)
#指定库路径
file(GLOB_RECURSE Opencv2.7_LIB "/usr/lib/python2.7/config-x86_64-linux-gnu/*.so")
file(GLOB_RECURSE Creres_LIB "/usr/local/lib/libceres.a")
file(GLOB_RECURSE cholmod_LIB "/usr/lib/x86_64-linux-gnu/libcholmod.so")
file(GLOB_RECURSE lapack_LIB "/usr/lib/lapack/liblapack.so")
file(GLOB_RECURSE f77blas_LIB "/usr/lib/libf77blas.so")
file(GLOB_RECURSE cxsparse_LIB "/usr/lib/x86_64-linux-gnu/libcxsparse.so")
file(GLOB_RECURSE glog_LIB "/usr/lib/x86_64-linux-gnu/libglog.so")
file(GLOB_RECURSE pthread_LIB "/usr/lib/x86_64-linux-gnu/libpthread.so")
#指定头文件路径
set(Opencv2.7_INLCUDE_DIRS "/usr/include/python2.7")
set(Creres_INLCUDE_DIRS "/usr/local/include/ceres")
set(suitespars_INLCUDE_DIRS "/usr/include/suitespars")
set(lapack_INLCUDE_DIRS "/usr/include/eigen3/Eigen/src/misc")
set(glog_INLCUDE_DIRS "/usr/include/glog")
set(pthread_INLCUDE_DIRS "/usr/include/boost/thread")
#添加头文件到工程
include_directories(include ${Opencv2.7_INLCUDE_DIRS} )
include_directories(include ${Creres_INLCUDE_DIRS} )
include_directories(include ${suitespars_INLCUDE_DIRS} )
include_directories(include ${lapack_INLCUDE_DIRS} )
include_directories(include ${glog_INLCUDE_DIRS} )
include_directories(include ${pthread_INLCUDE_DIRS} )
# include_directories("/usr/local/include/eigen3")
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/1 ceres_1_src)
# Declare the executable target built from your sources
add_executable(ceres_1 ${ceres_1_src})
# Link your application with OpenCV libraries
# target_link_libraries(undistort ${Pangolin_LIBRARIES})
target_link_libraries(ceres_1 ${Creres_LIB} ${cholmod_LIB} ${lapack_LIB} ${f77blas_LIB} ${cxsparse_LIB} ${pthread_LIB} ${glog_LIB} )
一个例子
一个目标函数如下
#include "/usr/local/include/ceres/ceres.h"
#include <glog/logging.h>
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solve;
using ceres::Solver;
using namespace ceres;
//定义代价函数
struct CostFunctor {
template <typename T>
bool operator()(const T* x, T* residual) const {
residual[0] = 10.0 - x[0];
return true;
}
};
int main(int argc, char** argv) {
//初始画参数
double x = 0.5;
const double initial_x = x;
//构建问题
Problem problem;
//设置残差
CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
//AutoDiffCostFunction 求导
//CostFunctor 代价函数
//1 惨差的维数
//1 输入数据的维数
problem.AddResidualBlock(cost_function, NULL, &x);
//目标函数
//cost_function:代价函数
//NULL:核函数,这里不使用用
//x:待优化参数
//进行求解
Solver::Options options;//建议一个求解问题类
options.linear_solver_type=ceres::DENSE_QR;//配置增量方程的解法
options.minimizer_progress_to_stdout = true;//是否在打印输出
Solver::Summary summary;//优化信息
Solve(options, &problem, &summary);//求解
std::cout << summary.BriefReport() << "\n";//输出优化的简要信息
std::cout << "x : " << initial_x << " -> " << x << "\n";//输出优化结果
return 0;
}
- 步骤1:定义代价函数:
//定义代价函数
struct CostFunctor{
template <typename T>//模板类T
bool operator()(const T* x,T* residual) const
{
residual[0]=10.0-x[0];
return true;
}
};
- 构建问题
//构建问题
Problem problem;
//设置残差
CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
//AutoDiffCostFunction 求导
//CostFunctor 代价函数
//1 输入数据的个数
//1 每个输入数据的维数
problem.AddResidualBlock(cost_function, NULL, &x);
//目标函数
//cost_function:代价函数
//NULL:核函数,这里不使用用
问题求解
//进行求解
Solver::Options options;//建议一个求解问题类
options.linear_solver_type=ceres::DENSE_QR;//配置增量方程的解法
options.minimizer_progress_to_stdout = true;//是否在打印输出
Solver::Summary summary;//优化信息
Solve(options, &problem, &summary);//求解