Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV YAML without knowing node names

Tags:

yaml

opencv

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;
like image 271
JuaniL Avatar asked Oct 23 '25 19:10

JuaniL


2 Answers

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

like image 134
Alvise Avatar answered Oct 27 '25 00:10

Alvise


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.

like image 36
Aurelius Avatar answered Oct 27 '25 02:10

Aurelius