OpenCV CV::Mat and Eigen::Matrix

后端 未结 5 1482
孤街浪徒
孤街浪徒 2020-11-29 00:55

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;
         


        
5条回答
  •  醉酒成梦
    2020-11-29 01:22

    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;
    

提交回复
热议问题