Ask Your Question
0

Mat to Eigen, eigen2cv and cv2eigen errors [closed]

asked 2019-03-28 10:31:25 -0600

HYPEREGO gravatar image

updated 2019-03-31 10:37:55 -0600

Dear guys,

I'm trying to solve SVD using the Eigen library, since I'm trying to solve one of the biggest error I've got so far in retrieving the fundamental matrix (here the link)

I'm using OpenCV 3.x and I can't compile OpenCV from sources with Eigen and/or LAPACK support, so I downloaded the Eigen library and putted under usr/local/include. No problem so far, I can see Eigen function and variables, so the linker and compiler are ok with that, I suppose. So, since I'm interesting in SVD and since I mostly use OpenCV for my stuff, I would like to write a function that directly compute the SVD for me. I've seen that there are a lot of possibilities to make cv::Mat working as Eigen::Matrix and vice-versa. What I would like is a function that do that:

int runEigenSVD(cv::InputArray _mat,
    cv::OutputArray _U, cv::OutputArray _S, cv::OutputArray _V,
    unsigned int QRPreconditioner,
    unsigned int computationOptions)

I'm not even able to compile since using different approaches I always get different errors. I obtain the input matrix with cv::Mat inputMat = _mat.getMat();, so suppose mat as my input matrix, always.

First try:

//OpenCV-> Eigen: it works!
 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
            mat_Eigen(inputMat.ptr<double>(), mat.rows, mat.cols);

//Executing Eigen stuff, here SVD
 Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::FullPivHouseholderQRPreconditioner> 
            svd(mat_Eigen, Eigen::ComputeThinU | Eigen::ComputeThinV);

//Eigen -> OpenCV
 int U_row = svd.matrixU().rows();
 int U_cols = svd.matrixU().cols();
 cv::Mat U_OpenCV(U_row, U_cols, CV_64FC1, svd.matrixU().data());

The errors I got are in the last line:

invalid conversion from ‘const void’ to ‘void’ [-fpermissive]

no matching function for call to ‘cv::Mat::Mat(int&, int&, int, const Scalar*)’

Using eigen2cv and cv2eigen

I got always error, using both function, even with simple code like this one:

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

The error I get is:

Invalid arguments ' Candidates are:

void eigen2cv(const ? &, cv::Mat &)

void eigen2cv(const ? &, cv::Matx<#0,int3 #1 0,int3 #2 0> &)'

Any suggestion? The documentation regarding it is like... nothing and it's incredible that nobody else have done it so far!!

EDIT ## I'm able to map from Mat to Eigen, but still find no way to map it back without the use of for-loop

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by HYPEREGO
close date 2020-05-10 09:56:31.480240

Comments

1

the 2nd one does not make sense at all, you're assigning a cv::Mat to a void function.

wouldn't you want something like cv2eigen there ?

berak gravatar imageberak ( 2019-03-28 10:34:57 -0600 )edit

Sorry, the example with eigen2cv was taken from the web, I'll edit the question immediately. Yeah, I would like to use something like cv2eigen because I just need to convert the input matrix from Mat to Eigen Matrix and viceversa with the result! I'm getting the same result as this. If I try the same code it give to me the same error but using the OpenCV documentation it seems to be ok the usage, isn't it?

HYPEREGO gravatar imageHYPEREGO ( 2019-03-28 10:56:11 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2020-05-10 09:55:52 -0600

HYPEREGO gravatar image

updated 2020-05-10 15:23:21 -0600

I'll reply to myself because I've done some very stupid errors. In the second part of the code I wrote:

 cv::Mat U_OpenCV(U_row, U_cols, CV_64FC1, svd.matrixU().data());

while the correct code should be:

 cv::Mat U_OpenCV = cv::Mat(U_row, U_cols, CV_64FC1, svd.matrixU().data());

So the error was as @berak underlined in the first part, sorry I didn't get it at first sight.

While in the second, (I checked better the error of the compiler) since I called cv2eigen I should provide more template arguments. At the end I didn't used this method, I just used Eigen and then compared the values of both one by one and I solved my problem, without the need of the function. For small matrix using a loop can do the trick, is not a big problem afterall move 9 variables (in my case).

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-03-28 10:31:25 -0600

Seen: 5,336 times

Last updated: May 10 '20