opencv操作yaml文件
来源:互联网 发布:网络通信 编辑:程序博客网 时间:2024/05/19 17:11
官网:YAML:http://www.yaml.org XML :http://www.w3c.org/XML
1.YAML与XML文件的打开和关闭
YAML格式的文件拓展名包括:.yml
和 .yaml
,两个都表示YAML文件;
XML格式的文件拓展名为: .xml
。
1.1 文件打开
在Opencv中,使用FileStorage
进行文件读写。XML文件操作与YAML一样,不过存在一些细小差别。
std::string fileName = "E:\\test.yml"; // YAMLstd::string fileName2 = "E:\\test.xml"; // XML// write filecv::FileStorage fs(fileName , cv::FileStorage::WRITE);// read filecv::FileStorage fs2(fileName , cv::FileStorage::READ);// or use: cv::FileStorage::openfs2.open(fileName , cv::FileStorage::READ);
FileStorage
的文件操作模式一共分为四种:READ
,WRITE
,APPEND
,MEMORY
。
文档打开后很关心的一件事就是,进行确认是否成功。FileStorage
有自己的成员函数返回文件打开状态:
// bool FileStorage::isOpened() const;if ( !fs.isOpened() ) // failed{ std::cout<<"Save File Failed!"<<std::endl; return ;}else // succeed{ ...}
1.2 文件关闭
FileStorage
文件关闭比较简单:
fs.release();
2.文件读写
FileStorage
文件读与写的方法与C++语言中的文件流对象的使用很像,对>>
和<<
进行了重载,分别用于文件读取和写入。很棒的是,FileStorage
支持一些常用格式的直接读写,例如字符、字符串、数字、cv::Mat
等。对于不支持的数据结构,只能按照规则自己去写啦~
2.1 写入
fs << "frameCount" << 5; // 字符和数字cv::Mat_<double> cameraMat = cv::Mat_<double>::zeros(3, 3); fs << "Camera Intrinsic Matrix" << cameraMat; // cv::Mat
注意:
fs << "frameCount" <<5
中""
内输出的字符串是有限制的,对于YAML有效范围是:[a-z],[A-Z],[0-9],”-“,”_”和空格。XML与YAML基本一致,但是YAML字符之间加空格是允许的,XML不允许。如果出现以下BUG,请不要慌张,检查一下输入的字符是否有效就OK~
2.2 读取
文件读取的方法有两种:
// first method: use (type) operator on FileNode.int frameCount = (int)fs2["frameCount"];// second second method: use cv::FileNode::operator >>int frameCount;fs2["frameCount"] >> frameCount;
2.3 Mat的操作
这一点真的很不错,而且与C++的输入输出方法很接近(链接:常用的三种Mat
类型):
cv::Mat_<double> cameraMat = cv::Mat_<double>::zeros(3, 3);cv::Mat_<double> distCoeffes = ( cv::Mat_<double>(5, 1)<< 0.1, 0.01, -0.001, 0.0, 0.0 );// C++std::cout<<"Camera Matrix"<<std::endl<<cv::Mat::Mat(cameraMat)<<std::endl;std::cout<<"Distortion Coefficients"<<std::endl<<cv::Mat::Mat(distCoeffes)<<std::endl;// cv::FileStoragefs << "Camera Matrix" << cameraMat;fs << "Distortion Coefficients"<<distCoeffes;
运行结果对比如下:
C++
YAML
XML
2.4 集合的操作
Opencv中将集合分为两类:映射和序列。
映射集合(Mappings, 又称named collections):每个元素有一个名字或者说关键字,并且可以通过名字访问其数据,类似于Key-Value结构。使用方法为:
// Mappings writeint x(1.0), y(0.0);fs << "features" << "["; // also can be "[:"fs <<"{:" << "x" << x << "y" << "}" << "]";
- 1
- 2
- 3
- 4
"{"
和"{:"
输出的结果是不一样的,YAML使用":"
后,使输出的文本具有Python的风格,映射集合会按照一行排列,不适用时,按照每个元素与其值单独一行的方法排列。XML使用":"
后输出结果会有不同,但基本可以视为把":"
忽略。
YAML {
YAML {:
XML {
XML {:
// Mappings readcv::FileNode features = fs2["features"];// 遍历查看cv::FileNodeIterator it = features.begin();std::cout<< "x="<<(int)(*it)["x"]<< " y="<<(int)(*it)["y"]<< " z="<<(int)(*it)["z"]<<std::endl;
输出结果:
- 编程的时候,不在Mapping的
"{ }"
外加上"[ ]"
输出的效果是不一样的,而且在数据读取的时候,加上"[
的Mapping结构会被认为是Mapping结构,否则会出错,以上述的
]"Mappings write
代码为例: 对于fs <<
结构,用上述方法可以读取成功; 对于
"fearures" << "[" << "{" << ... << "}" << "]"fs
结构,用上述方法时就会出错:
<< "features" << "{" << ... << "}"
序列集合(Sequences,又称unnamed collections):数据没有名字名字或者关键字,一般通过序号(indices)访问数据,例如最常见的数组。
与映射类似,序列集合需要在输出开始前加"["
,结束后使用"]"
,并且"[:"
与"["
在输出风格上与映射集合类似。
// Sequences writeint mySeq[5] = {0, 1, 2, 3, 4};fs << "mySeq" << "[";for ( int idx=0; idx<5; idx++ ){ fs << mySeq[idx];}fs << "]";
// Sequences readcv::FileNode mySeq2 = fs2["mySeq"];std::vector<int> seq;cv::FileNodeIterator it = mySeq2.begin(), it_end = mySeq2.end();for ( ; it != it_end; it++ ){ seq.push_back( (int)( *it ) ); // std::cout<<(int)(*it)<<" "<<std::endl;}
3.Opencv documentation 源码示例
下面贴出Opencv documentation中的示例代码,可以作为参考:
// file write#include "opencv2/opencv.hpp"#include <time.h>using namespace cv;using namespace std;int main(int, char** argv){ FileStorage fs("test.yml", FileStorage::WRITE); fs << "frameCount" << 5; time_t rawtime; time(&rawtime); fs << "calibrationDate" << asctime(localtime(&rawtime)); Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1); Mat distCoeffs = (Mat_<double>(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; 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;}
// results%YAML:1.0frameCount: 5calibrationDate: "Fri Jun 17 14:09:29 2011\n"cameraMatrix: !!opencv-matrix rows: 3 cols: 3 dt: d data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]distCoeffs: !!opencv-matrix rows: 5 cols: 1 dt: d data: [ 1.0000000000000001e-01, 1.0000000000000000e-02, -1.0000000000000000e-03, 0., 0. ]features: - { x:167, y:49, lbp:[ 1, 0, 0, 1, 1, 0, 1, 1 ] } - { x:298, y:130, lbp:[ 0, 0, 0, 1, 0, 0, 1, 1 ] } - { x:344, y:158, lbp:[ 1, 1, 0, 0, 0, 0, 1, 0 ] }
// file readFileStorage fs2("test.yml", FileStorage::READ);// first method: use (type) operator on FileNode.int frameCount = (int)fs2["frameCount"];std::string date;// second method: use FileNode::operator >>fs2["calibrationDate"] >> date;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;FileNode features = fs2["features"];FileNodeIterator it = features.begin(), it_end = features.end();int idx = 0;std::vector<uchar> lbpval;// iterate through a sequence using FileNodeIteratorfor( ; it != it_end; ++it, idx++ ){ cout << "feature #" << idx << ": "; cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: ("; // you can also easily read numerical arrays using FileNode >> std::vector operator. (*it)["lbp"] >> lbpval; for( int i = 0; i < (int)lbpval.size(); i++ ) cout << " " << (int)lbpval[i]; cout << ")" << endl;}fs.release();
- opencv操作yaml文件
- OpenCV------- 对XML和YAML文件实现I/O操作
- Opencv对XML和YAML文件实现I/O操作
- opencv对xml和yaml文件的读写操作
- OpenCV:对XML和YAML文件实现I/O操作
- 操作Yaml文件
- python操作yaml文件
- Opencv YAML和XML格式文件操作详解
- OpenCV学习笔记(六)——对XML和YAML文件实现I/O操作
- opencv学习笔记(二)-对xml和yaml文件的读写操作
- OpenCV学习笔记(六)——对XML和YAML文件实现I/O操作
- OpenCV中.xml .yml .yaml 文件的读写操作(矩阵读写)。
- OpenCV学习笔记——对XML和YAML文件实现I/O操作
- OpenCV学习笔记(六)——对XML和YAML文件实现I/O操作
- opencv学习(十七)之XML和YAML文件读写操作
- OpenCV XML YAML 文件输入输出(部分翻译)
- Opencv输出XML和YAML文件
- OpenCV 输入输出XML和YAML文件
- 多线程详解
- Python爬虫 --requests库
- 常量指针与指针常量的区别
- 从零学习Belief Propagation算法(一)
- cmd连接mysql的方法详解,需要的朋友可以参考
- opencv操作yaml文件
- javascript中的对象
- 机器学习项目一(linear regression 暂时未完成)
- mysql日期和字符串相互转化
- 内联函数与宏替换的区别
- 基础加强 -zookeeper
- idea
- Spring Struts2 Hibernate
- 解决mysql无法插入中文数据及插入后显示乱码的问题【亲测图文解说!】