OpenCV如何利用Eigen?

22
在从源代码编译OpenCV时,有一个名为WITH_EIGEN的CMake选项,其表示“包括Eigen3支持”。然而,在文档中(或通过Google)我找不到确切的信息,也不知道如何使用它。我可以想象几个选项:
- 我是否可以继续使用cv :: Mat,并且某些函数(哪些函数?)像cv :: Mat :: inv()将开始使用Eigen的算法? - 或者WITH_EIGEN标志基本上什么都不做,我需要将cv :: Mat转换为Eigen(或使用Eigen :: Map),然后手动使用Eigen的算法?
2个回答

24

经过一段时间的使用,我可以回答这个问题:

WITH_EIGEN标志除了使eigen-opencv互操作函数可用外,没有任何作用。

我能继续使用cv::Mat,并且某些函数(哪些函数?)像cv::Mat::inv()将开始使用Eigen的算法吗?

不行,cv::Mat::inv()没有智能逻辑,将使用OpenCV算法。

或者WITH_EIGEN标志基本上什么也没做,我需要转换cv::Mat到Eigen(或使用Eigen::Map),然后手动使用Eigen的算法吗?

确切地说,这就是正确的方法。 我不一定推荐使用cv2eigen()和eigen2cv()。我只使用Eigen::Map来映射内存(不需要复制任何内容),并使用cv::Mat(void*, ...)将数据映射回来。但要小心行/列主标志等。


1
为了在Eigen和cv::Mat之间进行适当的映射,而不需要复制数据,请参见我的答案:https://dev59.com/lmUq5IYBdhLWcg3wHcv5#21706778 - Ela782

5
这是我的Eigen+OpenCV互操作示例,希望对你有用:
//
#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;
}

3
是的,非常有用,谢谢你提供这个例子。不过,我已经知道了这种类型的例子,它并没有回答我的问题:OCV如何利用Eigen?我猜想答案是:它并没有!使用WITH_EIGEN编译只是意味着您将可以访问互操作函数和Eigen头文件。除非有人比我更了解,否则就是这样。 - Ela782

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接