OpenCV FileStorage读取错误

时间:2013-12-09 11:58:01

标签: c++ xml opencv

我正在尝试解决与OpenCV FileStorage相关的问题。 我可以成功地写入和读取yml / xml文件。 这只适用于写入和读取功能在同一程序中的情况。

当我在一个程序中成功创建并编写yml文件并尝试从另一个程序访问该文件时,我遇到了以下问题。

写作部分:

d_img = imread(filename, 1);
FileStorage fs_source("tmp.yaml", FileStorage::WRITE);
if (fs_source.isOpened()){
  fs_source << "source" << d_img;
  fs_source.releaseAndGetString();
  cout<<"Source : image written to temporary file"<<endl;
}
else
{
  fs_source.open("tmp.yaml", FileStorage::WRITE);
  fs_source << "source" << d_img;
  fs_source.releaseAndGetString();
  cout<<"Source : image written to temporary file 2"<<endl;
}

阅读部分:

FileStorage fs_sink("tmp.yaml", FileStorage::READ);
if (fs_sink.isOpened()){
  fs_sink["source"] >> d_img;
  fs_sink.releaseAndGetString();
  cout<<"Sink : image read from file 1"<<endl;
}
else
{
  fs_sink.open("tmp.yaml", FileStorage::READ);
  fs_sink["source"] >> d_img;
  fs_sink.releaseAndGetString();
  cout<<"Sink : image read from file 2"<<endl;
}

imwrite( "output.jpg", d_img );

第一个代码创建yml文件没有任何问题。 但是当第一行FileStorage fs_sink("tmp.yaml", FileStorage::READ);

时,第二个代码会抛出以下错误
OpenCV Error: Parsing error (tmp.yaml(0): Valid XML should start with '<?xml ...?>') in icvXMLParse, file /var/tmp/portage/media-libs/opencv-2.4.7/work/opencv-2.4.7/modules/core/src/persistence.cpp, line 2257
thread[thread-per-block[1]: <block image_sink (2)>]: /var/tmp/portage/media-libs/opencv-2.4.7/work/opencv-2.4.7/modules/core/src/persistence.cpp:2257: error: (-212) tmp.yaml(0): Valid XML should start with '<?xml ...?>' in function icvXMLParse

这很奇怪,因为文件是yml,而不是xml。当我将文件类型更改为xml时,它仍然会出现相同的错误。

什么可能导致此错误?

PS:尝试关闭文件的releaseAndGetString();release();方法。结果相同。 <{1}},xmlyml扩展也都在尝试过。

编辑:如果文件为空,则可以复制。

0 个答案:

没有答案
相关问题