我有一个连续的OpenCV(非对称)矩阵cv::Mat m
,我想通过Eigen::EigenSolver
计算其特征向量和特征值。
由于m
可能很大,因此通过cv2eigen
函数制作副本效率低下。然后,我想使用Eigen::Map
.这是我的代码:
Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> mMapped (m.ptr<float>(), m.rows, m.cols);
Eigen::EigenSolver<Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>> solver(mMapped,true);//this gives a bunch of errors
最后一行给出了一堆错误(见问题末尾)。我该如何解决这个问题?请注意,我应该避免将mMapped
复制到矩阵对象,否则等效于使用cv2eigen
(我认为)。
/usr/local/include/eigen3/Eigen/src/Eigenvalues/EigenSolver.h: In instantiation of ‘class Eigen::EigenSolver<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >’:
../Math.hpp:64:104: required from ‘static void Math::createHashTable(const cv::Mat&, cv::Mat&, cv::Mat&, int, int) [with T = float]’
../CloudCache.cpp:155:58: required from here
/usr/local/include/eigen3/Eigen/src/Eigenvalues/EigenSolver.h:71:10: error: ‘Options’ is not a member of ‘Eigen::EigenSolver<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >::MatrixType {aka Eigen::Map<Eigen::Matrix<float, -1, -1, 1> >}’
enum {
^
In file included from /usr/local/include/eigen3/Eigen/Eigenvalues:29:0,
from ../Math.hpp:16,
from ../CloudCache.cpp:15:
/usr/local/include/eigen3/Eigen/src/Eigenvalues/RealSchur.h: In instantiation of ‘class Eigen::RealSchur<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >’:
/usr/local/include/eigen3/Eigen/src/Eigenvalues/EigenSolver.h:312:27: required from ‘class Eigen::EigenSolver<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >’
../Math.hpp:64:104: required from ‘static void Math::createHashTable(const cv::Mat&, cv::Mat&, cv::Mat&, int, int) [with T = float]’
../CloudCache.cpp:155:58: required from here
/usr/local/include/eigen3/Eigen/src/Eigenvalues/RealSchur.h:58:10: error: ‘Options’ is not a member of ‘Eigen::RealSchur<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >::MatrixType {aka Eigen::Map<Eigen::Matrix<float, -1, -1, 1> >}’
enum {
^
In file included from /usr/local/include/eigen3/Eigen/src/Eigenvalues/RealSchur.h:14:0,
from /usr/local/include/eigen3/Eigen/Eigenvalues:29,
from ../Math.hpp:16,
from ../CloudCache.cpp:15:
/usr/local/include/eigen3/Eigen/src/Eigenvalues/HessenbergDecomposition.h: In instantiation of ‘class Eigen::HessenbergDecomposition<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >’:
/usr/local/include/eigen3/Eigen/src/Eigenvalues/RealSchur.h:228:41: required from ‘class Eigen::RealSchur<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >’
/usr/local/include/eigen3/Eigen/src/Eigenvalues/EigenSolver.h:312:27: required from ‘class Eigen::EigenSolver<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >’
../Math.hpp:64:104: required from ‘static void Math::createHashTable(const cv::Mat&, cv::Mat&, cv::Mat&, int, int) [with T = float]’
../CloudCache.cpp:155:58: required from here
/usr/local/include/eigen3/Eigen/src/Eigenvalues/HessenbergDecomposition.h:64:10: error: ‘Options’ is not a member of ‘Eigen::HessenbergDecomposition<Eigen::Map<Eigen::Matrix<float, -1, -1, 1> > >::MatrixType {aka Eigen::Map<Eigen::Matrix<float, -1, -1, 1> >}’
enum {
^
subdir.mk:30: recipe for target 'CloudCache.o' failed
make: *** [CloudCache.o] Error 1
你不能用Map
声明一个EigenSolver
,矩阵类型必须是Matrix
,所以在你的情况下:
typedef Matrix<float,Dynamic,Dynamic,RowMajor> RowMatrixXf;
Map<RowMatrixXf> mMapped (m.ptr<float>(), m.rows, m.cols);
EigenSolver<MatrixXf> eig(mMapped);
您也可以使用 RowMatrixXf
实例化EigenSolver
,但尽管进行了转置,但性能应该较低。