Reputation: 20294
Is it possible YML file in OpenCV and retrieve the first matrix in the file without knowing its name? I usuaully do it like this:
cv::FileStorage fs("foo.yml", cv::FileStorage::READ);
cv::Mat bar;
fs["A"] >> bar;
How to achieve this without knowing that A is named A?
I am interested in a solution that does not manually parse the file and figuring out the name.
Upvotes: 1
Views: 105
Reputation: 41775
If you know the structure of the YML, you can navigate it using FileNode
, and retrieve the elements:
#include <opencv2\opencv.hpp>
int main()
{
cv::Mat1b src(2, 3);
cv::randu(src, 0, 256);
{
// Create a simple YML file
cv::FileStorage fs("test.yml", cv::FileStorage::WRITE);
fs << "foo" << src;
}
// Read the saved data without knowing the name
cv::FileStorage fs("test.yml", cv::FileStorage::READ);
// Get first node
cv::FileNode fn = fs.getFirstTopLevelNode();
// Get its name
cv::String name = fn.name();
// Retrieve data as usual
cv::Mat res;
fs[name] >> res;
// Or directly from the FileNode
cv::Mat res2;
fn >> res2;
return 0;
}
Upvotes: 1