opencv文件的读写

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

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

	fs << "VECTOR" << "["; 
	for (list<CvRect>::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<CvRect>& 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();
}

猜你喜欢

转载自blog.csdn.net/zouxin_88/article/details/81284582
今日推荐