我用这样的数据编写 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;
//...
}
什么是正确的方法?
最佳答案
正确的方法是使用 FileNode
和 FileNodeIterator
。
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/