3
votes

I would like to convert my OpenCV Mat to a Matlab .mat file, which can be read easily by 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.

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.

Another possibility is to store it as csv file and read it then via matlab, as mentioned here: OpenCV -> CSV

Is there any other library or function available, which converts the data directly to an Matlab mat?

2

2 Answers

3
votes

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:

#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

And in Matlab use the following function to read the csv file:

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

I wrote the following C code for a project some time ago. It;s a little dated but it worked with 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.

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 );
}