Converting a .mat file from MATLAB into cv::Mat matrix in OpenCV

前端 未结 3 2009
我寻月下人不归
我寻月下人不归 2020-12-29 15:34

I have some MATLAB code that I want to migrate to OpenCV. The data that the MATLAB code uses is stored in a .mat file which is then loaded at run time.

I converted

3条回答
  •  [愿得一人]
    2020-12-29 16:00

    You can use the Matlab bridge from opencv contrib. All you need from Opencv Contrib is to copy contrib/modules/matlab/include/opencv2/matlab folder into the include/opencv2 folder.

    along with the Matlab Compiler Runtime (just libmx.lib, libmex.lib and libmat.lib).

    MATFile *pmat = matOpen(filename, "r");
    if (pmat == NULL)
    {
         cerr << "Error opening file " << filename << endl;
    }
    else
    {
         int numVars;
         char** namePtr = matGetDir(pmat, &numVars);
         cout << filename << " contains vars " << endl;
         for (int idx = 0; idx < numVars; idx++)
         {
               std::cout << "                     " << namePtr[idx] << " ";
               mxArray* m = matGetVariable(pmat, namePtr[idx]);
               matlab::MxArray mArray(m);
               cv::bridge::Bridge bridge(mArray);
               cv::Mat mat = bridge.toMat();
               //  do something with opencv Mat 
         }
    }
    

提交回复
热议问题