1

I am trying to convert a cv::Mat to Eigen::Mat with cv2eigen and back with eigen2cv (I understand that one should preferably use Eigen::Map for that, but that is a different story).

I am using OpenCV 2.4.9 and eigen3 3.0.5 in conjunction with Eclipse 3.7.2 on ubuntu 12.04 LTS

However, even with the simple example

#include <Eigen/Dense>
#include <opencv2/core/eigen.hpp>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;
using namespace Eigen;

int main() {

    cv::Mat_<float> a = Mat_<float>::ones(2,2);
    Eigen::Matrix<float,Dynamic,Dynamic> b;
    cv2eigen(a,b);

    return 0;
}

(main body taken from cv::Mat conversion to Eigen-Matrix and back) I get the following compile error:

Invalid arguments ' Candidates are: void cv2eigen(const cv::Matx<#0,1,#1> &, ? &) void cv2eigen(const cv::Matx<#0,#1,#2> &, ? &) void cv2eigen(const cv::Matx<#0,#1,1> &, ? &) void cv2eigen(const cv::Mat &, ? &) void cv2eigen(const cv::Matx<#0,#1,#2> &, ? &) void cv2eigen(const cv::Mat &, ? &) ' simpleExample.cpp /simpleExample_proj-Debug@build/[Source directory] line 17 Semantic Error

I took a peek into opencv2/core/eigen.hpp and suppose that the method I want to call is the following one:

    template<typename _Tp>
    void cv2eigen( const Mat& src,
                   Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
    {
        ...
    }

Looks like something is wrong with the parameters' types, i.e. the matrices but I cannot figure out what - the same code seems to work for other people. Any help/ hint is greatly appreciated!

P.S.: If I forgot to post any crucial detail, my apologies, just tell me so and I will fix that.

Edit Thought I might mention it: I am using the stock gcc version 4.6.3 that comes with ubuntu.

Community
  • 1
  • 1
oriol
  • 13
  • 1
  • 5

1 Answers1

1

This works for me:

#define EIGEN_RUNTIME_NO_MALLOC // Define this symbol to enable runtime tests for allocations
...
Mat src(N_rows,N_cols,CV_64FC1);    
MatrixXd X=MatrixXd(src.rows,src.cols);
    //  openCV -> Eigen 
    cv2eigen(src,X);
...

For float you should use MatrixXf and CV_32FC1 matrix types.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • Sorry, did not work for me, same error appears. I've tried with different number formats, resulting error is always the same. For now, I just use Ela782's solution (s. http://stackoverflow.com/questions/14783329/opencv-cvmat-and-eigenmatrix) as a replacement for cv2eigen and eigen2cv. – oriol Jun 09 '14 at 07:04