How to serialize OpenCV Mat with boost XML archive
I am trying to serialize an object which has a cv::Mat as a member, I've found this SO page but when I try it I get the following error:
usr/include/boost/archive/basic_xml_iarchive.hpp:70:9: error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::serialization::is_wrapper::************) note: template int mpl_::assertion_failed(typename mpl_::assert::type) /usr/include/boost/archive/basic_xml_iarchive.hpp: In member function 'void boost::archive::basic_xml_iarchive::load_override(T&, int) [with T = unsigned char, Archive = boost::archive::xml_iarchive]':'
In the example on the linked page they use a binary archive, whereas I'm using an xml one, could this be causing the problem?
需要在包含的头文件中添加这个地方:
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/60648.html
上一篇: 类从这里实例化错误c ++