Reading/Writing XML and YAML files
Demonstration of reading/writing from/to XML and YAML file storages.
Sources:
Contents
Data
First we create some different variables to save. We have an integer, a text string (calibration date), 2 matrices, and a custom structure "feature" (struct-array), which includes feature coordinates and LBP (local binary pattern) value.
frameCount = int32(5); calibrationDate = datestr(now()); cameraMatrix = [1000, 0, 320; 0, 1000, 240; 0, 0, 1]; distCoeffs = [0.1; 0.01; -0.001; 0; 0]; features = struct('x',cell(1,3), 'y',cell(1,3), 'lbp',cell(1,3)); for i=1:numel(features) features(i).x = randi(640,'int32'); features(i).y = randi(480,'int32'); features(i).lbp = num2cell(bitget(randi([0 255],'uint8'),1:8)); end
now we collect the variable inside a structure fields (that way we get a named collection (mapping) of variables)
S = struct(... 'frameCount',frameCount, ... 'calibrationDate',calibrationDate, ... 'cameraMatrix',cameraMatrix, ... 'distCoeffs', distCoeffs, ... 'features',features); display(S) display(S.features)
S = struct with fields: frameCount: 5 calibrationDate: '01-Dec-2017 17:13:58' cameraMatrix: [3×3 double] distCoeffs: [5×1 double] features: [1×3 struct] 1×3 struct array with fields: x y lbp
Save
next we save them to XML/YML files
fname = tempname(); cv.FileStorage([fname '.xml'], S) cv.FileStorage([fname '.yml'], S)
XML file
type([fname '.xml'])
<?xml version="1.0"?> <opencv_storage> <frameCount>5</frameCount> <calibrationDate>"01-Dec-2017 17:13:58"</calibrationDate> <cameraMatrix type_id="opencv-matrix"> <rows>3</rows> <cols>3</cols> <dt>d</dt> <data> 1000. 0. 320. 0. 1000. 240. 0. 0. 1.</data></cameraMatrix> <distCoeffs type_id="opencv-matrix"> <rows>5</rows> <cols>1</cols> <dt>d</dt> <data> 1.0000000000000001e-01 1.0000000000000000e-02 -1.0000000000000000e-03 0. 0.</data></distCoeffs> <features> <_> <x>593</x> <y>363</y> <lbp> 1 1 0 1 1 0 1 1</lbp></_> <_> <x>386</x> <y>52</y> <lbp> 0 0 1 0 1 1 1 0</lbp></_> <_> <x>419</x> <y>28</y> <lbp> 1 0 1 0 0 0 0 0</lbp></_></features> </opencv_storage>
YML file
type([fname '.yml'])
%YAML:1.0 --- frameCount: 5 calibrationDate: "01-Dec-2017 17:13:58" cameraMatrix: !!opencv-matrix rows: 3 cols: 3 dt: d data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ] distCoeffs: !!opencv-matrix rows: 5 cols: 1 dt: d data: [ 1.0000000000000001e-01, 1.0000000000000000e-02, -1.0000000000000000e-03, 0., 0. ] features: - x: 593 y: 363 lbp: - 1 - 1 - 0 - 1 - 1 - 0 - 1 - 1 - x: 386 y: 52 lbp: - 0 - 0 - 1 - 0 - 1 - 1 - 1 - 0 - x: 419 y: 28 lbp: - 1 - 0 - 1 - 0 - 0 - 0 - 0 - 0
Load
we read the files back
S_xml = cv.FileStorage([fname '.xml']); S_yml = cv.FileStorage([fname '.yml']);
and show the result
display(S_yml)
display(S_yml.cameraMatrix)
display(S_yml.distCoeffs)
celldisp(S_yml.features, 'features')
S_yml = struct with fields: frameCount: 5 calibrationDate: '01-Dec-2017 17:13:58' cameraMatrix: [3×3 double] distCoeffs: [5×1 double] features: {[1×1 struct] [1×1 struct] [1×1 struct]} 1000 0 320 0 1000 240 0 0 1 0.1000 0.0100 -0.0010 0 0 features{1} = x: 593 y: 363 lbp: {[1] [1] [0] [1] [1] [0] [1] [1]} features{2} = x: 386 y: 52 lbp: {[0] [0] [1] [0] [1] [1] [1] [0]} features{3} = x: 419 y: 28 lbp: {[1] [0] [1] [0] [0] [0] [0] [0]}
finally, we clean up the temporary files
delete([fname '.xml']) delete([fname '.yml'])