c++ - OpenCV FileStorage 如何读取结构 vector ?

标签 c++ opencv yaml

我用这样的数据编写 vector :

string filename= itemName+".yml";

FileStorage fs(filename, FileStorage::WRITE);
fs << "number_of_objects" << (int)vec.size();
fs << "objects" << "[";
for( int i=0; i < (int)vec.size(); ++i )
{
    fs << "{";
    fs << "x" << rc.x/imageScale;
    fs << "y" << rc.y/imageScale;
    fs << "w" << rc.width/imageScale;
    fs << "h" << rc.height/imageScale;
    fs << "}";
}
fs << "]";

但我无法读回它们。

FileStorage fs(filename, FileStorage::READ);

if(!fs.isOpened())
    return false;

int nObjects= 0;
fs["number_of_objects"] >> nObjects;
imageMarkupData.resize(nObjects);
for( int i=0; i < nObjects; ++i )
{
    int x,y,w,h;
    fs["x"] >> x;
    fs["y"] >> y;
    fs["w"] >> w;
    fs["h"] >> h;

    //...
}

什么是正确的方法?

最佳答案

正确的方法是使用 FileNodeFileNodeIterator

    FileStorage fs(filename, FileStorage::READ);

    if(!fs.isOpened())
        return false;

    int nObjects= 0;
    fs["number_of_objects"] >> nObjects;
    vec.resize(nObjects);

    FileNode fn = fs["objects"];
    int id=0;
    for (FileNodeIterator it = fn.begin(); it != fn.end(); it++,id++)
    {
        FileNode item = *it;

        int x,y,w,h;
        item["x"] >> x;
        item["y"] >> y;
        item["w"] >> w;
        item["h"] >> h;
    }

关于c++ - OpenCV FileStorage 如何读取结构 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275341/

相关文章:

c++ - C++ 类可以确定它是在堆栈上还是在堆上?

c++ - 如何在 C++ 中为 LTEXT 控件调用 WM_CTLCOLORSTATIC 消息

c - C 中的 OpenCV - 绘制一个应该只出现在图像上半部分的圆圈?

java - OpenCV Android : how to find all max locations of a Mat

python - PyYaml - 转储带有特殊字符(即重音符号)的 unicode

c++ - 寻找最大和正整数子数组

c++ - 无法使用 SFML 和 SDL 加载图像?

opencv - 光流中的像素对应

yaml - 如何在 Build Yaml 中进行 dotnet 发布

php - 将 GitLab CI 变量写入文件