Opencv filestorage class reads and writes XML/yml files

Source: Internet
Author: User
Reprinted: http://www.cnblogs.com/summerRQ/articles/2524560.html

In the opencv program, it is often used to save intermediate results. XML /. yml files, before opencv2.0, all use C-style code. At that time, the functions cvload () and cvsave () were used to read and write XML files respectively (). After 2.0, opencv was converted to support C ++, which greatly reduced the amount of code and the details to be considered during programming.

In the C ++ interface of the new version of opencv, imwrite () and imread () can only store integer data and must be in the image format. When you need to save floating point data or XML/yml files, the cvsave () function of the C language interface has been deleted from the C ++ interface, instead of the filestorage class. This class is very convenient and encapsulates a lot of data structure details. During programming, you can save the data structure according to the unified interface.

1. filestorage class write XML/yml files

• Create a New filestorage object to open a file in the format of filestorage: Write.

• Use <operation to operate the file.

• Release the object and close the object.

Example:

Filestorage FS ("test. yml ", filestorage: Write); FS <" framecount "<5; time_t rawtime; time (& rawtime ); FS <"calibrationdate" <asctime (localtime (& rawtime); MAT cameramatrix = (MAT _ <double> (1000) <0,320, 1000,240, 0, 0, 0, 1); // another mat initialization method mat distcoeffs = (MAT _ <double> (0.1) <0.01, 0.001,-, 0, 0); FS <"cameramatrix" <cameramatrix <"distcoeffs" <distcoeffs; // features is a vector 3 in size, where each element is composed of a random number X, the array Y and the uchar array of 8 form the FS <"Features" <"["; for (INT I = 0; I <3; I ++) {int x = rand () % 640; int y = rand () % 480; uchar HSV = rand () % 256; FS <"{: "<" X "<x <" Y "<Y <" sp_in "<" [: "; for (Int J = 0; j <8; j ++) FS <(HSV> J) & 1); FS <"]" <"}" ;}fs <"]"; FS. release ();

2. Read XML/yml files using the filestorage class

Filestorage is composed of hierarchical nodes in the memory. Each node type is filenode. filenode allows a single value, array, or a collection of filenode. Filenode can also be seen as a container. You can use the iterator interface to access smaller units of content in the node, such as accessing the content of "Features" in the file stored above. Steps are similar to writing files:

• Create a New filestorage object to open an existing file in the format of filestorage: Read

• Use the filestorage: operator [] () function to read files, or use filenode and filenodeiterator

• Use filestorage: release () to close the file

Example:

Filestorage FS ("test. yml ", filestorage: Read); // Method 1: [] OPERATOR int framecount = (INT) FS [" framecount "]; // Method 2: filenode :: operator> () string date; FS ["calibrationdate"]> date; MAT cameramatrix2, distcoeffs2; FS ["cameramatrix"]> cameramatrix2; FS ["distcoeffs"]> distcoeffs2; // note the use of filenodeiterator. It seems that only one-dimensional arrays can be used to read all the data in filenode features = FS ["Features"]; filenodeiterator it = features. begin (), it_e ND = features. End (); int idx = 0; STD: vector <uchar> lbpval; For (; it! = It_end; ++ it, idx ++) {cout <"feature #" <idx <":"; cout <"x =" <(INT) (* It) ["X"] <", y =" <(INT) (* It) ["Y"] <", sp_in :("; (* It) ["HSV"]> lbpval; // directly read the one-dimensional vector for (INT I = 0; I <(INT) lbpval. size (); I ++) cout <"<(INT) lbpval [I]; cout <") "<Endl;} FS. release ();

In addition, after creating a new filestorage object and opening the file in read or write mode, you can use filestorage: isopened () to check the file status and determine whether the file is successfully opened.

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.