在不知道矩阵名称的情况下读取YML

Read YML in OpenCV without knowing the matrix name

本文关键字:情况下 读取 YML 不知道      更新时间:2023-10-16

是否可能在OpenCV中YML文件并在文件中检索第一个矩阵而不知道其名称?我这样做是这样做的:

cv::FileStorage fs("foo.yml", cv::FileStorage::READ);
cv::Mat bar;
fs["A"] >> bar;

如何在不知道a命名的情况下实现这一目标?

我对不手动解析文件并弄清楚名称的解决方案感兴趣。

如果您知道YML的结构,则可以使用FileNode导航,然后检索元素:

#include <opencv2opencv.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;
}
相关文章: