如何使用boost XML存档序列化OpenCV Mat

时间:2013-04-20 21:52:22

标签: c++ opencv serialization boost

我正在尝试序列化一个有cv :: Mat作为成员的对象,我发现这个SO page但是当我尝试它时,我收到以下错误:

  

usr / include / boost / archive / basic_xml_iarchive.hpp:70:9:错误:否   匹配函数用于调用   “assertion_failed(MPL _ ::失败************   提高::系列化:: is_wrapper :: ************)   注意:template int mpl _ :: assertion_failed(typename mpl _ :: assert :: type)   /usr/include/boost/archive/basic_xml_iarchive.hpp:在成员函数'void boost :: archive :: basic_xml_iarchive :: load_override(T&,int)[with T = unsigned char,Archive = boost :: archive :: xml_iarchive ] ':'

在链接页面上的示例中,他们使用二进制存档,而我使用的是xml,这是否会导致问题?

1 个答案:

答案 0 :(得分:4)

需要在包含的标题中添加此内容:

namespace boost {
  namespace serialization {


    template<class Archive>
    inline void serialize(Archive & ar, cv::Mat& m, const unsigned int version) {
      int cols = m.cols;
      int rows = m.rows;
      size_t elemSize = m.elemSize();
      size_t elemType = m.type();

      ar & BOOST_SERIALIZATION_NVP(cols);
      ar & BOOST_SERIALIZATION_NVP(rows);
      ar & BOOST_SERIALIZATION_NVP(elemSize);
      ar & BOOST_SERIALIZATION_NVP(elemType); // element type.

      if(m.type() != elemType || m.rows != rows || m.cols != cols) {
        m = cv::Mat(rows, cols, elemType, cv::Scalar(0));
      }

      size_t dataSize = cols * rows * elemSize;
      cout << " datasize is " << dataSize;


      for (size_t dc = 0; dc < dataSize; dc++) {
        std::stringstream ss;
        ss << "elem_"<<dc;
        ar & boost::serialization::make_nvp(ss.str().c_str(), m.data[dc]);
      }

    }
  }
}