OpenCV Initial - Using XML and YAML for File Input and Output

Preface:

FileStorage (const string & soutce, int flages, const string & encoding = string (): class constructor

  1. source: If the file name to be opened or the string to be read is the file name, the file suffix determines its format, and the file suffix plus. gz becomes a compressed file.

  2. flags: specify the operation flag

    • FileStorage::READ: Read in files
    • FileStorage::WRITE: Files to be added
    • FileStorage::APPEND: Files to be attached
    • FileStorage::MENORY: Read data from source or write data to a buffer

    If FileStorage:: WRITE + FileStorage:: MENORY is specified, scorce determines the file format of the output.

  3. Encoding: file encoding rules, which do not currently support UTF-16 XML encoding rules, and can only use 8 bit encoding.

Bool FileStorage:: open (const string & filename, int flages, const string & encoding = string ()): Open the file

  1. filename: A file name or a literal string of read-in data; if it is a file name, the file suffix must be. xml or yml
  2. flags: Operational mode
  3. Encoding: file encoding rules

Template < type_Tp > FileStorage & operator < (FileStorage & fs, const_TP & valus): Write data to file storage
Or: template < typename_Tp > FileStorage & operator < (FileStorage & fs, const_TP & vec)

  1. fs: Opened file store used to write data velue: write data stored in file

  2. vec: Vector values written to file storage

template<type_Tp>void operator<<(const FileNode& n, _TP& v
Alus: Read data from file storage
Or: template < type_Tp > void operator < (const FileNode & n, vector < _TP > & v)
ec)
Or: template < type_Tp > FileNodeIterator operator < (const FileNodeIterator)&
it, _TP& vec)
Or: template < type_Tp > FileNodeIterator operator < (const FileNodeIterator)&
it,vector< _TP>& vec)

  1. n: Nodes to read data

  2. value: Data read from file storage

  3. vec: Vector values read from file storage

  4. it: an iterator for reading in data

//The code for accessing OpenCV's data structure through XML and YAML files is as follows
#include<opencv2/core/core.hpp>
#include<iostream>
#include<string>

using namespace std;
using namespace cv;

//Define data structures
class MyData
{
public:
	//Data member
	int A;
	double X;
	string id;


	//Member function
	MyData() :A(0), X(0), id()
	{}

	//Open to avoid implicit conversion
	explicit MyData(int) :A(97), X(CV_PI), id("mydata1234")
	{}

	//Output of this class
	void write(FileStorage& fs) const
	{
		fs << "{" << "A" << A << "X" << X << "id" << id << "}";
	}
	
	//Such input
	void read(const FileNode& node)
	{
		A = (int)node["A"];
		X = (double)node["X"];
		id = (string)node["id"];
	}
};

//Actual Entry-Exit and Output Functions
static void wirte(FileStorage& fs, const std::string&,const MyData& x)
{
	x.write(fs);
}

static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData())
{
	if (node.empty())
		x = default_value;
	else
		x.read(node);
}

//FileStorage Input Function
static ostream& operator<<(ostream& out, const MyData& m)
{
	out << "{ id = " << m.id << ",";
	out << "X = " << m.X << ",";
	out << "A = " << m.A << ",";
}

int main(int ac, char** av)
{
	string filename = "C:\\outputfile.yml";
	
	//Write in
	{
		cout << endl << "Write to start:" << endl;
		Mat R = Mat_<uchar>::eye(3, 3),
			T = Mat_<double>::zeros(3, 1);
		MyData m(1);

		FileStorage fs(filename, FileStorage::WRITE);

		fs << "iterationNr" << 100;

		//String Start
		fs << "string" << "{";
		fs << "C:\\LENA.jpg" << "Awesomeness"
			<< "C\\baboon.jpg";
		
		//End of string
		fs << "]";

		//String matching
		fs << "Mapping";
		fs << "{" << "one" << 1;
		fs << "tow" << 2 << "}";

		//cv::Mat
		fs << "R" << R;
		fs << "T" << T;

		//data structure
		fs << "MyData" << m;

		//Release fs memory
		fs.release();
		cout << "Write completion" << endl;
	}

	//read
	{
		cout << endl << "Read start:" << endl;
		FileStorage fs;
		fs.open(filename, FileStorage::READ);

		int itNr;
		//fs["iterationNr"] >> itNr;
		itNr = (int)fs["iterationNr"];
		cout << itNr;
		if (!fs.isOpened)
		{
			cerr << "failed to open" << filename << endl;
			return 1;
		}

		//Read the string and get the node
		FileNode n = fs["strings"];
		if (n.type() != FileNode::SEQ)
		{
			cerr << "Nodes are out of order!" << endl;
			return 1;
		}

		//Go through the node
		FileNodeIterator it = n.begin(), it_end = n.end();
		for (; it != it_end; ++it)
			cout << (string)*it << endl;

		//Read mappings from a sequence
		n = fs["Matting"];
		cout << "Two " << (int)(n["Two"]) << ";";
		cout << "One " << (int)(n["One"]) << endl << endl;

		MyData m;
		Mat R, T;

		//Read cv::Mat
		fs["R"] >> R;
		fs["T"] >> T;
		//Read your own data structure
		fs["MyData"] >> m;

		cout << endl
			<< "R = " << R << endl;
		cout << "T = " << T << endl << endl;
		cout << "MyData = " << endl << m << endl << endl;

		//Display basic features to non-existent nodes
		cout << "Attempt to read nonexistent";
		fs["NonExisting"] >> m;
		cout << endl << "NonExisting = " << endl << m << endl;
	}
	
	cout << endl
		<< "Open the stored file using a text editor" << filename
		<< endl;

	getchar();

	return 0;
}

Keywords: encoding xml OpenCV

Added by smokenwhispers on Tue, 08 Oct 2019 21:55:57 +0300