I have the following members of a class (v1 & v2) that I need to serialize to file using boost serialization and then read back in at a later stage.
The first one is a vector of a vector of an open cv type Point3f, and the other is a vector of a vector of type opencv Vec6f as can be seen below.
#include this in the class header
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
//private member variables
std::vector< std::vector<cv::Point3f> > v1;
std::vector< std::vector<cv::Vec6f> > v2;
// Allow serialization to access non-public data members.
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & v1;
ar & v2;
}
I have this defined at the top of my header file so that it can understand how to serialize the point3f and Vec6f types. Is this correct way? I can save the data but when I go to read it all back in, it has the right number of array of vectors but each entry has values of 0. What am I doing wrong? I presume it's do do with the serialize functions themselves but not sure.
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive &ar, cv::Point3_<float> pt3, const unsigned int)
{
ar & pt3.x;
ar & pt3.y;
ar & pt3.z;
}
template<class Archive>
void serialize(Archive &ar, cv::Vec<float,6> vec6, const unsigned int)
{
ar & vec6[0];
ar & vec6[1];
ar & vec6[2];
ar & vec6[3];
ar & vec6[4];
ar & vec6[5];
}
}
}
I use this code to write
std::ofstream ofs1("v1test.bin");
boost::archive::binary_oarchive oa1(ofs1);
oa1 << v1;
and I use this code to read
std::ifstream ifs1("v1test.bin");
boost::archive::binary_iarchive ia1(ifs1);
std::vector< std::vector<cv::Point3f> > test;
ia1 >> test; // this contains the right structure but all values are 0
Any help or points in the right direction would be appreciated
I figured it out - stupid mistake - forgot to add a reference to the variables when passing them in - should be this
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive &ar, cv::Point3_<float> &pt3, const unsigned int)
{
ar & pt3.x;
ar & pt3.y;
ar & pt3.z;
}
template<class Archive>
void serialize(Archive &ar, cv::Vec<float,6> &vec6, const unsigned int)
{
ar & vec6[0];
ar & vec6[1];
ar & vec6[2];
ar & vec6[3];
ar & vec6[4];
ar & vec6[5];
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.