2016-09-01 6 views
1

Я пишу вектор с некоторыми данными, как это:OpenCV FileStorage как читать вектор структур?

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; 

    //... 
} 

Каков порядок?

ответ

0

Правильный способ был использовать 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; 
    } 
Смежные вопросы