How does OpenCV make use of Eigen?
Asked Answered
T

2

22

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?

Thionate answered 6/2, 2014 at 23:4 Comment(0)
T
24

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.

Thionate answered 5/3, 2014 at 12:21 Comment(1)
For proper mapping between Eigen and cv::Mat, without copying data, see my answer here: #14783829Thionate
C
5

Here is my Eigen+OpenCV interoperability example, hope it'll be useful:

//
#define EIGEN_RUNTIME_NO_MALLOC // Define this symbol to enable runtime tests for allocations
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <vector>
#include <Eigen/IterativeLinearSolvers>
#include <iostream>
#include "opencv2/core/eigen.hpp"
#include "opencv2/opencv.hpp"
using namespace Eigen;
using namespace cv;
using namespace std;

void EnergyFilter(Mat& src,Mat& dst,double alpha)
{
int n_pixels=src.rows*src.cols;
// Image to row-vector
Mat m=src.reshape(1,n_pixels).clone();
// To double
m.convertTo(m,CV_64FC1);

// Eigen vectors
VectorXd I(n_pixels);
VectorXd u(n_pixels);

// convert image from openCV to Eigen 
cv2eigen(m,I);

// 
SparseMatrix<double> A(n_pixels,n_pixels);

// Fill sparse martix using triplets
typedef Eigen::Triplet<double> T;
std::vector<T> tripletList;

// Filter parameter (smoothing factor)
//double alpha=-0.1;

// Set values
for(int i=0;i<n_pixels;i++)
{
    tripletList.push_back(T(i,i,1+4*alpha));
    if((i+1) < n_pixels){tripletList.push_back(T(i,i+1,-alpha));} // +1
    if((i-1) >= 0){tripletList.push_back(T(i,i-1,-alpha));} // -1
    if((i+src.cols) < n_pixels){tripletList.push_back(T(i,i+src.cols,-alpha));} // +3
    if((i-src.cols) >= 0){tripletList.push_back(T(i,i-src.cols,-alpha));} // -3
}

// Boundary values of main diag
tripletList.push_back(T(0,0,1+2*alpha));
for(int i=1;i<src.cols;i++)
{
    tripletList.push_back(T(i,i,1+3*alpha));
}

// 
tripletList.push_back(T(n_pixels-1,n_pixels-1,1+2*alpha));
for(int i=1;i<src.cols;i++)
{
    tripletList.push_back(T(i,n_pixels-i-1,1+3*alpha));
}

// Init sparse matrix
A.setFromTriplets(tripletList.begin(),tripletList.end());

tripletList.clear();
// Solver init
ConjugateGradient<SparseMatrix<double> > cg;
cg.compute(A);
// Solve linear systyem
u = cg.solve(I);
std::cout << "#iterations:     " << cg.iterations() << std::endl;
std::cout << "estimated error: " << cg.error()      << std::endl;
// Get the solution
dst=Mat(n_pixels,1,CV_64FC1);
eigen2cv(u,dst);
dst=dst.reshape(1,src.rows);
dst.convertTo(dst,CV_8UC1);
}


int main(int argc, char* argv[])
{
    namedWindow("image");
    namedWindow("result");
    Mat img=imread("d:\\ImagesForTest\\lena.jpg",1);
    imshow("image",img);
    waitKey(10);
    Mat res;
    vector<Mat> ch;
    cv::split(img,ch);

    for(int i=0;i<3;i++)
    {
        EnergyFilter(ch[i],res,3);
        res.copyTo(ch[i]);
    }

    cv::merge(ch,res);
    // show the resilt
    imshow("result",res);
    waitKey(0);
    return 0;
}
Chiquitachirico answered 7/2, 2014 at 5:43 Comment(1)
Yes, it is useful, thank you for the example. However, I was aware of examples of this kind, and it doesn't answer my question: How does OCV make use of Eigen. I guess the answer is: It doesn't! Compiling with WITH_EIGEN just means you will have access to the interop-functions and the Eigen headers. Unless someone knows more than I do.Thionate

© 2022 - 2024 — McMap. All rights reserved.