Opencv:如何使用FileStorage在xml中编写Mat序列

Opencv: how write a sequence of Mat in an xml using FileStorage

本文关键字:Mat 序列 xml 何使用 FileStorage Opencv      更新时间:2023-10-16

我正在尝试将一系列 Mat 元素存储在 xml 文件中。这是我的代码草图

Mat SEQ[3];
int nFrame = 0;
while (1) {
    ...
    ...
    SEQ[nFrame] = dataAt_nFrame;
    if (nFrame == 2) break;
    }
FileStorage fs("test.xml", FileStorage::WRITE);
fs << "dataSequence" << SEQ;
fs.release();
cvReleaseCapture(&video1);
FileStorage fs2("test.xml", FileStorage::READ);
Mat SEQ2[3];
fs2["sequence"] >> SEQ2;
//.... here i want print out the values in order to check if are the same i've written...
fs2.release();

while(1) 分析一个视频, 对于每一帧,我得到一个 "dataAt_nFrame",这是一个 Mat。我想将这些数据的整个序列存储在一个数组 SEQ 中(如果您可以建议我更喜欢 Mat [] 类型的替代方案),然后能够读取它们并为每个帧号选择每个 Mat 。

我建议你使用字节数组。这里有一个关于如何将 cv::Mat 转换为字节数组的很好的例子。

您应该尝试sequences/unnamed collection,例如在链接中。下面的代码不使用它。

#include <string>
std::string toString( int count ) {
    return "frame"+std::to_string(count);
}
int nFrame = 0;
FileStorage fs("test.xml", FileStorage::WRITE);
while (1) {
    //...
    //...
    fs << toString(nFrame) << dataAt_nFrame;
}
//saving number of frames in file
fs << "frameCount" << nFrame;
fs.release();
//....
int count=0;
FileStorage fs2("test.xml", FileStorage::READ);
//reading number of frames from file
fs2["frameCount"] >> count;
std::vector<Mat> SEQ2(count);
while( --count >= 0 ) {
    //reading individual Mat
    fs2[ toString(count) ] >> SEQ2[count];
}
//...
fs2.release();