OpenCV文件存储如何读取结构向量
OpenCV FileStorage how to read vector of structures?
我用一些数据写向量:
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;
//...
}
正确的方法是什么?
正确的方法是使用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;
}
相关文章:
- 将结构向量保存到文件中,并从C++文件中读取结构向量
- 从只读内存中读取结构
- 从包含 IP 标头片段的二进制文件中读取结构的最佳方法是什么
- 读取结构化文件
- CPP 在读取结构数据时无限循环错误?
- 打开文件以读取结构数组
- 无效操作数以二进制读取结构的阵列
- 读取结构数组时出错
- 如何从另一个类读取结构成员
- 读取结构 -- uint32 不命名类型
- 从流或字节中读取结构时遇到问题
- 读取结构C++二进制文件的运行时错误
- 从二进制文件读取结构数据时获取垃圾值
- 在 c++ 中向二进制文件写入/读取结构
- 在C++文件中写入和读取结构体的三维矢量
- C++按字段读取结构,并将结构直接写入流
- 从LISP的套接字中读取c++结构
- 读取结构化二进制文件c++
- C++二进制文件正在读取结构
- 从CArchive保存的二进制数据中读取结构数组