Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Import Matlab Matrix to OpenCv

Hi all,

I'm new to C++ and OpenCv and I really appreciate your help. I'm trying to convert my Matlab codes to c++ using openCV. To verify my code I want to import my Matlab matices so I use this Matlab function to convert them to yml file:

unction matlab2opencv( variable, fileName, flag)

[rows, cols] = size(variable);

% Beware of Matlab's linear indexing variable = variable';

% Write mode as default if ( ~exist('flag','var') ) flag = 'w'; end

if ( ~exist(fileName,'file') || flag == 'w' ) % New file or write mode specified file = fopen( fileName, 'w'); fprintf( file, '%%YAML:1.0\n'); else % Append mode file = fopen( fileName, 'a'); end

% Write variable header fprintf( file, ' %s: !!opencv-matrix\n', inputname(1)); fprintf( file, ' rows: %d\n', rows); fprintf( file, ' cols: %d\n', cols); fprintf( file, ' dt: f\n'); fprintf( file, ' data: [ ');

% Write variable data for i=1:rowscols fprintf( file, '%.6f', variable(i)); if (i == rowscols), break, end fprintf( file, ', '); if mod(i+1,4) == 0 fprintf( file, '\n '); end end

fprintf( file, ']\n');

fclose(file);

Then using the following command I write to matrices in the yml function: varA = rand( 3, 6); varB = rand( 7, 2);

matlab2opencv( varA, 'newStorageFile.yml'); matlab2opencv( varB, 'newStorageFile.yml', 'a'); % append mode passed by 'a' flag

Then I use this c++ code to see the matrices in visual studio:

include <opencv2\opencv.hpp>

include <iostream>

include <string>

include <opencv2\highgui\highgui.hpp>

using namespace cv; using namespace std;

int main(int argc, char * const argv[]) { Mat var1; Mat var2; int ex;

string demoFile = "newStorageFile.yml";

FileStorage fsDemo(demoFile, FileStorage::READ);
fsDemo["varA"] >> var1;
fsDemo["varB"] >> var2;

cout << "Print the contents of var1:" << endl;
cout << var1 << endl << endl;

cout << "Print the contents of var2:" << endl;
cout << var2 << endl;
cin >> ex;

fsDemo.release();
return 0;

}

but the output are empty matrices. Does anyone have any idea why?

click to hide/show revision 2
No.2 Revision

Import Matlab Matrix to OpenCv

Hi all,

I'm new to C++ and OpenCv and I really appreciate your help. I'm trying to convert my Matlab codes to c++ using openCV. To verify my code I want to import my Matlab matices so I use this Matlab function to convert them to yml file:

unction matlab2opencv( variable, fileName, flag)

[rows, cols] = size(variable);

% Beware of Matlab's linear indexing variable = variable';

% Write mode as default if ( ~exist('flag','var') ) flag = 'w'; end

if ( ~exist(fileName,'file') || flag == 'w' ) % New file or write mode specified file = fopen( fileName, 'w'); fprintf( file, '%%YAML:1.0\n'); else % Append mode file = fopen( fileName, 'a'); end

% Write variable header fprintf( file, ' %s: !!opencv-matrix\n', inputname(1)); fprintf( file, ' rows: %d\n', rows); fprintf( file, ' cols: %d\n', cols); fprintf( file, ' dt: f\n'); fprintf( file, ' data: [ ');

% Write variable data for i=1:rowscols fprintf( file, '%.6f', variable(i)); if (i == rowscols), break, end fprintf( file, ', '); if mod(i+1,4) == 0 fprintf( file, '\n '); end end

fprintf( file, ']\n');

fclose(file);

Then using the following command I write to matrices in the yml function: varA = rand( 3, 6); varB = rand( 7, 2);

matlab2opencv( varA, 'newStorageFile.yml'); matlab2opencv( varB, 'newStorageFile.yml', 'a'); % append mode passed by 'a' flag

Then I use this c++ code to see the matrices in visual studio:

include <opencv2\opencv.hpp>

include <iostream>

include <string>

include <opencv2\highgui\highgui.hpp>

    #include <opencv2\opencv.hpp>
    #include <iostream>
    #include <string>
    #include <opencv2\highgui\highgui.hpp>
    using namespace cv;
 using namespace std;

std; int main(int argc, char * const argv[]) { Mat var1; Mat var2; int ex;

ex;
string demoFile = "newStorageFile.yml";
 FileStorage fsDemo(demoFile, FileStorage::READ);
 fsDemo["varA"] >> var1;
 fsDemo["varB"] >> var2;
 cout << "Print the contents of var1:" << endl;
 cout << var1 << endl << endl;
 cout << "Print the contents of var2:" << endl;
 cout << var2 << endl;
 cin >> ex;
 fsDemo.release();
 return 0;
}

}

but the output are empty matrices. Does anyone have any idea why?