%%将matlab的变量输出为opencv 的yml文件,以方便opencv 读取
%% 参数为变量,输出文件名,yml文件的节点名字,新建或者追加
function Matlab2OpenCV( variable, fileName,varName, flag)
% INPUTS:
% variable: name of the current matrix
% fileName: name of the output yaml file
% flag: w for writing a new file, a for appending to the file
% OUTPUTS:
% none, however it does output a .yaml file. For an example see the
% provided joints.yaml file.
% =========================================================================
% Get the size of the current matrix.
[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 the header for the Opencv Matrix
fprintf( file, ' %s: !!opencv-matrix\n',varName);
fprintf( file, ' rows: %d\n', rows);
fprintf( file, ' cols: %d\n', cols);
fprintf( file, ' dt: d\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);