std::vector<Eigen::Matrix<float, 3, 1> >转换成Eigen::Matrix 类型

文章讲述了在使用cv::eigen2cv函数时遇到的类型不匹配问题,介绍了两种方法将std::vector<Eigen::Vector3f>转换为Eigen::Matrix并最终到cv::Mat的过程,以及如何通过Eigen::Map进行性能优化以减少内存拷贝。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码在调用 cv::eigen2cv 函数时出现了参数列表匹配错误的问题

因为源数据类型和目标数据类型不一致导致。cv::eigen2cv 函数可以将 Eigen::Matrix 类型的数据转换为 cv::Mat 类型,但是源数据类型是 std::vector<Eigen::Matrix<float, 3, 1> >,这是一个向量而不是一个矩阵。需要先将源数据转换为 Eigen::Matrix 类型,然后再调用cv::eigen2cv。

方法一:

// 定义一个 std::vector<Eigen::Vector3f> 类型的变量
std::vector<Eigen::Vector3f> mappointInCurrentFrame;

// 假设这里已经给 mappointInCurrentFrame 赋值了一些数据

// 定义一个 cv::Mat 类型的变量
cv::Mat mappointInCurrentFrame_cv;

// 将 std::vector<Eigen::Vector3f> 类型的数据转换为 Eigen::Matrix<float, 3, Eigen::Dynamic> 类型的数据
Eigen::Matrix<float, 3, Eigen::Dynamic> m;
m.resize(3, mappointInCurrentFrame.size());
for (int i = 0; i < mappointInCurrentFrame.size(); i++)
{
    m.col(i) = mappointInCurrentFrame[i];
}

// 将 Eigen::Matrix<float, 3, Eigen::Dynamic> 类型的数据转换为 cv::Mat 类型的数据
cv::eigen2cv(m, mappointInCurrentFrame_cv);

方法二:

// 定义一个 std::vector<Eigen::Vector3f> 类型的变量
std::vector<Eigen::Vector3f> mappointInCurrentFrame;

// 假设这里已经给 mappointInCurrentFrame 赋值了一些数据

// 定义一个 cv::Mat 类型的变量
cv::Mat mappointInCurrentFrame_cv;

// 将 std::vector<Eigen::Vector3f> 类型的数据映射为 Eigen::Matrix<float, 3, Eigen::Dynamic> 类型的数据
Eigen::Map<Eigen::Matrix<float, 3, Eigen::Dynamic>> m(mappointInCurrentFrame[0].data(), 3, mappointInCurrentFrame.size());

// 将 Eigen::Matrix<float, 3, Eigen::Dynamic> 类型的数据转换为 cv::Mat 类型的数据
cv::eigen2cv(m, mappointInCurrentFrame_cv);

如果想要优化性能,可以使用 Eigen::Map 类来直接将 std::vector<Eigen::Vector3f> 类型的数据映射为 Eigen::Matrix 类型的数据,然后再使用 cv::eigen2cv 函数来转换为 cv::Mat 类型的数据2。这样可以避免不必要的内存拷贝和时间消耗。

/mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp: In function ‘vectorMatrix4f cluster_poses(float, float, const vectorMatrix4f&, const vectorMatrix4f&)’: /mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp:26:38: warning: format ‘%d’ expects argument of type ‘in ’, but argument 2 has type ‘std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type’ {aka ‘long unsigned int’} [-Wformat=] 26 | printf("num original candidates = %d\n",poses_in.size()); | ~^ ~~~~~~~~~~~~~~~ | | | | int std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type {aka long unsigned int} | %ld /mnt/e/wsl/foundationpose-main/mycpp/src/app/pybind_api.cpp:66:42: warning: format ‘%d’ expects argument of type ‘in ’, but argument 2 has type ‘std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type’ {aka ‘long unsigned int’} [-Wformat=] 66 | printf("num of pose after clustering: %d\n",poses_out.size()); | ~^ ~~~~~~~~~~~~~~~~ | | | | int std::vector<Eigen::Matrix<float, 4, 4>, Eigen::aligned_allocator<Eigen::Matrix<float, 4, 4> > >::size_type {aka long unsigned int} | %ld [100%] Linking CXX shared module mycpp.cpython-39-x86_64-linux-gnu.so lto-wrapper: warning: using serial compilation of 2 LTRANS jobs lto-wrapper: note: see the ‘-flto’ option documentation for more information make[2]: Leaving directory &#39;/mnt/e/wsl/foundationpose-main/mycpp/build&#39; [100%] Built target mycpp make[1]: Leaving directory &#39;/mnt/e/wsl/foundationpose-main/mycpp/build&#39; 翻译一下
最新发布
07-27
g++ -o pcd_to_pcd line_pcd.cpp -I/usr/include/pcl-1.12/ -I/usr/include/eigen3/ -L/usr/lib/x86_64-linux-gnu/ -lpcl_common -lpcl_io -lpcl_octree -lpcl_filters -loctomap -loctomath /usr/bin/ld: /tmp/cc5b11xV.o: in function `main&#39;: line_pcd.cpp:(.text+0x1d3): undefined reference to `pcl::RandomSampleConsensus<pcl::PointXYZ>::computeModel(int)&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl21RandomSampleConsensusINS_8PointXYZEEE[_ZTVN3pcl21RandomSampleConsensusINS_8PointXYZEEE]+0x20): undefined reference to `pcl::RandomSampleConsensus<pcl::PointXYZ>::computeModel(int)&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x28): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::computeModelCoefficients(std::vector<int, std::allocator<int> > const&, Eigen::Matrix<float, -1, 1, 0, -1, 1>&) const&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x30): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::optimizeModelCoefficients(std::vector<int, std::allocator<int> > const&, Eigen::Matrix<float, -1, 1, 0, -1, 1> const&, Eigen::Matrix<float, -1, 1, 0, -1, 1>&) const&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x38): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::getDistancesToModel(Eigen::Matrix<float, -1, 1, 0, -1, 1> const&, std::vector<double, std::allocator<double> >&) const&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x40): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::selectWithinDistance(Eigen::Matrix<float, -1, 1, 0, -1, 1> const&, double, std::vector<int, std::allocator<int> >&)&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x48): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::countWithinDistance(Eigen::Matrix<float, -1, 1, 0, -1, 1> const&, double) const&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x50): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::projectPoints(std::vector<int, std::allocator<int> > const&, Eigen::Matrix<float, -1, 1, 0, -1, 1> const&, pcl::PointCloud<pcl::PointXYZ>&, bool) const&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x58): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::doSamplesVerifyModel(std::set<int, std::less<int>, std::allocator<int> > const&, Eigen::Matrix<float, -1, 1, 0, -1, 1> const&, double) const&#39; /usr/bin/ld: /tmp/cc5b11xV.o:(.data.rel.ro._ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE[_ZTVN3pcl24SampleConsensusModelLineINS_8PointXYZEEE]+0x78): undefined reference to `pcl::SampleConsensusModelLine<pcl::PointXYZ>::isSampleGood(std::vector<int, std::allocator<int> > const&) const&#39; collect2: error: ld returned 1 exit status
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值