库及其依赖+测试工程: ceressolver使用.zip-C++文档类资源-CSDN下载

// ceres_solver_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include<ceres/ceres.h>
#include<opencv2/core/core.hpp>using namespace std;
using namespace ceres;
using namespace cv;//第一部分:构建代价函数,重载()符号,仿函数的小技巧
struct CostFunctor {template <typename T>bool operator()(const T* const x, T* residual) const {residual[0] = T(10.0) - x[0];return true;}
};//构建代价函数结构体,abc为待优化参数,residual为残差。
struct CURVE_FITTING_COST
{CURVE_FITTING_COST(double x, double y) :_x(x), _y(y) {}template <typename T>bool operator()(const T* const abc, T* residual)const{residual[0] = _y - ceres::exp(abc[0] * _x * _x + abc[1] * _x + abc[2]);return true;}const double _x, _y;
};//构建代价函数结构体,abc为待优化参数,residual为残差。
struct SIN_FITTING_COST
{SIN_FITTING_COST(double x, double y) :_x(x), _y(y) {}template <typename T>bool operator()(const T* const abc, T* residual)const{residual[0] = _y - (abc[0] + abc[1] * ceres::sin(_x + abc[2]));return true;}const double _x, _y;
};//主函数
int main(int argc, char** argv) {google::InitGoogleLogging(argv[0]);#if 0// 1/2*(10-x)^2// 寻优参数x的初始值,为5double initial_x = 5.0;double x = initial_x;// 第二部分:构建寻优问题Problem problem;CostFunction* cost_function =new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor); //使用自动求导,将之前的代价函数结构体传入,第一个1是输出维度,即残差的维度,第二个1是输入维度,即待寻优参数x的维度。problem.AddResidualBlock(cost_function, NULL, &x); //向问题中添加误差项,本问题比较简单,添加一个就行。//第三部分: 配置并运行求解器Solver::Options options;options.linear_solver_type = ceres::DENSE_QR; //配置增量方程的解法options.minimizer_progress_to_stdout = true;//输出到coutSolver::Summary summary;//优化信息Solve(options, &problem, &summary);//求解!!!std::cout << summary.BriefReport() << "\n";//输出优化的简要信息//最终结果std::cout << "x : " << initial_x<< " -> " << x << "\n";
#endif#if 0// y = e^(a * x * x + b * x + c)//参数初始化设置,abc初始化为0,白噪声方差为1(使用OpenCV的随机数产生器)。double a = 3, b = 2, c = 1;double w = 1;RNG rng;double abc[3] = { 0,0,0 };//生成待拟合曲线的数据散点,储存在Vector里,x_data,y_data。vector<double> x_data, y_data;for (int i = 0; i < 1000; i++){double x = i / 1000.0;x_data.push_back(x);y_data.push_back(std::exp(double(a * x * x + b * x + c)) + rng.gaussian(w));}//反复使用AddResidualBlock方法(逐个散点,反复1000次)//将每个点的残差累计求和构建最小二乘优化式//不使用核函数,待优化参数是abcceres::Problem problem;for (int i = 0; i < 1000; i++){problem.AddResidualBlock(new ceres::AutoDiffCostFunction<CURVE_FITTING_COST, 1, 3>(new CURVE_FITTING_COST(x_data[i], y_data[i])),nullptr,abc);}//配置求解器并求解,输出结果ceres::Solver::Options options;options.linear_solver_type = ceres::DENSE_QR;options.minimizer_progress_to_stdout = true;ceres::Solver::Summary summary;ceres::Solve(options, &problem, &summary);cout << "a= " << abc[0] << endl;cout << "b= " << abc[1] << endl;cout << "c= " << abc[2] << endl;#endif#if 1// y = a+b*sin(x+c)//参数初始化设置,abc初始化为0,白噪声方差为1(使用OpenCV的随机数产生器)。double a = 0.5, b = 0.4, c = CV_PI/6;double w = 0.01;RNG rng;double abc[3] = { 0,0,0 };//生成待拟合曲线的数据散点,储存在Vector里,x_data,y_data。vector<double> x_data, y_data;double step = (2 * CV_PI) / 200;for (int i = 0; i < 200; i++){double x = i * step;x_data.push_back(x);y_data.push_back(double(a + b * std::sin(x+c)) + rng.gaussian(w));}//反复使用AddResidualBlock方法(逐个散点,反复1000次)//将每个点的残差累计求和构建最小二乘优化式//不使用核函数,待优化参数是abcceres::Problem problem;for (int i = 0; i < 200; i++){problem.AddResidualBlock(new ceres::AutoDiffCostFunction<SIN_FITTING_COST, 1, 3>(new SIN_FITTING_COST(x_data[i], y_data[i])),nullptr,abc);}//配置求解器并求解,输出结果ceres::Solver::Options options;options.linear_solver_type = ceres::DENSE_QR;options.minimizer_progress_to_stdout = true;ceres::Solver::Summary summary;ceres::Solve(options, &problem, &summary);std::cout << "a= " << abc[0] << std::endl;std::cout << "b= " << abc[1] << std::endl;std::cout << "c= " << abc[2] << std::endl;#endifreturn 0;
}

ceres solver 使用相关推荐

  1. Ceres Solver Document学习笔记

    Ceres Solver Document学习笔记 Ceres Solver Document学习笔记 1. 基本概念 2. 基本方法 2.1 CostFunction 2.2 AutoDiffCos ...

  2. Ceres Solver 非线性优化库

    Ceres Solver 非线性优化库 1. Ceres Solver 2. 下载安装 3. 简易例程 4. 环境运行 5. 非线性拟合 1. Ceres Solver Ceres solver 是谷 ...

  3. Ceres Solver安装

    先是ceres solver的依赖安装 # CMake sudo apt-get install cmake # google-glog + gflags sudo apt-get install l ...

  4. Ceres Solver: 高效的非线性优化库(二)实战篇

    Ceres Solver: 高效的非线性优化库(二)实战篇 接上篇: Ceres Solver: 高效的非线性优化库(一) 如何求导 Ceres Solver提供了一种自动求导的方案,上一篇我们已经看 ...

  5. Ceres Solver从零开始手把手教学使用

    目录 一 .简介 二.安装 三.介绍 四.Hello Word! 五.导数 1 数值导数 2解析求导 六.实践 Powell函数 一 .简介 笔者已经半年没有更新新的内容了,最近学习视觉SLAM的过程 ...

  6. 【环境配置】ceres solver安装

    1. 安装 github地址 # CMake sudo apt-get install cmake # google-glog + gflags sudo apt-get install libgoo ...

  7. Ceres Solver安装及bug解决

    Ceres Solver安装与入门使用 安装教程:http://www.ceres-solver.org/installation.html 点击下载最新的稳定版 解压.编译 .测试.安装 1.Lin ...

  8. Ceres Solver:Terminating: Residual and Jacobian evaluation failed

    文章目录 前言 一.Ceres-Solver 二.解决方法 总结 前言 使用ceres-solver库求解非线性优化问题时,打印summary.message时出现报错: [trust_region_ ...

  9. Ceres Solver 官方教程学习笔记(十二)——非线性最小二乘法建模Modeling Non-linear Least Squares (下)

    这一部分主要是最后的Problem比较重要. 带条件的代价函数ConditionedCostFunction 这个类使用户可以在使用封装好的代价函数的同时,对残差值加入一定的条件限制.举个例子,现在已 ...

  10. Ceres solver安装与测试

    官方安装教程 点击下载最新的稳定版 解压.编译 .测试.安装 tar zxf ceres-solver-2.1.0.tar.gz mkdir ceres-bin cd ceres-bin cmake ...

最新文章

  1. navicat如何导入sql文件
  2. MySQL -- Lock wait timeout exceeded; try restarting transaction参数控制
  3. c# list 自定义排序
  4. vue --- SPA模式的组件
  5. [NOI2007]社交网络
  6. 多维度创新打造领先阿里云技术生态
  7. 搜狗高速浏览器怎么设置页面字体 设置方法介绍
  8. python中的split函数的用法实例_python中的split()函数的用法
  9. 150W光速秒充!realme真我GT Neo3正式发布 售价1999元起
  10. 爬虫抓图2022年全网最新方法,这一次终于是4k高清美图
  11. 计算机无线网络设备有哪些,电脑无线上网设备有哪几种
  12. 大数据智能算法及测评技术(二)
  13. 我不想安于当前的限度,以达到所谓的幸福,回顾下2020年的我
  14. 网红品牌的“敦刻尔克大撤退”
  15. 哈工大2019秋数据结构期末试题
  16. python安装环境变量出错_在windows 10上安装twisted时出错。INCLUDE环境变量为空
  17. 错误 LNK1104 无法打开文件“boost_thread-vc142-mt-gd-x64-1_79.lib”
  18. 涉密信息系统集成甲级资质需要什么条件?
  19. Tutorial: How to live stream a media file
  20. 上周热点回顾(10.22-10.28)

热门文章

  1. 基于php的心理测试,据说是韩国最受欢迎的心理测试~~
  2. 牛客网——B 遥远的记忆
  3. 深度技术 GHOST XP 电脑城克隆版 V7.0 (NTFS/F32)
  4. 2022年云南最新消防设施操作员模拟试题题库及答案
  5. JAVA小鑫の日常系列故事(七)——小纸条
  6. android游戏备份农场,zynga旗下的虚拟农场farmville将正式进入android平台
  7. 金融风控领域算法比赛经验分享——翼支付杯大数据建模大赛-季军方案
  8. 基于jsp+mysql+Spring的SSM在线蛋糕商城销售网站项目设计和实现
  9. Java常用的开发软件下载地址以及问题解决
  10. 夜神模拟器卡在android,夜神安卓模拟器很卡怎么办?模拟器卡顿解决方法分享...