Can I parse a YAML file in OpenCV (using FileStorage library) without knowing the names of the nodes?
For example, the next YAML file:
%YAML:1.0
descriptors1: !!opencv-matrix
rows: 82
cols: 64
dt: f
data: [ ... ]
descriptors2: !!opencv-matrix
rows: 161
cols: 64
dt: f
data: [ ... ]
and parse, node by node (OpenCV Matrix by OpenCV Matrix), without doing:
FileStorage fs;
...
Mat firstMatrix;
Mat secondMatrix;
fs["descriptors1"] >> firstMatrix;
fs["descriptors2"] >> secondMatrix;
To obtain all keys in any node, which have a name, you can do like this:
cv::FileStorage pfs(fileToRead, cv::FileStorage::READ);
cv::FileNode fn = pfs.root();
for (cv::FileNodeIterator fit = fn.begin(); fit != fn.end(); ++fit)
{
cv::FileNode item = *fit;
std::string somekey = item.name();
std::cout << somekey << std::endl;
}
You can also do this recursively by iterating on the file node item
If the matrices are all mapped to the top level (as in your example), there is not a way to access them without knowing the name of their corresponding nodes. FileStorage::operator[] only takes string arguments specifying the name of the node.
A workaround could be to parse the YAML with another method to get the node names, and then access the FileStorage afterward.
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