简体   繁体   English

OpenCV Mat - > Matlab Mat文件

[英]OpenCV Mat -> Matlab Mat File

I would like to convert my OpenCV Mat to a Matlab .mat file, which can be read easily by Matlab. 我想将我的OpenCV Mat转换为Matlab .mat文件,Matlab可以轻松读取。 I don't want to use a Mex function to directly provide the data to matlab, since I want to save the data on the hard disk. 我不想使用Mex函数直接向matlab提供数据,因为我想将数据保存在硬盘上。

There is the cvmatio cpp function available: cvmatio , but as I have seen there is only a function to read a matlab Mat with OpenCV, but no function to create a matlab Mat out of openCV. 有cvmatio cpp函数可用: cvmatio ,但正如我所看到的,只有一个函数可以用OpenCV读取matlab Mat,但没有函数用openCV创建matlab Mat。

Another possibility is to store it as csv file and read it then via matlab, as mentioned here: OpenCV -> CSV 另一种可能性是将其存储为csv文件并通过matlab读取,如下所述: OpenCV - > CSV

Is there any other library or function available, which converts the data directly to an Matlab mat? 是否有其他库或函数可用,它们将数据直接转换为Matlab mat?

I have found this as the best answer for the question, since I haven't found a direct way without the use of a temporary file. 我发现这是问题的最佳答案,因为我没有找到一个没有使用临时文件的直接方法。

Write the OpenCV Mat to a CSV File: 将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 写入CSV的来源

And in Matlab use the following function to read the csv file: 在Matlab中使用以下函数来读取csv文件:

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

I wrote the following C code for a project some time ago. 我不久前为一个项目编写了以下C代码。 It;sa little dated but it worked with matlab 2011a. 它;有点过时,但它与matlab 2011a一起工作。 It should serve as an example if nothing else. 它应该作为一个例子,如果没有别的。 It does make a number of assumptions - mostly documented. 它确实做了许多假设 - 大多数记录在案。

Input parameters are the filename to write to, an array of pointers to CvMat, an array of names for these matrices and the number of matrices to write. 输入参数是要写入的文件名,指向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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM