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

我想序列化一个对象,它有一个cv :: Mat作为成员,我发现这个SO页面,但是当我尝试它时,我得到以下错误:

usr / include / boost / archive / basic_xml_iarchive.hpp:70:9:错误:没有匹配函数调用'assertion_failed(mpl _ :: failed ************ boost :: serialization :: 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,这是否会导致问题?


需要在包含的头文件中添加这个地方:

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]);
      }

    }
  }
}
链接地址: http://www.djcxy.com/p/60647.html

上一篇: How to serialize OpenCV Mat with boost XML archive

下一篇: c++