A_toaster
A_toaster

Reputation: 1288

How to convert an OpenCV Mat that has been written in an xml file back into an image?

I wrote some code to generate an .xml file that contains the RGB data from a Mat file in OpenCV. I would like to recreate this image in MATLAB from the data points in the xml file. I am however unsure of the formatting of the xml file, since when I open it it looks something like this:

<?xml version="1.0?>
<opencv_storage>
<myMatrix type_id="opencv-matrix">
    <rows>116</rows>
    <cols>116</cols>
    <dt>u</dt>
    <data>
      97 101 97 98 99 97 ...
    </data>
    </myMatrix>
    </opencv_storage>

Upvotes: 3

Views: 1672

Answers (2)

berak
berak

Reputation: 39806

you can convert it to matlab format within opencv.

read it in using the Filestorage:

Mat m;
Filestorage fs("m.xml", Filestorage::READ);
fs["myMatrix"] >> m;

then print it out (or write to a file) in matlab format:

// 2.4 version
cerr << format(m,"MATLAB") << endl;
// 3.0 version
cerr << format(m,cv::Formatter::FMT_MATLAB) << endl;

Upvotes: 4

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33904

Get a xml to struct converter such as the following:

http://www.mathworks.com/matlabcentral/fileexchange/28518-xml2struct

this will allow you to convert the data into a struct, then you can simply reshape your data:

pic = reshape(struct.data, struct.rows, struct.cols)
image(pic)

Note: This isn't out of the box code.

Upvotes: 0

Related Questions