openCV保存XML文件基本操作

openCV保存XML文件基本操作

const std::string fileName = "C://structuredLight/test.xml";

CvFileStorage* fs=cvOpenFileStorage(fileName.c_str(),0,CV_STORAGE_WRITE);
cvReleaseFileStorage(&fs);

 

写入int或者bool,使用cvWriteInt();

写入实数,使用cvWriteReal();

写入字符串,使用cvWriteString(); input需要char*类型,string转char*使用了c_str()函数;

 

const std::string fileName = "C://structuredLight/test.xml";
CvFileStorage* fs=cvOpenFileStorage(fileName.c_str(),0,CV_STORAGE_WRITE);

int ai = 100;
bool T = true;
bool F = false;
float af = 99.87;
std::string astr = "this is a string";

cvWriteInt(fs,"int",ai);
cvWriteInt(fs,"bool_true",T);
cvWriteInt(fs,"bool_false",F);
cvWriteReal(fs,"float",af);
cvWriteString(fs,"string",astr.c_str());

cvReleaseFileStorage(&fs);

 运行结果:

<?xml version="1.0"?>
<opencv_storage>
<int>100</int>
<bool_true>1</bool_true>
<bool_false>0</bool_false>
<float>9.9870002746582031e+001</float>
<string>"this is a string"</string>
</opencv_storage>

需要写入struct类型时,使用成对出现的cvStartWriteStruct()与cvEndWriteStruct();

参数列表中有数据结构选项

CV_NODE_SEQ 表示被写入的数据结构为序列结构,这样的数据没有名称

CV_NODE_MAP 表示被写入的数据结构为图表结构,这样的数据含有名称

 

        params slParams;
    
	slParams.projHeight = 912;
	slParams.projWidth = 1140;
	slParams.camHeight = 1280;
	slParams.camWidth = 1024;
	slParams.colEncode = true;
	slParams.rowEncode = false;

	CvPoint p = cvPoint(2,3);
	
	const std::string fileName = "C://structuredLight/test.xml";
	CvFileStorage* fs=cvOpenFileStorage(fileName.c_str(),0,CV_STORAGE_WRITE);

	cvStartWriteStruct(fs,"slParams",CV_NODE_MAP);
	cvWriteInt(fs,"slParams_projWidth",slParams.projWidth);
	cvWriteInt(fs,"slParams_projHeight",slParams.projHeight);
	cvWriteInt(fs,"slParams_camWidth",slParams.camWidth);
	cvWriteInt(fs,"slParams_camHeight",slParams.camHeight);
	cvWriteInt(fs,"slParams_colEncode",slParams.colEncode);
	cvWriteInt(fs,"slParams_rowEncode",slParams.rowEncode);
	cvEndWriteStruct(fs);

	cvStartWriteStruct(fs,"point",CV_NODE_SEQ);
	cvWriteInt(fs,0,p.x);
	cvWriteInt(fs,0,p.x);
	cvEndWriteStruct(fs);


	cvReleaseFileStorage(&fs);        

 程序写入数据结构slparams与point,其中slparams的数据用图标结构,point的数据用序列结构,运行结果:

<?xml version="1.0"?>
<opencv_storage>
<slParams>
  <slParams_projWidth>1140</slParams_projWidth>
  <slParams_projHeight>912</slParams_projHeight>
  <slParams_camWidth>1024</slParams_camWidth>
  <slParams_camHeight>1280</slParams_camHeight>
  <slParams_colEncode>1</slParams_colEncode>
  <slParams_rowEncode>0</slParams_rowEncode></slParams>
<point>
  2 2</point>
</opencv_storage>

 从结果可以看到slparams中的成员带有名字保存(例如,slparams_projWidth),而point的数据则没有x与y的说明。

 

posted @ 2014-11-10 10:41  Cyrus Ho  阅读(2694)  评论(0编辑  收藏  举报