分享

OpenCV 2.4.3 Cheat Sheet学习 数据的输入和输出

 昵称14216904 2016-02-24
// wenjianduxie.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdafx.h"
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream>  


using namespace cv;  
using namespace std;  

int main( int argc, char** argv )

{      //1.    将数据写入YAML(或XML)
//注意,在OpenCV中,无论读写,文件的格式均由指定的后缀名确定。示例:
/*
FileStorage fs("test.yml", FileStorage::WRITE);
fs << "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH";
fs << "mtx" << Mat::eye(3,3,CV_32F);
fs << "mylist" << "[" << CV_PI << "1+1" << "{:" << "month" << 12 << "day" << 31 << "year" << 1969 << "}" << "]";
fs << "mystruct" << "{" << "x" << 1 << "y" << 2 << "width" << 100 << "height" << 200 << "lbp" << "[:";
const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1};
fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));
fs << "]" << "}";
*/

///*
//1.    将数据读回 这段代码把上面写入文件的数据再读回来。
FileStorage fs("test.yml", FileStorage::READ);
int i1 = (int)fs["i"];
double r1 = (double)fs["r"];
string str1 = (string)fs["str"];
Mat M;
fs["mtx"] >> M;
cout<<M<<endl;
FileNode tl = fs["mylist"];
CV_Assert(tl.type() == FileNode::SEQ && tl.size() == 3);
double tl0 = (double)tl[0];
string tl1 = (string)tl[1];
int m = (int)tl[2]["month"], d = (int)tl[2]["day"];
int year = (int)tl[2]["year"];
FileNode tm = fs["mystruct"];
Rect r;
r.x = (int)tm["x"], r.y = (int)tm["y"];
r.width = (int)tm["width"], r.height = (int)tm["height"];
int lbp_val = 0;
FileNodeIterator it = tm["lbp"].begin();
for(int k = 0; k < 8; k++, ++it)
  lbp_val |= ((int)*it) << k;
  //*/

cvWaitKey(0);  

return 0;  
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多