在我问这个问题之前我就知道如何解决这个问题,但我知道如何解决这个问题的唯一原因是因为我也在学习如何在 C++ 中同时执行此操作。在 opencv 的最新更新中如何做到这一点是not stated at all in the documentation。我无法在网上找到任何解决此问题的方法,所以希望你们中那些不使用 C++ 的人能够毫不费力地理解如何在 python 中做到这一点。
这个最小的示例应该足以向您展示该过程的工作原理。实际上,当前用于 opencv 的 python 包装器看起来更像 c++ 版本,您现在直接使用 cv2.FileStorage 而不是 cv2.cv.Save 和 cv2.cv.Load。
python cv2.FileStorage 现在是它自己的文件处理程序,就像它在 C++ 中一样。在 c++ 中,如果您想使用 FileStorage 将文件写入,您可以执行以下操作:
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();
要阅读,您需要执行以下操作:
cv::FileStorage opencv_file("test.xml", cv::FileStorage::READ);
cv::Mat file_matrix;
opencv_file["my_matrix"] >> file_matrix;
opencv_file.release();
在python中,如果你想写你必须做以下事情
#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()
如果你想读取矩阵,那就有点做作了。
# 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示例的输出应该是:
write matrix
[[1 2 3]
[4 5 6]
[7 8 9]]
read matrix
[[1 2 3]
[4 5 6]
[7 8 9]]
XML 看起来像这样:
<?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>