首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV如何利用Eigen?

OpenCV如何利用Eigen?
EN

Stack Overflow用户
提问于 2014-02-07 07:04:10
回答 2查看 17K关注 0票数 20

在从源代码编译OpenCV时,有一个CMake选项WITH_EIGEN,它显示“包含Eigen3支持”。然而,在文档中(或者在google中),我找不到这到底是做什么以及如何使用它的。我可以想象几种选择:

我是否可以继续使用cv::Mat和某些函数(哪些函数?)例如cv::Mat::inv()将开始使用来自Eigen的算法?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-05 20:21:22

在使用它一段时间后,我可以给出答案:

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*,...)将数据映射回。不过,在使用行/列主要标志之类的东西时要小心。

票数 22
EN

Stack Overflow用户

发布于 2014-02-07 13:43:19

以下是我的Eigen+OpenCV互操作性示例,希望对您有所帮助:

代码语言:javascript
复制
//
#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;
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21616015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档