Is there a reversible way to convert an OpenCV cv::Mat object to an Eigen::Matrix?
e.g., Some way of doing:
cv::Mat cvMat;
Pierluigi's version has not worked for me completely for 3 channel images! After some investigation I ended with the following solution which has worked for me:
using namespace Eigen;
constexpr uint32_t height = 3;
constexpr uint32_t width = 7;
cv::Mat img(height, width, CV_32FC3, cv::Scalar(1.0f, 2.0f, 3.0f));
using MatrixXfRowMajor = Matrix;
using C3Stride = Stride;
C3Stride c3Stride(width *3,3);
using cvMap = Map;
cvMap imgC1(reinterpret_cast(img.data) + 0, img.rows, img.cols, c3Stride);
cvMap imgC2(reinterpret_cast(img.data) + 1, img.rows, img.cols, c3Stride);
cvMap imgC3(reinterpret_cast(img.data) + 2, img.rows, img.cols, c3Stride);
std::cout << imgC1 << std::endl << std::endl;
std::cout << imgC2 << std::endl << std::endl;
std::cout << imgC3 << std::endl << std::endl;