How does OpenCV make use of Eigen?

Ela782 picture Ela782 · Feb 7, 2014 · Viewed 15.6k times · Source

When compiling OpenCV from source, there's the CMake option WITH_EIGEN, which says "Include Eigen3 support". However, nowhere in the documentation (or with google, for that matter) I can find out what exactly this does and how to use it. I can imagine a few options:

Can I just continue using cv::Mat, and certain functions (which ones?) like cv::Mat::inv() will start using the algorithms from Eigen?

Or does the WITH_EIGEN flag basically do nothing and I need to convert the cv::Mat's to Eigen (or use Eigen::Map) and then use Eigen's algorithms manually?

Answer

Ela782 picture Ela782 · Mar 5, 2014

After working with it for a bit, I can provide the answer:

The WITH_EIGEN flag does nothing except making the eigen-opencv interopability functions available.

Can I just continue using cv::Mat, and certain functions (which ones?) like cv::Mat::inv() will start using the algorithms from Eigen?

No, cv::Mat::inv() has no smart logic and will use the OpenCV algorithms.

Or does the WITH_EIGEN flag basically do nothing and I need to convert the cv::Mat's to Eigen (or use Eigen::Map) and then use Eigen's algorithms manually?

Exactly, that's the way to go. I wouldn't necessarily recommend to use cv2eigen() and eigen2cv() though. I used Eigen::Map to just map the memory (no cost in copying anything) and cv::Mat(void*, ...) to map the data back. Be careful though with row/col-major flags and stuff.