opencv - 使用Filestrage在OpenCV中保存和读取Mat向量的向量

我想在使用opencv的filestorage读写Mats vector 的载体方面寻求帮助。

我用这个函数写:

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 << "]";
}

这读为:
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);
        }
    }
}

编写代码的效果很差,无法编写用于阅读的代码。
我实际上是绝望的,我已经尽力了。我在这里寻找了相同的示例代码,但是没有一个对我没用。
感谢帮助!!!

最佳答案

我终于自己解决了这个问题。

这是代码:

    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);
        }
    }
}

https://stackoverflow.com/questions/14397363/

相关文章:

python - opencv-python:不是一个numpy数组

opencv - 最大颜色数和最大深度

image-processing - 使用 openCV 检测图案区域

opencv - OpenCV Codebook方法-背景模型

opencv - "cache"cv Warp透视图

image-processing - 通过 find_contour 获取每个标志的 1 个轮廓并在

image - GUI,可在ubuntu12.04中使用OpenCV查看图像中的值

python - 用于在 python 中存储多个图像的高效数据结构

c++ - 负 RGB 值

opencv - 在OpenCV中编译