How to edit / update YAML file in OpenCV?

2019-07-28 03:08发布

问题:

What is the best way to edit/update YAML/YML file in OpenCV ?

回答1:

There is NO DIRECT support for update in YAML in general because it need to rewrite the whole file below the update node, so the reason in OpenCV as well as in yaml-cpp dont support direct edit of node value. So, the work around is re-create/write YAML structure again.



回答2:

It looks like OpenCV has some native ways to read and write YAML. From this SO answer, I found the following "cheat sheet" for the OpenCV C++ interface:

https://code.ros.org/trac/opencv/export/3163/trunk/opencv/doc/opencv_cheatsheet.pdf

A portion of their example to write YAML:

FileStorage fs("test.yml", FileStorage::WRITE);
fs << "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH";

If you're interested in updating an existing YAML file, it seems like the best way is to read the existing file into your own data type, make your changes, and then write the new data to the file.