【问题标题】:How to write comments in the YAML programmatically in OpenCV?如何在 OpenCV 中以编程方式在 YAML 中编写注释?
【发布时间】:2015-12-04 23:09:22
【问题描述】:

我通常将 cmets 放在 YAML 中,以便读者快速了解 YAML 参数。

%CommentC: "~~~~~~~~~~~~~~~~~~~Filtering Setting~~~~~~~~~~~~~~~~~~~"
WindowSize: 3
Sigma: 3
LowerThreshold: 25
HigherThreshold: 35 

但是如何在 OpenCV 中使用 FileStorage 以编程方式编写评论?

【问题讨论】:

  • 您现在如何编写 YAML 文件?手动(使用例如std::ostream 和输出运算符<<)?使用一些库?
  • 我将 cv::FileStorage 与
  • 所以你正在做类似someFileStorage << "WindowSize: " << theWindowSize << '\n'的事情?然后用同样的方式写 cmets:someFileStorage << "%CommentC: \"~~~~~~~~~~~~~~~~~~~Filtering Setting~~~~~~~~~~~~~~~~~~~\"\n";
  • 返回错误。这就是我问这个问题的原因。可能有一些官方的方式来写 YAML 注释。
  • 您能否编辑您的问题以包含错误(完整且未编辑)以及导致错误的代码?

标签: c++ opencv comments yaml


【解决方案1】:

您可以使用该功能:

/* writes a comment */
CVAPI(void) cvWriteComment( CvFileStorage* fs, const char* comment, int eol_comment );

这是工作示例:

#include <opencv2\opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    {
        FileStorage fs("test.yml", FileStorage::WRITE);

        cvWriteComment(*fs, "a double value", 0);
        fs << "dbl" << 2.0;

        cvWriteComment(*fs, "a\nvery\nimportant\nstring", 0);
        fs << "str" << "Multiline comments work, too!";
    }

    {
        double d;
        string s;
        FileStorage fs("test.yml", FileStorage::READ);
        fs["dbl"] >> d;
        fs["str"] >> s;
    }

    return 0;
}

test.yml 文件:

%YAML:1.0
# a double value
dbl: 2.
# a
# very
# important
# string
str: "Multiline comments work, too!"

【讨论】:

  • 非常感谢!祝你有美好的一天!
  • 对不起,我忘了! :)
  • OpenCV-3.0 中是否仍然存在此功能?
【解决方案2】:

我们在新版OpenCV(3.2或更高版本)中使用FileStorage写评论的方式与之前的版本有点不同。这是函数:

void cv::FileStorage::writeComment(const String &   comment, bool  append = false)  

一个例子:

#include <opencv2/core.hpp>
#include <iostream>
#include <string>

using namespace cv;
using namespace std;

int main()
{
    {
        FileStorage fs("test.yml", FileStorage::WRITE);

        fs.writeComment("a double value", 0);
        fs << "dbl" << 2.0;

        fs.writeComment("a\nvery\nimportant\nstring", 0);
        fs << "str" << "Multiline comments work, too!";
    }

    {
        double d;
        string s;
        FileStorage fs("test.yml", FileStorage::READ);
        fs["dbl"] >> d;
        fs["str"] >> s;
    }

    return 0;
}

结果(test.yml):

%YAML:1.0
---
# a double value
dbl: 2.
# a
# very
# important
# string
str: "Multiline comments work, too!"

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    相关资源
    最近更新 更多