【问题标题】:Opencv: how write a sequence of Mat in an xml using FileStorageOpencv:如何使用 FileStorage 在 xml 中写入 Mat 序列
【发布时间】:2015-01-23 10:58:54
【问题描述】:

我正在尝试将一系列 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。

【问题讨论】:

    标签: c++ xml opencv mat file-storage


    【解决方案1】:

    我建议你使用字节数组。 Here 是如何将 cv::Mat 转换为字节数组的一个很好的例子。

    【讨论】:

      【解决方案2】:

      您应该尝试sequences/unnamed collection,例如link。下面的代码虽然没有使用它。

      #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();
      

      【讨论】:

        猜你喜欢
        • 2013-04-14
        • 2014-07-03
        • 2012-03-28
        • 2013-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多