//拟合函数: y=ax^4+bx^3+cx^2+dx+e
#include <iostream>
#include <opencv2/core/core.hpp>
#include <ceres/ceres.h>
struct function_residual
{
function_residual(double x_, double y_) : x(x_), y(y_){}
template<typename T>
bool operator()(const T* const abcde, T* residual) const
{
residual[0] = T(y)-(abcde[0]*T(x)*T(x)*T(x)*T(x)+
abcde[1]*T(x)*T(x)*T(x)+
abcde[2]*T(x)*T(x)+
abcde[3]*T(x)+
abcde[4]);//这里记得要将x和y进行T类型转换!!!
return true;//这里记得return!!!!!!!!
}
const double x, y;
};
int main()
{
using namespace std;
double a=1.0, b=2.0, c=3.0, d=4.0, e=5.0;
int N = 100;
cv::RNG rng;//创建一个随机数发生器random number generator
vector<double> x_data, y_data;
for (int i = 0; i < N; ++i)
{
double x=i/10.0;//这里也要注意,要用10.0将运算自动转型
x_data.push_back(x);
试用ceres写一个拟合
最新推荐文章于 2025-05-16 10:49:17 发布