gpt4 book ai didi

OpenCV 存储点向量的向量 vector>

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:28 25 4
gpt4 key购买 nike

我正在使用 OpenCV 2.4.2 FileStorage 并尝试以一种我可以读回的方式存储点向量的向量。执行此操作的最佳方法是什么?

下面是我尝试过的,但是读回它会抛出错误:OpenCV 错误:cvReadRawDataSlice 中的未指定错误(序列元素不是数值标量),文件/Downloads/OpenCV-2.4.2/modules/core/src/persistence.cpp,第 3367 行

保存:

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

阅读:

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

xml 文件如下所示:

<?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>

最佳答案

有点晚了:vector of vector 的一个类似的更简单的例子:

编写函数,每次添加一个不同名称的节点(仅供引用):

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

读取函数,使用 FileNodeIterator 读取每个 vector< x > 并将其推回 vector of 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);
}
}

使用 OpenCV 2.4.8/C++/Vs2013 测试。

希望这能简化您的代码。

关于OpenCV 存储点向量的向量 vector<vector<Point>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com