繁体   English   中英

将 cv::Mat 转换为 Eigen::Matrix 会导致来自 opencv2/core/eigen.hpp 文件的编译错误(OpenCV + Eigen)

[英]Converting cv::Mat to Eigen::Matrix gives compilation error from opencv2/core/eigen.hpp file (OpenCV + Eigen)

在我的代码中,我必须将cv::Mat转换为Eigen::Matrix以便我可以使用Eigen库的一些功能。 我相信一个简单的选择是将cv::Mat的每个元素手动复制+粘贴到Eigen::Matrix中。 但是,我在 SO 上偶然发现了这个问题,并发现了cv::cv2eigen function。 它位于opencv2/core/eigen.hpp文件中。

这是示例代码:

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/core/eigen.hpp>
#include <Eigen/Core>

int main(int argc, char const *argv[]) {
    cv::Mat cv_mat = cv::Mat::eye(3, 3, CV_64FC1);
    std::cout << cv_mat << std::endl;
    
    Eigen::Matrix3d eigen_mat;
    cv::cv2eigen(cv_mat, eigen_mat);
    std::cout << eigen_mat << std::endl;
    
    return 0;
}

然而,奇怪的是,在尝试构建时,我从opencv2/core/eigen.hpp文件中得到了太多编译错误:


/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:63:22: error: ‘Eigen’ does not name a type; did you mean ‘eigen’?
 void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, OutputArray dst )
                      ^~~~~
                      eigen
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:63:35: error: expected unqualified-id before ‘<’ token
 void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, OutputArray dst )
                                   ^
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:63:35: error: expected ‘)’ before ‘<’ token
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:63:35: error: expected initializer before ‘<’ token
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:81:22: error: ‘Eigen’ does not name a type; did you mean ‘eigen’?
 void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src,
                      ^~~~~
                      eigen
.
.
.
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:234:6: error: redefinition of ‘template<class _Tp> void cv::cv2eigen(const cv::Mat&, int)’
 void cv2eigen( const Mat& src,
      ^~~~~~~~
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:141:6: note: ‘template<class _Tp> void cv::cv2eigen(const cv::Mat&, int)’ previously declared here
 void cv2eigen( const Mat& src,
      ^~~~~~~~
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:259:16: error: ‘Eigen’ has not been declared
                Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
                ^~~~~
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:259:29: error: expected ‘,’ or ‘...’ before ‘<’ token
                Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
                             ^
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp: In function ‘void cv::cv2eigen(const cv::Matx<_Tp, 1, _cols>&, int)’:
.
.
.
/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp:262:23: error: ‘Eigen’ has not been declared
     if( !(dst.Flags & Eigen::RowMajorBit) )
                       ^~~~~

这很奇怪,对吧? 因为我遇到了OpenCV函数的错误,我什至没有接触过。

那么,我该如何解决这个奇怪的问题呢?

附言

  1. 我正在使用OpenCV 4.2.0
  2. 我用<string>修改了一些显式路径

实际上,我找到了根本原因:

根据4.x分支上的opencv/modules/core/include/opencv2/core/eigen.hpp 第 78 行和第 79 行

@note 使用这些函数需要在此 header 之前包含Eigen/Dense或类似的 header。

因此,令人惊讶的是,我所要做的就是将#include顺序从

#include <opencv2/core.hpp>
#include <opencv2/core/eigen.hpp>
#include <Eigen/Core>

#include <opencv2/core.hpp>
#include <Eigen/Core>
#include <opencv2/core/eigen.hpp>

另一方面,我在我的机器上检查了现有的/usr/local/<blah_blah>/include/opencv4/opencv2/core/eigen.hpp文件( OpenCV 4.2.0 ),遗憾的是,它没有说明#include订单!

¯\_(ツ)_/¯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM