【问题标题】:OpenCV Mat -> Matlab Mat FileOpenCV Mat -> Matlab Mat 文件
【发布时间】:2015-07-21 05:51:42
【问题描述】:

我想将我的 OpenCV Mat 转换为 Matlab .mat 文件,Matlab 可以轻松读取该文件。我不想使用Mex函数直接将数据提供给matlab,因为我想将数据保存在硬盘上。

有可用的 cvmatio cpp 函数:cvmatio,但正如我所见,只有一个使用 OpenCV 读取 matlab Mat 的函数,但没有从 openCV 创建 matlab Mat 的函数。

另一种可能性是将其存储为 csv 文件,然后通过 matlab 读取,如此处所述:OpenCV -> CSV

是否有任何其他可用的库或函数可以将数据直接转换为 Matlab mat?

【问题讨论】:

    标签: c++ matlab opencv data-conversion mat


    【解决方案1】:

    前段时间,我为一个项目编写了以下 C 代码。它有点过时,但它适用于 matlab 2011a。如果没有别的,它应该作为一个例子。它确实做出了许多假设 - 大部分都记录在案。

    输入参数是要写入的文件名、指向 CvMat 的指针数组、这些矩阵的名称数组以及要写入的矩阵数。

    void writeMatFile( const char *fileName, CvMat ** matrices, const char **names, int numMatrices ) {
        FILE *file = fopen( fileName, "wb" );
    
        for( int i=0; i<numMatrices; i++ ) {
            CvMat * mat = matrices[i];
            const char *name = names[i];
    
            // If mat is ND we write multiple copies with _n suffixes
            int depth = CV_MAT_CN(mat->type);
            for( int d=0; d<depth; d++ ) {
    
                // Assumption that we are always dealing with double precision
                uint32_t MOPT = 0000;
                fwrite(&MOPT, 1, sizeof( uint32_t), file);
                uint32_t    mrows = mat->rows;
                uint32_t    ncols = mat->cols;
                uint32_t    imagef = 0;
    
                char nameBuff[ strlen( name ) + 10];
                strcpy(nameBuff, name);
                if( depth>1) {
                    char suffix[5];
                    sprintf(suffix, "_L%d", d+1);
                    strcat(nameBuff, suffix);
                }
    
                uint32_t    nameLength = strlen( nameBuff ) + 1;
                fwrite( &mrows, 1, sizeof( uint32_t), file);
                fwrite( &ncols, 1, sizeof( uint32_t), file);
                fwrite( &imagef, 1, sizeof( uint32_t), file);
                fwrite( &nameLength, 1, sizeof( uint32_t), file);
                fwrite( nameBuff, nameLength, 1, file );
    
                for( int col = 0; col<ncols; col++ ) {
                    for( int row=0; row<mrows; row++ ) {
                        CvScalar sc = cvGet2D(mat, row, col);
                        fwrite(&(sc.val[d]), 1, sizeof( double ), file);
                    }
                }
            }
        }
        fclose( file );
    }
    

    【讨论】:

      【解决方案2】:

      我发现这是该问题的最佳答案,因为我没有找到不使用临时文件的直接方法。

      将 OpenCV Mat 写入 CSV 文件:

      #include <fstream>
      void MeasureTool::writeCSV(string filename, cv::Mat m) 
      {
         cv::Formatter const * c_formatter(cv::Formatter::get("CSV"));
         ofstream myfile;
         myfile.open(filename.c_str());
         c_formatter->write(myfile, m);
         myfile.close();
      }
      

      Source for write CSV

      而在 Matlab 中使用以下函数读取 csv 文件:

      x1 = csvread('yourFile.csv',0,0);
      

      【讨论】:

        猜你喜欢
        • 2012-07-17
        • 2015-06-11
        • 2015-04-13
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 2014-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多