打开CV存储文件错误:
openCV Storagefile Error :
我正在尝试从.yaml文件加载矩阵,但opencv给了我以下错误:
OpenCV 错误:解析错误 (myFile.yaml(1):有效的 xml 应以 ') 开头 OpenCV 错误:解析错误(myFile.yaml(1):在未知函数中,标签应以"<"开头>)
这是我的写入存储文件,工作正常:
cv::FileStorage fs("myFile.yaml", cv::FileStorage::APPEND);
while(counter<_imgPtrVector.size()){
unsigned char* _pointer=(unsigned char*)_imgPtrVector.at(counter);
cv::Mat _matrixImage(cv::Size( width,height), CV_8UC1,_pointer , cv::Mat::AUTO_STEP);
fs <<"Matrix"<<_matrixImage;
counter++;
}
但是当我想从同一文件加载数据时,我遇到了这些错误; 这是从存储文件中读取的代码:
cv::FileStorage f("myFile.yaml", cv::FileStorage::READ );
cv::Mat mat(cv::Size( width,height), CV_8UC1);
if(f.isOpened()){
cv::FileNode n = f["Matrix"];
if (n.type() != cv::FileNode::SEQ){
std::cout << "error!";
}
f["Matrix"] >> mat;
}
问题可能是您总是附加到现有文件。因此,您可能需要将代码更改为:
FileStorage fs("test.yml", FileStorage::WRITE);
这将在每次程序运行时重新创建文件。
OpenCV 文档有一个关于如何使用 XML/YAML 持久性编写的示例,非常清楚:
#include "opencv2/opencv.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
FileStorage fs("test.yml", FileStorage::WRITE);
fs << "frameCount" << 5;
time_t rawtime; time(&rawtime);
fs << "calibrationDate" << asctime(localtime(&rawtime));
Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
fs << "features" << "[";
for( int i = 0; i < 3; i++ )
{
int x = rand() % 640;
int y = rand() % 480;
uchar lbp = rand() % 256;
fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
for( int j = 0; j < 8; j++ )
fs << ((lbp >> j) & 1);
fs << "]" << "}";
}
fs << "]";
fs.release();
return 0;
}
还有另一个例子展示了如何阅读。
相关文章:
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- 奇怪的Arduino C++编译错误取决于文件位置
- 使用 C++ iOS::ate 获取错误的文件大小
- 张量流错误 此文件需要编译器和库支持 ISO C++ 2011 标准
- 在visual Studio中构建代码时,我遇到错误,.h文件丢失
- 分段错误:从文件中访问csv记录时转储了核心
- 来自提升的分段错误(提升:文件系统:存在)
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- Visual Studio控制台调试了错误的文件
- std :: ofstream,覆盖错误的文件行
- 成功使用CMakeLists制作后如何修复"open: No such file or directory"错误(该文件实际上存在)
- cmake 错误 当文件由EXEC_PROGRAM生成时找不到源文件
- 提升 ASIO 绑定:错误的文件描述符
- Xcode 8.3.2 中的错误警告:文件的非可移植路径 - 指定的路径与磁盘上的文件名不同
- 链接的错误/生成文件
- 存储文件中的字符串,但是数组仍然为空?阵列大小的循环
- 存储文件c++中的集合
- 在使用 Dev-C++5.11 的C++程序中编译了错误的文件
- 打开CV存储文件错误: