Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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