Ask Your Question
1

Import Matlab Matrix to OpenCv

asked 2015-03-23 03:29:40 -0600

Zar gravatar image

updated 2015-03-23 09:22:43 -0600

wuling gravatar image

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-03-23 10:39:00 -0600

dinhtuyen gravatar image

I have writen a function in matlab to write a matrix to a file .yml. after in opencv i read this file!!! Matlab function:

function TH0001_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:rows*cols
    fprintf( file, '%.6f', variable(i));
    if (i == rows*cols), break, end
    fprintf( file, ', ');
    if mod(i+1,4) == 0
        fprintf( file, '\n            ');
    end
end

fprintf( file, ']\n');
fclose(file);

%==============================================================

% call this function in matlab

TH0001_Matlab2OpenCV( img11, 'img01.yml', 'img01');

//======================================================= //Function read file .yml in opencv

cv::Mat LoadDatafromymlfile(char* ymlfilename, char* varriablestring)
{
    cv::Mat temp;
    cv::FileStorage fs(ymlfilename, cv::FileStorage::READ);
    fs[varriablestring] >> temp;
    fs.release();
    return temp;
}

// Call this function

char *filenameyml = "D://img01.yml"; //location of file .yml
cv::Mat img11 = LoadDatafromymlfile(filenameyml, "img11");
edit flag offensive delete link more

Comments

Thanks a lot

Zar gravatar imageZar ( 2015-03-23 19:44:48 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-03-23 03:29:40 -0600

Seen: 2,127 times

Last updated: Mar 23 '15