Re: [eigen] Eigen::Matrix and cv::Mat conversions
• To: eigen@xxxxxxxxxxxxxxxxxxx
• Subject: Re: [eigen] Eigen::Matrix and cv::Mat conversions
• From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
• Date: Fri, 20 Aug 2010 12:52:44 -0400
• Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=rpzBh15IUTYEl6p7NYGQXhwHddIY0n1IZAYubE5jY/ougy7oOxYxkslRu7VCY3ePBA guK48FsjJFw6snYDR1OLWiRPmNBku9Z6eOmLozIjlSkr0WfHBOkG3UekDwK5bTI15/wY Luoh3Y6Ltswb4lcLdPqQTKfihoQK3Y8PMnO4Y=

2010/8/20
I want to store the vertices of a convex polygon in an Eigen::Matrix<double, 7,2>, and then find the area of the polygon using the method of determinants. In other words, I need to make a series of mat.block<2,2>(n,0).det() calls. However, for this to work, the vertices must be in order (clockwise or ccw doesn't matter). So I figured I'd use a convex hull algorithm to reorder them.

Well, I already have a dependency on OpenCV, so I thought I'd use their convexHull() method. However, to do this I must either reinterpret or copy the contents of the Eigen::Matrix into a cv::Mat_<cv::Vec2f>(7,1). The internal format of this type is straightforward----I can just write floats in rowmajor order using std::transform() and it will be fine.

The tricky part is figuring how what is the best way to get at that data in the Eigen::Matrix. Curiously, upon examining the internals from data() in the debugger, the data appears to be in column-major order even though I didn't specify that (is it the default for some reason?). I know I can use the rowwise() method, but it isn't obvious to me if it's possible to get any kind of iterator from that, or if there's some other way to do the copy cleanly.

Eigen defaults to column-major.
If you want row-major with Eigen you can do:
typedef Matrix<float,2,2,RowMajor> MyRowMajorMatrixType;

or you can even do:
#define EIGEN_DEFAULT_TO_ROW_MAJOR