参考地址:
http://blog.csdn.net/jarvischu/article/details/8481510
《Learning opencv3》
创建XML
向XML中存储或者是读取Int float型基本数据
通过创建XML元素,存取复杂的结构如:结构体、矩阵
opencv3提供一种机制用于序列化和去序列化,可以操作disk中的YAML文件或者XML文件。这些方式可以用于在一个文件中加载或者存储任何数量,任意类中的数据物体(包括基本的数据类型,etc:int,float)。FileStorage对象是最基本的读写文件的机制。
1.
FileStorage();
这种构造函数,可以使用
FileStorage::open( string fileName, int flag );
2.
FileStorage(const String& source, int flags, const String& encoding=String());
FileStorage(CvFileStorage* fs, bool owning=true);
1.
FileStorage中的数据存储方式有两种
在第一层,写入方式都是mapping,在第一层的mapping 中,你可以放置其它的mapping和sequence,深度没有任何限制
2.<<
使用操作符 <<进行写入
myFileStorage << "someInteger" << 27; // save an integer
myFileStorage << "anArray" << cv::Mat::eye(3,3,CV_32F); // save an array
3.写入规则
为了使用sequence,你首先提供文件名,然后再进行操作。
你可以操作数(int,float..),string,以及任意 OpenCV数据类型.
如果需要创建新的mapping(sequence),需要{ 或者[来开始,
以}或者]来结束
myFileStorage << "theCat" << "{";
myFileStorage << "fur" << "gray" << "eyes" << "green" << "weightLbs" << 16;
myFileStorage << "}";
4.释放
cv::FileStorage::release()
1.
FileStorage::FileStorage( string fileName, int flag );
flag需要被设置为FileStorage::READ
2.如果文件没有被打开,需要打开
FileStorage::open( string fileName, int flag );
两种方式
1)[]
2)FileNodeIterator
读出对象
1)从mapping 中读出,需要使用[]
2)从sequence中读出,需要[]
如果FileNode代表一个对象(或者number或者string),可以用>>来进行加载。
cv::Mat anArray;
myFileStorage["calibrationMatrix"] >> anArray;
int aNumber;
myFileStorage["someInteger"] >> aNumber;
int aNumber;
aNumber = (int)myFileStorage["someInteger"];
#include "opencv2/opencv.hpp"
#include
int main(int, char** argv)
{
cv::FileStorage fs("test.yml", cv::FileStorage::WRITE);
fs << "frameCount" << 5;
time_t rawtime; time(&rawtime);
fs << "calibrationDate" << asctime(localtime(&rawtime));
cv::Mat cameraMatrix = (
cv::Mat_ (3,3)
<< 1000, 0, 320, 0, 1000, 240, 0, 0, 1
);
cv::Mat distCoeffs = (
cv::Mat_ (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;
Data Persistence | 199
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();
return 0;
}
cv::FileStorage fs2("test.yml", cv::FileStorage::READ);
// first method: use (type) operator on FileNode.
int frameCount = (int)fs2["frameCount"];
// second method: use cv::FileNode::operator >>
//
std::string date;
fs2["calibrationDate"] >> date;
cv::Mat cameraMatrix2, distCoeffs2;
fs2["cameraMatrix"] >> cameraMatrix2;
fs2["distCoeffs"] >> distCoeffs2;
cout << "frameCount: " << frameCount << endl
<< "calibration date: " << date << endl
<< "camera matrix: " << cameraMatrix2 << endl
<< "distortion coeffs: " << distCoeffs2 << endl;
cv::FileNode features = fs2["features"];
cv::FileNodeIterator it = features.begin(), it_end = features.end();
int idx = 0;
std::vector lbpval;
Data Persistence | 203
// iterate through a sequence using FileNodeIterator
for( ; it != it_end; ++it, idx++ )
{
cout << "feature #" << idx << ": ";
cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
// ( Note: easily read numerical arrays using FileNode >> std::vector. )
//
(*it)["lbp"] >> lbpval;
for( int i = 0; i < (int)lbpval.size(); i++ )
cout << " " << (int)lbpval[i];
cout << ")" << endl;
}
fs.release();
#include "mainwindow.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
#include
#include
using namespace std;
using namespace cv;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//创建XML文件存储
CvFileStorage* fs = cvOpenFileStorage("D:\\jarvischu.xml",
0, //用于存储的内存,如果为NULL,则会使用一个临时内存
CV_STORAGE_WRITE,
"GB2312"//编码
);
//写入数据
cvWriteInt(fs,"frame_count",10); //--写Int
cvWriteReal(fs,"pi",3.14); //--写Float
//--写结构体
cvStartWriteStruct(fs,"frame_size",CV_NODE_MAP,"id_size" );// 有名称(会新建一个标签)
cvWriteInt(fs,"width",320);
cvWriteInt(fs,"height",200);
cvEndWriteStruct(fs);
cvStartWriteStruct(fs,"author_info",CV_NODE_SEQ,"id_author");// 无名称
cvWriteString(fs,0,"JarvisChu");
cvWriteString(fs,0,"China");
cvEndWriteStruct(fs);
//--写矩阵
unsigned char vec[]={1,2,3,4,5,6,7,8,9};
CvMat mat = cvMat(3,3,CV_8UC1,vec);
cvWrite(fs,"Matrix",&mat);
//--写注释
cvWriteComment(fs,"This is a example for operatoring the xml file",0);//不为0表示放在当前行,0表示新行
//释放
cvReleaseFileStorage(&fs);
//打开XML文件
fs = cvOpenFileStorage("jarvischu.xml",
0, //用于存储的内存,如果为NULL,则会使用一个临时内存
CV_STORAGE_READ,
"GB2312"//编码
);
//读取数据
int f = cvReadIntByName(fs,0,"frame_count",0);//读取有名字的Int
//--读取元素数据
CvFileNode* fn = cvGetFileNodeByName(fs,0,"frame_size"); //先读取父元素(标签,节点)
int width = cvReadIntByName(fs,fn,"width"); //通过父元素读取子元素
int height = cvReadIntByName(fs,fn,"height");
//--读取元素内的顺序数据
fn = cvGetFileNodeByName(fs,0,"author_info");//先读取元素
CvSeq* seq = fn->data.seq; //获取元素的顺序流
const char* name = cvReadString((CvFileNode*)cvGetSeqElem(seq,0));//读取顺序流中数据 0
const char* country = cvReadString((CvFileNode*)cvGetSeqElem(seq,1));//读取顺序流中数据 1
cout<<"frame_count:"<"width:"<"height"<"name:"<"country:"<