Opencv YAML和XML格式文件操作详解
本系列文章由 @YhL_Leo 出品,转载请注明出处。 文章链接: http://www.52php.cn/article/p-eyzsfhvb-qe.html 本文参考Opencv 2.4.11 documentation整理对YAML和XML文件实现I/O操作的方法。 官网:YAML:http://www.yaml.org XML :http://www.w3c.org/XML 1.YAML与XML文件的打开和关闭 YAML格式的文件拓展名包括: 1.1 文件打开 在Opencv中,使用 std::string fileName = "E:test.yml"; // YAML
std::string fileName2 = "E:test.xml"; // XML
// write file
cv::FileStorage fs(fileName,cv::FileStorage::WRITE);
// read file
cv::FileStorage fs2(fileName,cv::FileStorage::READ);
// or use: cv::FileStorage::open
fs2.open(fileName,cv::FileStorage::READ);
文档打开后很关心的一件事就是,进行确认是否成功。 // bool FileStorage::isOpened() const;
if ( !fs.isOpened() ) // failed
{
std::cout<<"Save File Failed!"<<std::endl;
return ;
}
else // succeed
{
...
}
1.2 文件关闭
fs.release();
2.文件读写
2.1 写入 fs << "frameCount" << 5; // 字符和数字
cv::Mat_<double> cameraMat = cv::Mat_<double>::zeros(3,3);
fs << "Camera Intrinsic Matrix" << cameraMat; // cv::Mat
注意:
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++的输入输出方法很接近(链接:常用的三种 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::FileStorage
fs << "Camera Matrix" << cameraMat;
fs << "Distortion Coefficients"<<distCoeffes;
运行结果对比如下:
2.4 集合的操作 Opencv中将集合分为两类:映射和序列。 映射集合(Mappings, 又称named collections):每个元素有一个名字或者说关键字,并且可以通过名字访问其数据,类似于Key-Value结构。使用方法为: // Mappings write
int x(1.0),y(0.0);
fs << "features" << "["; // also can be "[:"
fs <<"{:" << "x" << x << "y" << "}" << "]";
// Mappings read
cv::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;
输出结果:
序列集合(Sequences,又称unnamed collections):数据没有名字名字或者关键字,一般通过序号(indices)访问数据,例如最常见的数组。 与映射类似,序列集合需要在输出开始前加 // Sequences write
int mySeq[5] = {0,1,2,3,4};
fs << "mySeq" << "[";
for ( int idx=0; idx<5; idx++ )
{
fs << mySeq[idx];
}
fs << "]";
// Sequences read
cv::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,1000,240,1);
Mat distCoeffs = (Mat_<double>(5,1) << 0.1,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.0
frameCount: 5
calibrationDate: "Fri Jun 17 14:09:29 2011n"
cameraMatrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 1000.,0.,320.,1000.,240.,1. ]
distCoeffs: !!opencv-matrix
rows: 5
cols: 1
dt: d
data: [ 1.0000000000000001e-01,1.0000000000000000e-02,-1.0000000000000000e-03,0. ]
features:
- { x:167,y:49,lbp:[ 1,1 ] }
- { x:298,y:130,lbp:[ 0,1 ] }
- { x:344,y:158,0 ] }
// file read
FileStorage 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 FileNodeIterator
for( ; 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();
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |