zoukankan      html  css  js  c++  java
  • OPENCV(3) —— 对XML和YAML文件实现I/O 操作

    XMLYAML文件在OpenCV中的数据结构为FileStorage

    string filename = "I.xml";
    FileStorage fs(filename, FileStorage::WRITE);
    \...
    fs.open(filename, FileStorage::READ);
    fs.release();

    写入文件使用  <<  运算符 ,读取文件,使用 >> 运算符

    fs << "iterationNr" << 100;
    int itNr;
    fs["iterationNr"] >> itNr;
    itNr = (int) fs["iterationNr"];

    OpenCV 数据结构的输入和输出

    Mat R = Mat_<uchar >::eye (3, 3),
    T = Mat_<double>::zeros(3, 1);
    fs << "R" << R; // Write cv::Mat
    fs << "T" << T;
    fs["R"] >> R; // Read cv::Mat
    fs["T"] >> T;

    vector要注意在第一个元素前加上“[”,在最后一个元素前加上"]"

    fs << "strings" << "["; // text - string sequence
    fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
    fs << "]"; // close sequence

    对于map结构的操作使用的符号是"{"和"}"

    fs << "Mapping"; // text - mapping
    fs << "{" << "One" << 1;
    fs << "Two" << 2 << "}";

    读取这些结构的时候,会用到FileNode和FileNodeIterator数据结构。对FileStorage类的[]操作符会返回FileNode数据类型,对于一连串的node,可以使用FileNodeIterator结构

    FileNode n = fs["strings"]; // Read string sequence - Get node
    if (n.type() != FileNode::SEQ)
    {
    cerr << "strings is not a sequence! FAIL" << endl;
    return 1;
    }
    FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
    for (; it != it_end; ++it)
    cout << (string)*it << endl;
    <?xml version="1.0"?>
    <opencv_storage>
    <depthImg190 type_id="opencv-image">
    <width>320</width>
    <height>240</height>
    <origin>top-left</origin>
    <layout>interleaved</layout>
    <dt>w</dt>
    <data>
    0 0 0 0 27120 27384 27120 27120 27384 27120 27120 27120 27120 27384
    27384 27664 27664 27944 27944 27664 27664 27944 27944 27944 28224
    27944 27944 28224 28224 28224 28224 28520 28816 29120 29120 29120
    29120 29120 29120 29120 29432 29744 30072 30072 29744 29744 30072
    30072 30072 30400 30400 30736 30736 31080 31080 31080 31440 31440
    31440 31440 31800 31800 31800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 27120 27120 27120 27120 27384 27384 27384 27384 27384 27384
    </depthImg190>
    </opencv_storage>

    这种格式不能直接使用xml,而需要

    IplImage* depth=(IplImage*)cvLoad("depthImg190.xml");

    cvLoad 函数

    const char* filename = "zhang.jpg";
    
        std::ifstream file(filename);
        std::vector<char> data;
    
        file >> std::noskipws;    // noskipws 不忽略空白
        std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(data));
    
    
        cv::Mat matrixJprg = cv::imdecode(cv::Mat(data), 1);
        IplImage qImg;
        qImg = IplImage(matrixJprg); // cv::Mat -> IplImage 可以直接强制转换
    
        cvSaveImage("./out.jpg", &qImg);

    cvSaveImage 直接把 cv::Mat 数据保存为图片会出现问题,需要以上的代码强制转换

  • 相关阅读:
    存在和本质
    数据库的日志机制
    【msql】关于redo 和 undo log
    乐观锁是基于比较的无锁并发控制机制
    两段锁协议和防止死锁的一次封锁法
    并发编程沉思录
    什么是B-Tree
    二叉树与b树的性能区别:计算、层级与io
    认知模型
    复杂性、认知与心理学
  • 原文地址:https://www.cnblogs.com/sprint1989/p/4065888.html
Copyright © 2011-2022 走看看