I want to load an Image in OpenCV 4.5.3 and transfer that matrix over to Eigen 3.4 to compare methods of those two modules.
For example picture a code like this:
#include <Eigen/Dense>
#include <opencv2/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/eigen.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
int main() {
std::clog << "loading image" << std::endl;
cv::Mat image_matrix = cv::imread("testimage.png");
if (image_matrix.empty()) {
std::clog << "can't load image" << std::endl;
return -1;
}
cv::Size size = image_matrix.size();
int type = image_matrix.type();
std::clog << "creating opencv matrix" << std::endl;
cv::Mat cv_matrix(size, type);
cv_matrix = image_matrix.clone();
std::clog << "creating eigen matrix" << std::endl;
Eigen::Matrix<uchar, Eigen::Dynamic, Eigen::Dynamic> eigen_matrix;
eigen_matrix.resize(size.height, size.width);
std::clog << "writing eigen matrix" << std::endl;
cv::cv2eigen(image_matrix, eigen_matrix);
std::clog << "done" << std::endl;
return 0;
}
That code gives the following output:
loading image
creating opencv matrix
creating eigen matrix
writing eigen matrix
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.3) /home/nummer42o/Downloads/opencv/modules/core/src/copy.cpp:320: error: (-215:Assertion failed) channels() == CV_MAT_CN(dtype) in function 'copyTo'
Aborted (core dumped)
My problem is that I can't seem to make sense of it. Any ideas what could cause this? I already checked and the type of image_matrix is 16, so it's an CV_8UC3 meaning an 3-channel uchar (at least when fererring to this).