Search code examples
opencvvectorfile-storage

Save and read vector of vectors of Mat in OpenCV using Filestrage


I'd like to ask for help with reading and writing vector of vectors of Mats using opencv's filestorage.

I use this function to write:

Template<typename _Tp>inline void writeFileNodeList(FileStorage& fs, const string& name,const vector<_Tp>& items) 
{
        // typedefs
        //typedef typename vector<_Tp>::const_iterator constVecIterator;
        vector<Mat>::iterator it;
        // write the elements in item to fs
        fs << name << "[";
        for (it = items.begin(); it != items.end(); ++it) {
            fs << *it;
        }
        fs << "]";
}

and this to read:

template<typename _Tp>inline void readFileNodeList(const FileNode& fn, vector<_Tp>& result) {
    if (fn.type() == FileNode::SEQ) {
        //vector<Mat>::iterator it;
        for (FileNodeIterator it = fn.begin(); it != fn.end();it++) {
            _Tp item;
            it >> item;
            result.push_back(item);
        }
    }
}

Code for writing works badly and that one for reading is not possible to build. I'm actually totally desparete, I have used all I can imagine. I have looked here for same sample codes but none of them didn't worked for me. Thanks for help!!!


Solution

  • I have finally solved the problem by myself.

    Here's the code:

        void writeFileNodeList(FileStorage& fs, const string& name,vector<vector<Mat>> items) 
        {
            int IDs=items.size();
            // typedefs
            fs << name << "{";
            for (int i=0;i<IDs;i++)
            {
                stringstream ss;
                string s;
                string a;
                a="ID-label";
                ss << (i+1);
                s = ss.str();
                a+=s;
    
                fs  << a << "[";
    
                for (int j=0;j<items[i].size();j++)
                {
                    fs<<items[i][j];        
                }   
                fs <<"]";
            }
            fs << "}";
    
    }
    

    and here is the part for reading xml back

    vector<vector<Mat>> readFileNodeList2(const FileNode& fn) 
    {   
        //cout <<fn.name() <<endl;
        //cout <<fn.size() <<endl;
        vector<vector<Mat>> output;
        //cout << fn.isMap() << endl;
    
        for (int ID=0;ID<fn.size();ID++)
        {
            stringstream ss;
            string s;
            string a;
            a="ID-label";
            ss << (ID+1);
            s = ss.str();
            a+=s;
            FileNode temp_ID;
            temp_ID=fn[a];
            vector<Mat> one_person_patrerns;
            readFileNodeList(temp_ID,one_person_patrerns);
            output.push_back(one_person_patrerns);
        }
        return output;
    }
    
    template<typename _Tp>inline void readFileNodeList(const FileNode& fn,vector<_Tp>& result) 
    {
        if (fn.type() == FileNode::SEQ) {
            for (FileNodeIterator it = fn.begin(); it != fn.end();) {
                _Tp item;
                it >> item;
                result.push_back(item);
            }
        }
    }