Opencv storage vectors vectors vectors <vector <Point>>

I am using OpenCV 2.4.2 FileStorage and trying to save point vector vectors so that I can read it. What is the best way to do this?

Below I tried, but reading it gives an error: OpenCV Error: Unspecified error (The sequence element is not a numerical scalar) in cvReadRawDataSlice, file /Downloads/OpenCV-2.4.2/modules/core/src/persistence.cpp, line 3367

Preservation:

 bool writeVectorVectorPoint(string fname, vector<vector<Point> > vvP) { FileStorage fs(fname, FileStorage::WRITE); fs << "nV" << static_cast<int>(vvP.size()); for(size_t i = 0; i < vvP.size(); i++) { ostringstream istr; istr << "v" << i; fs << istr.str() << "["; for(size_t jj = 0; jj < vvP.at(i).size(); jj++) { ostringstream jjstr; fs << vvP.at(i).at(jj); } fs << "]"; } fs.release(); return(true); } 

Reading:

 FileStorage fs(argv[2], FileStorage::READ); if(!fs.isOpened()) { cout << "Unable to open: " << argv[2] << endl; return(-1); } int nV = static_cast<int>(fs["nV"]); cout << "nV: " << nV << endl; vector<Point> vPts; for(int ii = 0; ii < nV; ii++) { ostringstream iistr; iistr << "v" << ii; cout << iistr.str() << ": "; fs[iistr.str()] >> vPts; cout << endl; } fs.release(); 

The xml file looks like this:

 <?xml version="1.0"?> <opencv_storage> <nV>4</nV> <v0> <_> 269 490</_> <_> 400 522</_> <_> 331 600</_> <_> 294 610</_></v0> <v1> <_> 537 510</_> <_> 590 458</_> <_> 603 612</_> <_> 508 626</_></v1> <v2> <_> 594 287</_> <_> 444 240</_></v2> <v3> <_> 451 330</_> <_> 632 342</_> <_> 652 344</_> <_> 470 381</_></v3> </opencv_storage> 
+4
source share
3 answers

At the moment, I solved this by simply not using OpenCV FileStorage. Here is what I did to store the vector of the Point vector, since there are good procedures for printing vectors.

 bool writeVectorVectorPoint(string fname, vector<vector<Point> > vvP) { ofstream fsOut; vector<vector<Point> >::iterator p; fsOut.open(fname.c_str()); if(fsOut.fail()) { cout << "Failed to open " << fname << endl; return(false); } for(p = vvP.begin(); p != vvP.end(); p++) { fsOut << (*p) << endl; } fsOut.close(); return(true); } 

Reading them back is a little trickier, but still not tricky. If anyone sees any optimizations, let me know, as I suspect there is a better (or at least more elegant) solution.

 bool readVectorVectorPoint(string fname, vector<vector<Point> >& vvP) { ifstream fsIn; fsIn.open(fname.c_str()); if(fsIn.fail()) { cout << "Failed to open: " << fname << endl; return(false); } Point pt; vector<Point> vPt; vector<Point>::iterator p; string line; while(getline(fsIn, line)) { cout << line << endl; string ptStr; stringstream s(line.substr(1,line.size()-1)); vPt.clear(); while(getline(s, ptStr, ';')) { stringstream s1(ptStr); string lastStr; getline(s1, lastStr, ','); pt.x = atoi(lastStr.c_str()); getline(s1, lastStr, ','); pt.y = atoi(lastStr.c_str()); vPt.push_back(pt); } vvP.push_back(vPt); } fsIn.close(); return(true); } 
+1
source

A bit late: A similar simpler example with a vector vector:

Write a function, add node each time with a different name (for information only):

 void writeVectorOfVector(FileStorage &fs, string name, vector<vector<Point2f>> &vov) { fs << name; fs << "{"; for (int i = 0; i < vov.size(); i++) { fs << name + "_" + to_string(i); vector<Point2f> tmp = vov[i]; fs << tmp; } fs << "}"; } 

Reading function, use FileNodeIterator to read each vector <x> and return it back to the vector of the vector:

 void readVectorOfVector(FileNode &fns, string name, vector<vector<Point2f>> &vov) { vov.clear(); FileNode fn = fns[name]; if (fn.empty()){ return; } FileNodeIterator current = fn.begin(), it_end = fn.end(); // Go through the node for (; current != it_end; ++current) { vector<Point2f> tmp; FileNode item = *current; item >> tmp; vov.push_back(tmp); } } 

Tested with OpenCV 2.4.8 / C ++ / Vs2013.

We hope this simplifies your code.

+3
source

in my case, I used this solution to read the vector o KeyPoints and Mat based on bloublo's answer
im using opencv 2.4.9

  FileNodeIterator current = fn.begin(), it_end = fn.end(); // Go through the node for (; current != it_end; ++current) { vector<KeyPoint> kp; Mat desc; FileNode item = *current; read(item,kp); ++current; item = *current; item >> desc; ... } 
0
source

All Articles