使用 opencv 读取带有 type_id opencv-image 的 xml 文件

Posted

技术标签:

【中文标题】使用 opencv 读取带有 type_id opencv-image 的 xml 文件【英文标题】:Read xml file with type_id opencv-image using opencv 【发布时间】:2014-10-17 09:00:12 【问题描述】:

嘿,我在读取带有 type_id“opencv-image”的 xml 文件时尝试了很多搜索,我在这里看到的只是“opencv-matrix”,所有可用的帮助对我来说都是无用的。 请帮助我从 xml 文件中读取图像矩阵。

我在这里粘贴我的 xml 文件的上半部分以获取一些想法。

<?xml version="1.0"?>
<opencv_storage>
<depthImg190 type_id="opencv-image">
<width>320</width>
<height>240</height>
<origin>top-left</origin>
<layout>interleaved</layout>
<dt>w</dt>
<data>
0 0 0 0 27120 27384 27120 27120 27384 27120 27120 27120 27120 27384
27384 27664 27664 27944 27944 27664 27664 27944 27944 27944 28224
27944 27944 28224 28224 28224 28224 28520 28816 29120 29120 29120
29120 29120 29120 29120 29432 29744 30072 30072 29744 29744 30072
30072 30072 30400 30400 30736 30736 31080 31080 31080 31440 31440
31440 31440 31800 31800 31800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 27120 27120 27120 27120 27384 27384 27384 27384 27384 27384
</depthImg190>
</opencv_storage>

我用过代码

FileStorage f;
Mat m;
f.open("temp.xml", FileStorage::READ);
f["depthImg190"] >> m;
f.release();

但我面临一个异常“Opencv Errorr: Bad argument in cv::read, file ........\opencv\modules\core\persistence.cpp, line 5535”。

如有任何帮助,将不胜感激

【问题讨论】:

其中一个问题是,上面的 xml 是序列化的 IplImage ,而不是 cv::Mat。它不仅是“opencv-image”部分,宽度列,高度行等。如果这是遗留数据,那么除了cvLoad(yukk)之外,您将没有其他机会。如果你真的在这个,你应该首先改变那个部分。 @berak 你说的很对,问题是我没有写这些数据。我只是将这个数据集用于我的研究工作,我必须不惜一切代价使用它。我已经完成了所有工作,但是当我在循环中使用 cvLoad 来获取通过 IplImage* depth=(IplImage*)cvLoad(getPath(filename)); 读取的文件时,我被困住了。其中 getPath(filename) 返回 "data\\depth\\depthImg190.xml" ,但 cvLoad 没有正确读取文件但是当我使用 IplImage* depth=(IplImage*)cvLoad("data\\depth\\depthImg190.xml ");它正在正确读取。 这里有什么可以帮助我的,当我将文件作为硬代码字符提供但不能仅使用变量时,我没有得到它是否正常工作。 你可能想打印出变量中的文件名,看看是否相同(可能前面的某些目录丢失了) 正是我所说的,我有变量文件名,它正在使用 cout 打印出“data\\depth\\depthImg190.xml”,但它不是读取文件,但是当我给出硬编码路径时s 参数到 cvLoad,文件没有被读取。 【参考方案1】:

实际上,您可以使用一些文档,例如:http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html

无论如何,您的问题的答案很简单:

FileStorage fs("file.xml", FileStorage::READ);
Mat image;

fs["depthImg190"] >> image;

(...)

fs.release();

应该可以了!

【讨论】:

嘿!我试过这个,但我面临一个错误“Opencv Errorr: Bad argument in cv::read, file ..\..\..\..\opencv\modules\core\persistence.cpp ,第 5535 行“我对 opencv 非常陌生,请您帮我摆脱困境吗?我必须尽快阅读此 xml 以进行研究。谢谢 我需要尽快阅读这个 xml,因为我是 opencv 的新手,请指导我作为一个非常基础的 opencv 学生。谢谢 如果没有成功,那么我请您提供有关您的问题的更多信息,以便我们为您提供帮助:您正在使用的代码和您的 xml 文档的结构。 我正在使用您建议的相同代码,并面临我在上面的评论中添加的异常。并且我的xml文件的整体结构也在帖子中给出。我也在使用“cvLoad”函数来读取 xml CvMat* A1 = (CvMat*)cvLoad("depthImg190.xml");它运行成功,但我是 c++ 和 opencv 的新手,不知道如何从 A1 获取“数据”。无论如何应用断点我已经看到数据正在被读取,现在我只想让这个矩阵使用 imwrite 命令写入图像。你能调查一下吗?

以上是关于使用 opencv 读取带有 type_id opencv-image 的 xml 文件的主要内容,如果未能解决你的问题,请参考以下文章

带有 Gstreamer 流的 OpenCV 具有延迟、缓慢和伪影

无法使用 opencv2.3 imread 读取图像

带有 C++ 的 OpenCV 无法编译

带有 pthread 和互斥锁的 OpenCV

在 Windows 中的 Qt 中配置 OpenCv

使用 OpenCV(C++)读取文件夹中的任何 .jpg 图像 [关闭]