opencv

时间:2017-04-22 17:41:18

标签: c++ xml opencv

我一直在尝试在OpenCV中编写一个带有vector<vector<Point>>数据类型的嵌套.xml文件(在C ++上)。 但是,我总是在循环中创建一个额外的标记:

string filename = "test.xml";
FileStorage fs(filename, FileStorage::WRITE);
fs << "data" << "[";


    for (int i = 0; i < myvector.size(); ++i)
    {
         //Write each vector
        fs << "{:" << "vector" << "[:";

        for (int j = 0; j < myvector[i].size(); ++j)
        {
            fs << "{:" << "point";
             //Write each point
            fs << "{" << "x" << myvector[i][j].x << "y" << myvector[i][j].y << "}";  
            fs  << "}"; // close point
        }
        fs << "]" << "}"; // close vector
    }
    fs << "]"; // close data
    fs.release();

结果是:

    <?xml version="1.0"?>
<opencv_storage>
<data>
  <_><vector><_><point>
          <x>162</x>
          <y>30</y></point></_>
      <_><point>
          <x>162</x>
          <y>31</y></point></_>
      <_><point>
          <x>161</x>
          <y>32</y></point></_>
      </vector></_>
  </data>
</opencv_storage>

虽然预期的输出应该是(也需要在循环中更改标签名称)

<?xml version="1.0"?>
<opencv_storage>
  <data>
    <vector>
      <point1>
          <x>162</x>
          <y>30</y>
        </point1>
        <<point2>
          <x>162</x>
          <y>31</y>
        </point2>
        <point3>
          <x>161</x>
          <y>32</y>
        </point3>
      </vector>
    </data>
</opencv_storage>

有关可能导致此问题的原因的任何想法?谢谢!

1 个答案:

答案 0 :(得分:2)

两个序列(“[”)和映射(“{”)都有括号。你可以试试这个:

string filename = "test.xml";
FileStorage fs(filename, FileStorage::WRITE);
fs << "data" << "{";
for (int i = 0; i < myvector.size(); ++i)
    {
         //Write each vector
        fs << "vector" << "{";

        for (int j = 0; j < myvector[i].size(); ++j)
        {
            fs << "point" << "{";
             //Write each point
            fs  << "x" << myvector[i][j].x << "y" << myvector[i][j].y ;  
            fs  << "}"; // close point
        }
        fs << "}"; // close vector
    }
    fs << "}"; // close data
fs.release();

更多信息here