如何将tif图像从cv :: Mat数据类型转换为Eigen :: MatrixXf

时间:2019-05-14 15:16:50

标签: c++ opencv eigen tiff

由于某种原因,每当我尝试将使用OpenCV导入的图像转换为特征矩阵时,都会出现此错误:

libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.3)

/opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_graphics_opencv/opencv/work/opencv-3.4.3/modules/core/src/matrix_wrap.cpp:1186: 

error: (-215:Assertion failed) !fixedType() || ((Mat*)obj)->type() == mtype in function 'create'

我认为这是因为我错误地将图像导入为错误的数据类型。谁能提供一些关于如何在两个图书馆之间正确转换的见解?请谢谢!

#include <Eigen/Core>
#include <opencv2/opencv.hpp>
#include <opencv2/core/eigen.hpp>

using namespace Eigen;
using namespace cv;

String filename = "phantom.tif";
Mat img = imread(filename);
MatrixXf img_matrix(img.rows, img.cols);
cv2eigen(img, img_matrix);

1 个答案:

答案 0 :(得分:0)

如果有人好奇,我可以通过运行以下命令使其工作:

//Load Dataset.
String filename = "phantom.tif";
Mat img = imread(filename, cv::ImreadModes::IMREAD_GRAYSCALE);
int Nslice = img.rows;
int Nray = img.cols;
Eigen::MatrixXf tiltSeries;
cv::cv2eigen(img, tiltSeries);