OpenCV Mat & # 8594; Matlab Mat File

I would like to convert my OpenCV Mat to a Matlab.mat file that Matlab can read easily. I don't want to use the Mex function to provide data to matlab directly, as I want to save the data to the hard drive.

There is a function cvmatio cpp: cvmatio available , but as I already saw there is only a function to read Matlab Mat with OpenCV, but no function to generate Matlab Mat from openCV.

Another possibility is to save it as a csv file and read it then through matlab as mentioned here: OpenCV -> CSV

Is there any other library or function that converts data directly to Matlab?


source to share

2 answers

I found this as the best answer to the question, since I couldn't find a direct path without using a temporary file.

Write Matrix OpenCV to CSV file:

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


Source for CSV recording

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

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




I wrote the following C code for a project a while ago. It's a bit outdated, but it worked with Matlab 2011a. This should serve as an example if nothing else. He makes a number of assumptions - mostly documented ones.

The input parameters are the name of the file to write, 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 );




All Articles