This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Conversion between OpenCV and Eigen

Tags: None
(comma "," separated)
pwtr
Registered Member
Posts
2
Karma
0

Conversion between OpenCV and Eigen

Fri Oct 28, 2011 2:13 pm
Hi,

I'd like to use Eigen for solving the eigenvalue problem of a general matrix. I'll need this for a project using OpenCV (since the OpenCV solver only solves symmetric matrices), so I have to convert my data between both. Are there any best practices how to do so for dynamic sized matrices?

I don't have much experience with Eigen (so sorry if I am totally off here), but the first thing, that came to my mind is simply mapping the data of OpenCV's cv::Mat to a MatrixXd, like this:

Code: Select all
MatrixXd m = Eigen::Map<MatrixXd>(reinterpret_cast<double*>(a.data),a.rows,a.cols);


Which works fine for now, but I am afraid this is totally not best practice. Can anyone point me into the right direction or is it OK to do so?

Best regards, Philipp.
pwtr
Registered Member
Posts
2
Karma
0
Sorry. My wrong. OpenCV already comes with a conversion defined in:
Code: Select all
#include <opencv2/core/eigen.hpp>

which makes it as easy as:
Code: Select all
cv::Mat_<float> a = Mat_<float>::ones(2,2);
Eigen::Matrix<float,Dynamic,Dynamic> b;
cv2eigen(a,b);

and is obviously a bit less error-prone than my attempt.

If someone is coming here from Google... I've pushed a simple example on how to do a Linear Discriminant Analysis with OpenCV and Eigen into my github repository at https://github.com/bytefish/opencv/tree/master/lda.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]