opencv文件的读写

void WriteCvRectList(const list& listRect)
{
	FileStorage fs(SAVE_FILE_NAME, FileStorage::WRITE); //创建XML文件

	if (!fs.isOpened())
	{
		cerr << "failed to open " << SAVE_FILE_NAME << endl;
	}

	fs << "VECTOR" << "["; 
	for (list::const_iterator itr = listRect.begin(); itr != listRect.end(); itr++)
	{
		fs << "{";
		fs << "x" << itr->x;
		fs << "y" << itr->y;
		fs << "width" << itr->width;
		fs << "height" << itr->height;
		fs << "}";
	}
	fs << "]";

	fs.release();
}

void ReadCvRectList(list& listRect)
{
	listRect.clear();

	FileStorage fs(SAVE_FILE_NAME, FileStorage::READ); //创建XML文件

	if (!fs.isOpened())
	{
		cerr << "failed to open " << SAVE_FILE_NAME << endl;
	}

	FileNode n = fs["VECTOR"];
	if (n.type() != FileNode::SEQ)
	{
		cerr << "VECTOR is not a sequence! FAIL" << endl;
	}
	for (FileNodeIterator it = n.begin(); it != n.end(); it++)
	{
		CvRect rect;
		rect.x = (*it)["x"];
		rect.y = (*it)["y"];
		rect.width = (*it)["width"];
		rect.height = (*it)["height"];

		listRect.push_back(rect);
	}


	fs.release();
}

 

你可能感兴趣的:(C++,opencv,xml)