OpenCV Save as XML (Filestorage) and CSV (overloaded << operator) files

Source: Internet
Author: User

XML file (using the Filestorage Class)

When using OpenCV not only to save the image results, often also need to save the intermediate matrix results, and OPENCV imwrite function only support cv8u type of data ( using OPENCV to save other types of mat, the program will not error, but unable to generate the result file ), so it will bring a lot of inconvenience to the work. OPENCV provides the Filestorage class for users to use directly and save them as Xml/yaml files in later versions of 2.0.

Save XML

The save example is as follows:

= Mat::eye(Size(12,12), CV_8UC1);FileStorage fs("f:\\vocabulary.xml", FileStorage::WRITE<<"vocabulary"<< mat;fs.release();

Get the result file as follows:

<?xml version= "1.0"?><opencv_storage><vocabulary type_id="Opencv-matrix">  <rows>12</rows>  <cols>12</cols>  <DT>U</dt>  <data>1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1</Data></Vocabulary></opencv_storage>

It is important to note that because the saved result is an XML file, it needs to be given a label when it is saved, and the format is the following, storing the label content and then storing the matrix:

"vocabulary" << mat;
Read XML

You can also use the Filestorage class to get the contents of the saved XML file directly.
The reading example is as follows:

fs(".\\vocabulary.xml", FileStorage::READ);Mat mat_vocabulary;fs["vocabulary"] >> mat_vocabulary;

When reading a file, it is also given the label content, which is given by means of brackets [] :

fs["vocabulary"] >> mat_vocabulary;
CSV file

Many times, it is more convenient to use the CSV file directly than XML, the data saved using XML, whether it is to view (Excel view, the matrix is not formatted in XML) or then use another tool processing (such as MATLAB), can be quickly read, and XML needs parsing is more cumbersome. So how to save the CSV file, and how to read the saved CSV file, see below decomposition.

Save CSV file

OPENCV provides the format of the formatted function saved as result, and we can print and save the data in the format we want, using format.

Just look at the preservation function, then analyze the specific meaning:

file("test.csv");fileformat(mat, Formatter::FMT_CSV);file.close();

According to the above code only open the CSV file to be written with the Ofstream object, and then write the data directly, it is important to note that the access matrix is formatted with format, the second parameter of the Format function represents the form type, here the CSV format is Formatter:: Fmt_csv.

Save Results:

This saves the format as an enumeration type defined in the formatter class, with a total of 6 options, which you can choose according to your needs.

Class Cv_exports formatter{ Public:enum{Fmt_default =0, Fmt_matlab =1, Fmt_csv =2, Fmt_python =3, Fmt_numpy =4, Fmt_c =5};Virtual~formatter ();Virtualptr<formatted> Format (Constmat& MTX)Const=0;Virtual voidSet32fprecision (intp =8) =0;Virtual voidSet64fprecision (intp = -) =0;Virtual voidSetmultiline (BOOLml =true) =0;StaticPtr<formatter>Get(intFMT = Fmt_default);};
Read CSV data

How to read it after saving it in CSV format, which can be read using the function of the Traindata class of the ML module in OpenCV. The specific code looks like this, the second behavior loads the data, the parameter 0 means that there is no header row, and the third row gets the resulting data.

Ptr<TrainData>= TrainData::loadFromCSV("test.csv"0= train_data-><< format(m, Formatter::FMT_CSV<< endl;

The result of the code operation is:

Reference Links:
1. OpenCV using Filestorage to save mat data
2. xml/yaml file storages. Writing to a file storage.

OpenCV Save as XML (Filestorage) and CSV (overloaded << operator) files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.