Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write comments in the YAML programmatically in OpenCV?

I usually put comments in the YAML so that the reader could understand YAML parameters quickly.

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

But how can I write the comment programmatically in OpenCV using FileStorage?

like image 658
user1098761 Avatar asked Nov 19 '25 05:11

user1098761


2 Answers

You can use the function:

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

This is working example:

#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;
}

The test.yml file:

%YAML:1.0
# a double value
dbl: 2.
# a
# very
# important
# string
str: "Multiline comments work, too!"
like image 163
Miki Avatar answered Nov 21 '25 19:11

Miki


The way we use FileStorage to write comment in the new version of OpenCV (3.2 or higher) is a bit different from the previous version. This is the function:

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

An example:

#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;
}

The result (test.yml):

%YAML:1.0
---
# a double value
dbl: 2.
# a
# very
# important
# string
str: "Multiline comments work, too!"
like image 27
Erman Avatar answered Nov 21 '25 18:11

Erman