opencv通过FileStorage读写xml、yaml文件

FileStorage类

FileStorage类将各种OpenCV数据结构的数据存储为XML或YML格式。

构造函数:cv::FileStorage(const string& source, int flags, const string& encoding=string());

参数说明:

source:存储或读取数据的文件名(字符串),其扩展名(.xml 或 .yml或者.yaml)决定文件格式。

flags:操作方式,包括:FileStorage::READ、FileStorage::WRITE、FileStorage::APPEND。

encoding:编码方式,用默认值就好。

 写xml/ymal文件:

#include
#include

using namespace std;
using namespace cv;

void main()
{
	//标定结果保存
	FileStorage fs("caliResult.xml", FileStorage::WRITE);

	int cameraId = 0;
	Mat intrMatrix = (Mat_(3, 3) << 7.7881772950073355e+002, 0, 3.1562441595543476e+002, 0, 7.8624564811643825e+002, 2.5630331974129393e+002, 0, 0, 1);
	Mat distCoeffs = (Mat_(1, 5) << -7.2660835182078581e-002, 2.0765291395491934e+000, 5.9477659924542790e-004, -8.2981148319346263e-004, -7.0307616798578119e+000);

	fs << "cameraId" << cameraId;
	fs << "intrinsic_parameters" << intrMatrix;
	fs << "distortion_parametes" << distCoeffs;

	fs.release();
}

 保存的.xml文件:



0

  3
  3
  
d
7.7881772950073355e+02 0. 3.1562441595543476e+02 0. 7.8624564811643825e+02 2.5630331974129393e+02 0. 0. 1.
1 5
d
-7.2660835182078581e-02 2.0765291395491934e+00 5.9477659924542790e-04 -8.2981148319346263e-04 -7.0307616798578119e+00

读xml/ymal文件:

#include
#include

using namespace std;
using namespace cv;

void main()
{
    //FileStorage fs("caliResult.xml",FileStorage::READ);
    FileStorage fs;
    fs.open("caliResult.xml",FileStorage::READ);
    if(!fs.isOpened())
    {
        return;
    }

    int cameraId;
    Mat intrMatrix(3,3,CV_64F);
    Mat distCoeffs(5,1,CV_64F);

    fs["cameraId"]>>cameraId;
    fs["intrinsic_parameters"]>>intrMatrix;
    fs["distortion_parametes"]>>distCoeffs;

    fs.release();
}

 

 

你可能感兴趣的:(alan)