OpenCV之FileStorage类的数据存取操作与示例


OpenCV —FileStorage类的数据读写操作与示例


OpenCV的许多应用都需要使用数据的存储于读取,例如经过3D校准后的相机,需要存储校准结果矩阵,以方便下次调用该数据;基于机器学习的应用,同样需要将学习得到的参数保存等。OpenCV通过XML/YAML格式实现数据持久化。本文简要梳理了使用FileStorage类进行基本数据持久化操作,给出了示例代码。

主要内容包括:

FileStorage类
  • 构造函数
  • operator <<
  • FileStorage::open
  • FileStorage::isOpened
  • FileStorage::release
  • FileStorage::getFirstTopLevelNode
  • FileStorage::root
  • FileStorage::operator[]

示例代码
  • 创建写入器、创建读取器
  • 写入数值、写入矩阵、写入自定义数据结构、写入当前时间
  • 读取数值、读取矩阵、读取自定义数据结构、读取当前时间
  • 关闭写入器、关闭读取器


FileStorage类


FileStorage类将各种OpenCV数据结构的数据存储为XML 或 YAML格式。同时,也可以将其他类型的数值数据存储为这两种格式。

构造函数

FileStorage类的构造函数为:

cv::FileStorage(const string& source, int flags, const string& encoding=string());
  
  

参数:

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

flags – 操作模式,包括:

  • FileStorage::READ 打开文件进行读操作
  • FileStorage::WRITE 打开文件进行写操作
  • FileStorage::APPEND打开文件进行附加操作
  • FileStorage::MEMORY 从source读数据,或向内部缓存写入数据(由FileStorage::release返回)

encoding – 文件编码方式。目前不支持UTF-16 XML 编码,应使用 8-bit 编码。

写数据operator <<

向filestorage中写入数据


  
  
  1. template<typename_Tp> FileStorage& operator<<(FileStorage& fs, const _Tp& value)
  2. template<typename_Tp> FileStorage& operator<<(FileStorage& fs, const vector<_Tp>& vec)
参数:

fs – 已经打开的用于写数据的file storage对象

value – 待写入fs 的数据.

vec – 待写入fs 的向量值

 

以下代码分别演示写入数值、矩阵、多个变量、当前时间和关闭文件:


  
  
  1. // 1.create our writter
  2. cv:: FileStorage fs("test.yml", FileStorage::WRITE);
  3. // 2.Save an int
  4. int imageWidth= 5;
  5. int imageHeight= 10;
  6. fs << "imageWidth" << imageWidth;
  7. fs << "imageHeight" << imageHeight;
  8. // 3.Write a Mat
  9. cv::Mat m1= Mat::eye( 3, 3, CV_8U);
  10. cv::Mat m2= Mat::ones( 3, 3, CV_8U);
  11. cv::Mat resultMat= (m1+ 1).mul(m1+ 2);
  12. fs << "resultMat" << resultMat;
  13. // 4.Write multi-variables
  14. cv::Mat cameraMatrix = (Mat_< double>( 3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
  15. cv::Mat distCoeffs = (Mat_< double>( 5, 1) << 0.1, 0.01, -0.001, 0, 0);
  16. fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
  17. // 5.Save local time
  18. time_t rawtime; time(&rawtime); //#include <time.h>
  19. fs << "calibrationDate" << asctime(localtime(&rawtime));
  20. // 6.close the file opened
  21. fs.release();

生成的文件test.yml


FileStorage::open

打开一个文件

boolFileStorage::open(const string& filename, int flags, const string&encoding=string())
  
  

参数:

filename – 待打开的文件名,其扩展名(.xml 或 .yml/.yaml) 决定文件格式(XML 或 YAML)

flags – 操作模式。见构造函数

encoding – 文件编码方式。


  
  
  1. // open a file
  2. cv::FileStorage fs;
  3. fs.open( "test.yml",FileStorage::WRITE);
  4. // ... some process here
  5. fs.release();


FileStorage::isOpened

检查文件是否已经打开,调用:

boolFileStorage::isOpened()

  
  

返回:

ture – 如果对象关联了当前文件;

false – 其他情况。

尝试打开文件后调用此方法是个比较好的做法。


  
  
  1. // Checks whether the file is opened
  2. cv::FileStorage fs;
  3. fs.open( "test.yml",FileStorage::WRITE);
  4. bool flag = fs.isOpened();
  5. cout<< "flag = "<<flag<< endl<< endl;
  6. // if failed to open a file
  7. if(!fs.isOpened()){
  8. cout<< "failed to open file test.yml "<< endl<< endl;
  9. return 1;
  10. }

运行结果:


FileStorage::release

存储或读取操作完成后,需要关闭文件并释放缓存,调用

void FileStorage::release()
  
  

  
  
  1. cv:: FileStorage fs("test.yml", fs::WRITE);
  2. //... some processing here
  3. fs.release();

FileStorage::getFirstTopLevelNode

返回映射(mapping)顶层的第一个元素:

FileStorage::getFirstTopLevelNode()
  
  


  
  
  1. // open a file for reading
  2. fs.open( "test.yml", FileStorage::READ);
  3. // get the first top level node
  4. int firstNode = fs.getFirstTopLevelNode();
  5. cout<< "the First Top Level Node = "<<firstNode<< endl<< endl;

运行结果



FileStorage::root

返回顶层映射(mapping)

FileNode FileStorage::root(int streamidx=0) 
  
  

参数:

streamidx – 从0开始的字符串索引。大部分情况文件中只有一个串,但YAML支持多个串,因此可以有多个。

Returns: 顶层映射


读数据:FileStorage::operator[]

返回指定的顶层映射元素


  
  
  1. FileNode FileStorage:: operator[]( const string& nodename) const
  2. FileNode FileStorage:: operator[]( const char* nodename) const

参数:

nodename – 文件节点名(见下文FileNode类)

返回:名称为nodename的节点数据


  
  
  1. // read data using operator []
  2. cv:: FileStorage fs("test.yml", FileStorage::READ);
  3. int width;
  4. int height;
  5. fs[ "imageWidth"]>>width;
  6. fs[ "imageHeight"]>>height;
  7. cout<< "width readed = "<<width<< endl;
  8. cout<< "height readed = "<<height<< endl<< endl;
  9. // read Mat
  10. cv::Mat resultMatRead;
  11. fs[ "resultMat"]>>resultMatRead;
  12. cout<< "resultMat readed = "<<resultMatRead<< endl<< endl;
  13. cv::Mat cameraMatrixRead,distCoeffsRead;
  14. fs[ "cameraMatrix"]>>cameraMatrixRead;
  15. fs[ "distCoeffs"]>>distCoeffsRead;
  16. cout<< "cameraMatrix readed = "<<cameraMatrixRead<< endl;
  17. cout<< "distCoeffs readed = "<<distCoeffsRead<< endl<< endl;
  18. // read string
  19. string timeRead;
  20. fs[ "calibrationDate"]>>timeRead;
  21. cout<< "calibrationDate readed = "<<timeRead<< endl<< endl;
  22. fs.release();

运行结果

转载自:http://blog.csdn.net/iracer/article/details/51339377

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值