Question

I am storing a map in a yaml file with OpenCV FileStorage, something like this:

cv::FileStorage fs("file.yaml.gz", cv::FileStorage::WRITE);
std::string somekey = ...;
double someval = ...;
fs << "mymap" << "{:" << somekey << someval << "}";

Now, I want to get the data back, but I do not know the keys of the map. I guess the code should be like this:

cv::FileStorage fs("file.yaml.gz", cv::FileStorage::READ);
cv::FileNode fn = fs["mymap"];
// fn has type == cv::FileNode::MAP
// fn has size == 1 element (one pair)
// fn[0] returns also a MAP with 1 element

Is is possible to get all the pairs of that map without the keys? I have checked the cv::FileNodeIterator, but it does not seem to solve this problem. The C interface, CvFileNode, allows to retrieve the values but just after providing the key.

Was it helpful?

Solution

I found the answer thanks to @SchighSchagh.

It is necessary to iterate through the node with a cv::FileNodeIterator and cast it later to a cv::FileNode. The key is then stored as the name of this cv::FileNode:

cv::FileStorage fs("file.yaml.gz", cv::FileStorage::READ);
cv::FileNode fn = fs["mymap"];

for(cv::FileNodeIterator fit = fn.begin(); fit != fn.end(); ++fit)
{
  cv::FileNode item = *fit;
  std::string somekey = item.name();
  double someval = (double)item;
  ...
}

It is important to realize that the notation fit->name() does not work with a cv::FileNodeIterator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top