gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:16:14 26 4
gpt4 key购买 nike

我用这样的数据编写 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com