opencv写入读取xml、yaml文件

写入读取xml、yaml文件

#include 
#include 
#include 

using namespace std;
using namespace cv;

//xml文件的写入
int main()
{
	FileStorage fs("test.yaml", FileStorage::WRITE);
	fs << "frameCount" << 5;
	time_t rawtime;
	time(&rawtime);
	char str[50];
	struct tm buf;
	localtime_s(&buf, &rawtime);
	asctime_s(str, sizeof str, &buf);
	fs << "calibrationDate" << str;
	Mat cameraMatrix = (Mat_<double>(3, 3) << 100, 0, 320, 0, 1000, 240, 0, 0, 1);
	Mat distCoeffs = (Mat_<double>(5, 1) << 0.1, 0.01, -0.001, 0, 0);
	fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
	fs << "features" << "[";
	for (int i = 0; i < 3; i++) {
		int x = rand() % 640;
		int y = rand() % 480;
		uchar lbp = rand() % 256;
		fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
		for (int j = 0; j < 8; j++) {
			fs << ((lbp >> j) & 1);
		}
		fs << "]" << "}";
	}
	fs << "]";
	fs.release();

	system("color 6F");
	FileStorage fs2("test.yaml", FileStorage::READ);
	int frameCount = (int)fs2["frameCount"];
	string date;
	fs2["calibrationDate"] >> date;
	Mat cameraMatrix2, distCoeffs2;
	fs2["cameraMatrix"] >> cameraMatrix2;
	fs2["distCoeffs"] >> distCoeffs2;
	cout << "frameCount:" << frameCount << endl
		<< "calibrationDate:" << date << endl
		<< "cameraMatrix:" << cameraMatrix2 << endl
		<< "distCoeffs" << distCoeffs2 << endl;
	FileNode features = fs2["features"];
	FileNodeIterator it = features.begin(), it_end = features.end();
	int idx = 0;
	vector<uchar> lbpval;
	for (; it != it_end; idx++, it++) {
		cout << "feature #" << idx << ": ";
		cout << "x = " << (int)(*it)["x"] << ",y = " << (int)(*it)["y"] << ",lbp:(";
		(*it)["lbp"] >> lbpval;
		for (int i = 0; i < (int)lbpval.size(); i++) {
			cout << " " << (int)lbpval[i];
		}
		cout << ")" << endl;
	}
	fs2.release();
	printf("\n文件读取完毕,请输入任意键结束程序~");
	return 0;
}

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