精华内容
下载资源
问答
  • 参看:opencvyaml和xml文件读写
    展开全文
  • opencv操作yaml文件

    千次阅读 2017-10-29 13:56:07
    官网:YAML:http://www.yaml.org XML :http://www.w3c.org/XML 1.YAML与XML文件的打开和关闭 YAML格式的文件拓展名包括:.yml 和 ....在Opencv中,使用FileStorage进行文件读写。XML文件操作与YAML一样,不过存在一

    官网: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"; // 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);

    FileStorage的文件操作模式一共分为四种:READWRITEAPPENDMEMORY

    文档打开后很关心的一件事就是,进行确认是否成功。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::FileStorage
    fs << "Camera Matrix" << cameraMat;
    fs << "Distortion Coefficients"<<distCoeffes;

    运行结果对比如下:

    C++ C++
    YAML YAML
    XML YAML

    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" << "}" << "]";
     
    • 1
    • 2
    • 3
    • 4
    • "{""{:"输出的结果是不一样的,YAML使用":"后,使输出的文本具有Python的风格,映射集合会按照一行排列,不适用时,按照每个元素与其值单独一行的方法排列。XML使用":"后输出结果会有不同,但基本可以视为把":"忽略。

    YAML { Map1
    YAML {: Map2
    XML { Map3
    XML {: Map4

    // 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;

    输出结果:Output

    • 编程的时候,不在Mapping的"{ }"外加上"[ ]"输出的效果是不一样的,而且在数据读取的时候,加上"[
      ]"
      的Mapping结构会被认为是Mapping结构,否则会出错,以上述的Mappings write代码为例: 对于 fs <<
      "fearures" << "[" << "{" << ... << "}" << "]"
      结构,用上述方法可以读取成功; 对于 fs
      << "features" << "{" << ... << "}"
      结构,用上述方法时就会出错:

    序列集合(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, 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.0
    frameCount: 5
    calibrationDate: "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 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();
    展开全文
  • opencvYAML或XML的操作分为两种操作,操作与所对应的opencv类如下: 输入数据 :FileStorag; 输出数据 :FileStorag ,FileNode ,FileNodeIterator; 因为对YAML与XML的操作都是一样的,只是读取文件时...

    用opencv对YAML或XML的操作分为两种操作,操作与所对应的opencv类如下:

    输入数据 :FileStorag;

    输出数据 :FileStorag ,FileNode ,FileNodeIterator;

            因为对YAML与XML的操作都是一样的,只是读取文件时前者后缀是 .yaml 后者是 .xml,所以在这只举例对YAML操作,输入的数据类型有一般常类型(如int),矩阵,序列(vector),maps和自定义的类。不管是输入还是输出数据都是通过键值的方式,输入时先输入键再输入对应的值,输出时是由键索引值,通过下面代码会容易理解。

    1.输入数据 

    数据有:

    整型 int 100 ;

    矩阵 Mat R = Mat_<uchar>::eye(3, 3),T = Mat_<double>::zeros(3, 1);

    序列 ["image1.jpg" ,"Awesomeness" ,"baboon.jpg"];

    maps  {One:1,Two:2 };

    自定义类 MyData m(1);//类的具体定义在下面代码中给出

    代码:

                
     string filename = "E:\\test.yaml";
     FileStorage fs;
     fs.open(filename,FileStorage::WRITE);
    
                //输入100 
                fs << "iterationNr" << 100;
                //输入序列
                fs << "strings" << "[";                              
                fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
                fs << "]";                                           
               
                //输入maps
                fs << "Mapping";                              
                fs << "{" << "One" << 1;
                fs <<        "Two" << 2 << "}";
               
                //输入矩阵
                fs << "R" << R;                                      
                fs << "T" << T;
               
                //输入自定义类
                fs << "MyData" << m;
    
    fs.release();
    

    2.输出数据

    代码:

     FileStorage fs;
     fs.open(filename, FileStorage::READ);
    
                 int itNr;
                //fs["iterationNr"] >> itNr;
                itNr = (int) fs["iterationNr"];
                cout << itNr;
                if (!fs.isOpened())
                {
                    cerr << "Failed to open " << filename << endl;
    
                    return 1;
                }
                
                //获取节点
                FileNode n = fs["strings"];                         
                if (n.type() != FileNode::SEQ)
                {
                    cerr << "strings is not a sequence! FAIL" << endl;
                    return 1;
                }
                //遍历序列输出各元素
                FileNodeIterator it = n.begin(), it_end = n.end(); 
                for (; it != it_end; ++it)
                    cout << (string)*it << endl;
    
                //遍历maps输出各元素
                n = fs["Mapping"];                                
                cout << "Two  " << (int)(n["Two"]) << "; ";
                cout << "One  " << (int)(n["One"]) << endl << endl;
    
    
                MyData m;
                Mat R, T;
                
                //输出矩阵
                fs["R"] >> R;                                      
                fs["T"] >> T;
                //输出自定义类
                fs["MyData"] >> m;

          当打开文件时,会使用FileStorag类的一个open()函数,这个函数中的第一个参数是文件名路径,第二个参数都以常量形式指定你要对文件进行操作的类型,包括:WRITE, READ 或 APPEND。

    3.完整的代码如下:

    using namespace cv;
    using namespace std;
    
    
    class MyData
    {
    public:
        MyData() : A(0), X(0), id() {}
        explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") {}  
    
        void write(FileStorage& fs) const                        //将类的数据写入到fs
        {
            fs << "{" << "A" << A << "X" << X << "id" << id << "}";
        }
        void read(const FileNode& node)                          //将node的数据读入类中
        {
            A = (int)node["A"];
            X = (double)node["X"];
            id = (string)node["id"];
        }
    public:   // Data Members
        int A;
        double X;
        string id;
    };
    
    //These write and read functions must be defined for the serialization in FileStorage to work
    void write(FileStorage& fs, const std::string&, const MyData& x)
    {
        x.write(fs);//将x类中的数据写入到fs中
    }
    void read(const FileNode& node, MyData& x, const MyData& default_value = MyData())
    {
        if(node.empty())
            x = default_value;
        else
            x.read(node);//将node的数据读入到x类中
    }
    
    // 对<<的重载,将类的属性输出
    ostream& operator<<(ostream& out, const MyData& m)
    {
        out << "{ id = " << m.id << ", ";
        out << "X = " << m.X << ", ";
        out << "A = " << m.A << "}";
        return out;
    }
    
    int main()
    {
    
          string filename = "E:\\test.yaml";
            { //write
                Mat R = Mat_<uchar>::eye(3, 3),
                    T = Mat_<double>::zeros(3, 1);
                MyData m(1);
    
                FileStorage fs(filename, FileStorage::WRITE);
    
                fs << "iterationNr" << 100;
                //输入序列
                fs << "strings" << "[";                              
                fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";
                fs << "]";                                           
               
                //输入maps
                fs << "Mapping";                              
                fs << "{" << "One" << 1;
                fs <<        "Two" << 2 << "}";
               
                //输入矩阵
                fs << "R" << R;                                      
                fs << "T" << T;
               
                //输入自定义类
                fs << "MyData" << m;                                
    
                fs.release();                                       
                cout << "Write Done." << endl;
            }
    
            {//read
                cout << endl << "Reading: " << endl;
                FileStorage fs;
                fs.open(filename, FileStorage::READ);
    
                int itNr;
                //fs["iterationNr"] >> itNr;
                itNr = (int) fs["iterationNr"];
                cout << itNr;
                if (!fs.isOpened())
                {
                    cerr << "Failed to open " << filename << endl;
    
                    return 1;
                }
                
                //获取节点
                FileNode n = fs["strings"];                        
                if (n.type() != FileNode::SEQ)
                {
                    cerr << "strings is not a sequence! FAIL" << endl;
                    return 1;
                }
                //遍历序列输出各元素
                FileNodeIterator it = n.begin(), it_end = n.end(); 
                for (; it != it_end; ++it)
                    cout << (string)*it << endl;
    
                //遍历maps输出各元素
                n = fs["Mapping"];                                
                cout << "Two  " << (int)(n["Two"]) << "; ";
                cout << "One  " << (int)(n["One"]) << endl << endl;
    
    
                MyData m;
                Mat R, T;
                
                //输出矩阵
                fs["R"] >> R;                                     
                fs["T"] >> T;
                //输出自定义类
                fs["MyData"] >> m;                                  
    
                cout << endl
                    << "R = " << R << endl;
                cout << "T = " << T << endl << endl;
                cout << "MyData = " << endl << m << endl << endl;
    
                //Show default behavior for non existing nodes
                cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
                fs["NonExisting"] >> m;
                cout << endl << "NonExisting = " << endl << m << endl;
            }
    
            cout << endl
                << "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl;
    return 0;
    }

    PS:本人是用Qtcreator创建的程序。

     

    展开全文
  • #include "opencv2/opencv.hpp" using namespace cv; int main(int, char** argv) { FileStorage fw("./config.yaml", FileStorage::WRITE); fw [" ; fw ; fw ; fw ; fw ]"; return 0; } ```...
  • 参见博客https://www.cnblogs.com/ChrisCoder/p/10116247.html 作者写

    参见博客https://www.cnblogs.com/ChrisCoder/p/10116247.html

    作者写的很好,感谢辛苦整理

    展开全文
  • ros opencv qt yaml综合运用之滤波

    千次阅读 2018-03-17 18:23:25
    本文是针对前面三篇所写内容: 1、三种方法在ROS中加载Qt库进行GUI设计 2、运用ROS和OpenCV进行简单的图像...首先用catkin_create_pkg命令新建一个ROS工程,然后CMakeLists.txt文件中,依次包含opencv,qt和yaml-c...
  • OpenCV读写YAML/XML文件

    2020-12-21 18:21:00
    YAML格式 OpenCV识别和YAML格式必须带一个头 %YAML:1.0 --- T_camera_lidar: !!opencv-matrix rows: 4 cols: 4 dt: d data: [ -9.9966749413220379e-01, -2.1254345991927007e-02, -1.4599792871676539e-02, -6....
  • 参考文章:Opencv YAML和XML格式文件操作详解 YAML格式的文件拓展名包括:.yml 和 .yaml; XML格式的文件拓展名为: .xml; 在OpenCV中,使用FileStorage进行文件读写。XML文件操作与YAML一样,不过存在一些细小差别...
  • http://docs.opencv.org/doc/tutorials/core/file_input_output_with_xml_yml/file_input_output_with_xml_yml.html#fileinputoutputxmlyaml 转载自地址: http://blog.njuaplusplus.com/file-inpu
  • 刚入坑C++,学习写yaml文件,我仿照已有的一个yaml文件,内容为: ``` %YAML:1.0 --- CameraExtrinsicMat: !!opencv-matrix rows: 4 cols: 4 dt: d data: [ -1.6713176217181935e-02, -1....
  • OpenCV 3 REQUIRED ) include_directories( ${OpenCV_INCLUDE_DIRS} ) add_executable(read_yaml_mat read_yaml_mat_test.cpp) target_link_libraries(read_yaml_mat ${OpenCV_LIBS}) 2.c++主文件(read_yaml_mat_...
  • 如何打印和阅读文本条目文件和OpenCV使用YAML或XML文件?如何为OpenCV做同样的数据结构?为你的数据结构如何做到这一点?使用OpenCV的数据结构等FileStorage,FileNode或FileNodeIterator。 源代码 你可以下载 ...
  • target_link_libraries(node_name ${OpenCV_LIBRARIES}) 配置yaml-cpp库 find_package(yaml-cpp REQUIRED) include_directories(${YAML_CPP_INCLUDE_DIR}) target_link_libraries(node_name yaml-cpp) 配置...
  • OpenCV —数据持久化: FileStorage类的数据存取操作与示例"https://blog.csdn.net/iracer/article/details/51339377 注意:使用FileStorage的前提是yaml文件必须遵循以下格式:%YAML:1.0 fx: 100. fy: 101. ...
  • Opencv YAML和XML格式文件操作详解

    万次阅读 2015-08-14 14:09:52
    本文参考Opencv 2.4.11 documentation整理对YAML和XML文件实现I/O操作的方法。
  • OpenCV中读取YAML文件方法

    千次阅读 2017-09-07 20:14:19
    一般要用到一个甚至多个相机参数,所谓的相机参数,也就是指相机外参、内参、畸变参数等,它们一般以矩阵的形式存在,如果都写在代码里面,一来显得臃肿拖沓,二来也不方便修改参数,OpenCV提供了YAML文件扩展配置的...
  • opencv轻松读写yaml/xml文件

    千次阅读 2018-10-24 15:15:40
    有时候我们需要把参数写入文档,利用opencv库可以轻松实现读写yaml/xml文件,相对于txt文件,参数可以更加直观。(以下部分代码需要编译器支持C++11) 样例1(单个变量) 写文件 #include &amp;amp;lt;opencv...
  • OpenCV XML和YAML文件的文件输入和输出XML和YAML文件的文件输入和输出目标源代码解释XML / YAML文件打开和关闭。文本和数字的输入和输出。OpenCV数据结构的输入/输出。向量(数组)和关联图的输入/输出。读写您自己...
  • 源文件#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include using namespace std;int main( ) { string setting_file("setting.yaml"); cv::FileStorage fs;

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,958
精华内容 1,983
关键字:

opencv读yaml