如何使用python从OpenCV 3中的持久XML/YAML文件读取/写入矩阵? [英] How to read/write a matrix from a persistent XML/YAML file in OpenCV 3 with python?
问题描述
我一直在尝试使用anaconda的当前cv2
(我相信实际上是OpenCV 3.x)对持久性文件存储(例如XML)进行读写矩阵.我在网上查看了解决方案,人们引用了这样的方法:
I've been trying to read and write matrices to persistent file storage (eg. XML) with anaconda's current cv2
(which I believe is actually OpenCV 3.x). I looked at the solutions online for this, and people reference something done like this:
object = cv2.cv.Load(file)
object = cv2.cv.Save(file)
源.这不适用于当前的anaconda python cv2
.人们提出了此yaml示例这样的解决方案,但我为什么这个简单功能需要那么多样板代码,我感到困惑,我觉得这不是一个可以接受的解决方案.我想要和旧解决方案一样简单的东西.
source. This does not work on the current anaconda python cv2
. People propose solutions like this yaml example, but I'm confused why so much boiler plate code is needed for this simple functionality, I don't find this an acceptable solution. I want something that is as simple as the old solution.
推荐答案
在问这个问题之前,我知道如何解决这个问题,但是知道如何解决这个问题的唯一原因是因为我也在学习如何同时进行此操作C ++.如何在最新的opencv更新中完成此操作,请参见 文档中没有任何说明 .我无法在网上找到解决此问题的方法,因此希望那些不使用C ++的人可以轻松地理解如何在python中进行此操作.
I knew how to solve this before I asked this, but the only reason I knew how to solve this is because I was also learning how to do this simultaneosly in C++. How this is done in the most recent update of opencv is not stated at all in the documentation. I could not find anywhere online with a solution to this, so hopefully those of you who don't use C++ can understand how to do this in python with out much effort.
这个最小的示例应该足以向您展示该过程的工作方式.实际上,当前用于opencv的python包装器看起来更像是c ++版本,并且您现在直接使用cv2.FileStorage
而不是cv2.cv.Save
和cv2.cv.Load
.
This minimal example should be enough to show you how the process works. Effectively the current python wrapper for opencv looks much more like the c++ version, and you now use cv2.FileStorage
directly instead of cv2.cv.Save
and cv2.cv.Load
.
python cv2.FileStorage
现在是它自己的文件处理程序,就像在C ++中一样.在c ++中,如果您想使用文件存储写入到文件,则可以执行以下操作:
The python cv2.FileStorage
now is its own file handler like it inside C++. In c++ if you wanted to write to a file with FileStorage you would do the following:
cv::FileStorage opencv_file("test.xml", cv::FileStorage::WRITE);
cv::Mat file_matrix;
file_matrix = (cv::Mat_<int>(3, 3) << 1, 2, 3,
3, 4, 6,
7, 8, 9);
opencv_file << "my_matrix" << file_matrix
opencv_file.release();
要阅读,您需要执行以下操作:
And to read you would do the following:
cv::FileStorage opencv_file("test.xml", cv::FileStorage::READ);
cv::Mat file_matrix;
opencv_file["my_matrix"] >> file_matrix;
opencv_file.release();
在python中,如果要编写代码,则必须执行以下操作
In python if you want to write you have to do the following
#notice how its almost exactly the same, imagine cv2 is the namespace for cv
#in C++, only difference is FILE_STORGE_WRITE is exposed directly in cv2
cv_file = cv2.FileStorage("test.xml", cv2.FILE_STORAGE_WRITE)
#creating a random matrix
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("write matrix\n", matrix)
# this corresponds to a key value pair, internally opencv takes your numpy
# object and transforms it into a matrix just like you would do with <<
# in c++
cv_file.write("my_matrix", matrix)
# note you *release* you don't close() a FileStorage object
cv_file.release()
如果您想读取矩阵,则该矩阵会更加人为设计.
If you want to then read the matrix it is a bit more contrived.
# just like before we specify an enum flag, but this time it is
# FILE_STORAGE_READ
cv_file = cv2.FileStorage("test.xml", cv2.FILE_STORAGE_READ)
# for some reason __getattr__ doesn't work for FileStorage object in python
# however in the C++ documentation, getNode, which is also available,
# does the same thing
#note we also have to specify the type to retrieve other wise we only get a
# FileNode object back instead of a matrix
matrix = cv_file.getNode("my_matrix").mat()
print("read matrix\n", matrix)
cv_file.release()
读取和写入python示例的输出应为:
The output of the read and write python examples should be:
write matrix
[[1 2 3]
[4 5 6]
[7 8 9]]
read matrix
[[1 2 3]
[4 5 6]
[7 8 9]]
XML看起来像这样:
And the XML looks like this:
<?xml version="1.0"?>
<opencv_storage>
<my_matrix type_id="opencv-matrix">
<rows>3</rows>
<cols>3</cols>
<dt>i</dt>
<data>
1 2 3 4 5 6 7 8 9</data></my_matrix>
</opencv_storage>
这篇关于如何使用python从OpenCV 3中的持久XML/YAML文件读取/写入矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!