std vector 二维数组 和 Eigen Matrix 的相互转换

文章详细展示了如何在Eigen++中处理向量与Matrix的双向转换,适用于计算密集型任务。
摘要由CSDN通过智能技术生成

Eigen C++开源模板库,支持各种计算。

遇到了二维数组和Eigen Matrix相互转换的问题。

先将vector转换为Matrix使用Eigen计算,完成计算后再将结果转换为vector

首先包含头文件

#include <Eigen/Dense>

std::vector 二维 转 Eigen Matrix

Eigen::MatrixXf toEigenMatrix(std::vector<std::vector<float> > matrix)
{
    Eigen::MatrixXf outMat(matrix.size(), matrix[0].size());
    for (int i = 0; i < matrix.size(); i++)
    {
        outMat.row(i) = Eigen::VectorXf::Map(&matrix[i][0], matrix[0].size());
    }
    return outMat;
}

Eigen Matrix 转回 std::vector

std::vector<std::vector<float>> toVector(Eigen::MatrixXf matrix)
{
    std::vector<std::vector<float>> output(matrix.rows(), std::vector<float>(matrix.cols()));
    for (int i = 0; i < matrix.rows(); i++)
    {
        Eigen::VectorXf p = matrix.row(i);
        Eigen::VectorXf::Map(&output[i][0], p.size()) = p;
    }
    return output;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Ceres Solver求解二维变换矩阵时,可以使用以下步骤: 1. 定义优化问题:定义Ceres Solver中的Problem对象,设置优化参数和残差函数,其中残差函数是将实际值与预测值之间的差异表示为非负数的函数。 2. 定义变换模型:定义二维变换模型,例如旋转、平移、缩放等,可以使用Eigen库中的MatrixVector进行表示。 3. 定义残差函数:根据变换模型,定义残差函数,将实际值与预测值之间的差异表示为非负数。可以使用Eigen库中的MatrixVector进行计算。 4. 添加残差项:将残差函数添加到优化问题中,使用Ceres Solver中的AddResidualBlock函数。 5. 设置初始值:设置变换矩阵的初始值,可以使用Eigen库中的MatrixVector进行表示。 6. 运行求解器:运行Ceres Solver中的Solve函数,求解变换矩阵的最优值。 下面是一个使用Ceres Solver求解二维变换矩阵的示例代码: ``` #include "ceres/ceres.h" #include "Eigen/Dense" #include <iostream> using namespace ceres; using namespace Eigen; // 定义二维变换模型,包括旋转、平移和缩放 class TransformModel { public: TransformModel(double angle, double tx, double ty, double scale) : angle_(angle), tx_(tx), ty_(ty), scale_(scale) {} // 定义变换矩阵 Matrix3d getTransform() const { Matrix3d T; T << scale_ * cos(angle_), -scale_ * sin(angle_), tx_, scale_ * sin(angle_), scale_ * cos(angle_), ty_, 0, 0, 1; return T; } private: double angle_; double tx_; double ty_; double scale_; }; // 定义残差函数 class TransformCostFunction : public SizedCostFunction<2, 2> { public: TransformCostFunction(const Vector2d& observed, const TransformModel& model) : observed_(observed), model_(model) {} virtual ~TransformCostFunction() {} virtual bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const { const Vector2d predicted = model_.getTransform().block<2, 2>(0, 0) * Vector2d(parameters[0][0], parameters[0][1]) + Vector2d(parameters[0][2], parameters[0][3]); residuals[0] = predicted[0] - observed_[0]; residuals[1] = predicted[1] - observed_[1]; if (jacobians != NULL && jacobians[0] != NULL) { Matrix<double, 2, 4> jacobian; jacobian << model_.getTransform().block<2, 2>(0, 0), Matrix2d::Identity(); jacobian *= model_.getTransform().block<2, 2>(0, 0).transpose(); memcpy(jacobians[0], jacobian.data(), sizeof(double) * 8); } return true; } private: const Vector2d observed_; const TransformModel model_; }; int main(int argc, char** argv) { // 定义二维数据点 Vector2d observed(1.0, 2.0); // 定义初始值 double angle = 0.1; double tx = 0.2; double ty = 0.3; double scale = 1.1; double parameters[4] = { cos(angle), -sin(angle), tx, ty }; // 定义优化问题 Problem problem; // 添加残差项 CostFunction* cost_function = new TransformCostFunction(observed, TransformModel(angle, tx, ty, scale)); problem.AddResidualBlock(cost_function, NULL, parameters); // 设置优化参数 Solver::Options options; options.minimizer_progress_to_stdout = true; options.max_num_iterations = 100; // 运行求解器 Solver::Summary summary; Solve(options, &problem, &summary); // 输出结果 std::cout << summary.BriefReport() << std::endl; std::cout << "Initial parameters: " << cos(angle) << ", " << -sin(angle) << ", " << tx << ", " << ty << std::endl; std::cout << "Final parameters: " << parameters[0] << ", " << parameters[1] << ", " << parameters[2] << ", " << parameters[3] << std::endl; return 0; } ``` 在这个示例中,我们定义了一个二维变换模型,包括旋转、平移和缩放,然后定义了一个残差函数,将实际值与预测值之间的差异表示为非负数。然后我们将残差函数添加到了优化问题中,并设置了初始值和优化参数。最后,我们运行了求解器,并输出了求解结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值