gpt4 book ai didi

c++ - openCV 存储文件错误 :

转载 作者:太空狗 更新时间:2023-10-29 21:48:36 27 4
gpt4 key购买 nike

我正在尝试从 .yaml 文件加载矩阵,但 opencv 给我以下错误:

OpenCV Error: Parsing Error (myFile.yaml(1): valid xml should start with ') OpenCV Error: Parsing Error (myFile.yaml(1): Tag should start with '<'> in unknown function)

这是我的写入存储文件,工作正常:

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 文档在 how to write 上有一个示例非常清楚的 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;
}

还有另一个例子显示how to read .

关于c++ - openCV 存储文件错误 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10321780/

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