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?
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!"
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!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With