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的說明。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM