圖片銜接自定義類序列化
#include <fstream>
// include headers that implement a archivein simple text format
#include<boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <Eigen/Dense>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;
using namespace cv;
using namespace Eigen;
BOOST_SERIALIZATION_SPLIT_FREE(::cv::Mat)
namespace boost {
namespace serialization {
/** Serialization support for cv::Mat */
template<class Archive>
void save(Archive & ar, const ::cv::Mat& m, const unsigned int version)
{
int elem_size = m.elemSize();
int elem_type = m.type();
ar & m.cols;
ar & m.rows;
ar & elem_size;
ar & elem_type;
const size_t data_size = m.cols * m.rows * elem_size;
ar & boost::serialization::make_array(m.ptr(), data_size);
}
template <class Archive>
void load(Archive & ar, ::cv::Mat& m,const unsigned int version)
{
int cols, rows;
int elem_size, elem_type;
ar & cols;
ar & rows;
ar & elem_size;
ar & elem_type;
m.create(rows, cols, elem_type);
size_t data_size = m.cols * m.rows * elem_size;
ar & boost::serialization::make_array(m.ptr(), data_size);
}
}
}
class Point2D;
class Rect2D;
class Circle2D;
class Arc2D;
class Line2D;
class Angle2D;
class Point2D{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar,const unsigned int version)
{
ar & m_x;
ar & m_y;
}
double m_x;
double m_y;
public:
Point2D(double x,double y):m_x(x),m_y(y){}
Point2D(){}
~Point2D(){}
double x()const{return this->m_x;}
double y()const{return this->m_y;}
Vector2d toVector()const{return Vector2d(m_x,m_y);}
Point2D operator +(Point2D point){return Point2D(point.x()+m_x,point.y()+m_y);}
Point2D operator -(Point2D point){return Point2D(point.x()-m_x,point.y()-m_y);}
Point2D operator *(double value){return Point2D(m_x*value,m_y*value);}
};
class Line2D{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar,const unsigned int version)
{
ar & start_point;
ar & end_point;
}
Point2D start_point;
Point2D end_point;
Vector2d direction;
public:
Line2D(Point2D A,Point2D B):start_point(A),end_point(B){}
Line2D(){}
~Line2D(){}
Point2D get_start(){return this->start_point;}
Point2D get_end(){return this->end_point;}
//double length(){return }
};
class Rect2D{
private:
double m_length;
double m_width;
Point2D m_center;//=Point2D(0,0);
Vector2d direction;
public:
Rect2D(double length,double width,Point2D center,Vector2d direction):m_length(length),m_width(width),m_center(center){}
Rect2D(){}
~Rect2D(){}
double width(){return this->m_width;}
double length(){return this->m_length;}
};
void test()
{
Mat img = imread("/Users/suxianbin/Desktop/ImageToUse/yingjing.jpg");
std::ofstream ofs("matrices.bin", std::ios::out | std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
{ // use scope to ensure archive goes out of scope before stream
Point2D start=Point2D(2,5);
Point2D end=Point2D(6,8);
Line2D line=Line2D(start,end);
//oa<<line;
oa << img <<line;
cout<<"write finish"<<endl;
}
ofs.close();
//std::ofstream ofs("filename.txt");
//boost::archive::text_oarchive oa(ofs);
//oa<<line;
//cout<<"serialization finished..."<<endl;
ofs.close();
Mat imgOut;
ifstream inf("matrices.bin",std::ios::binary);
{
boost::archive::binary_iarchive ia(inf);
Line2D lineout;
//ia>>lineout;
ia >> imgOut>>lineout;
cout<<lineout.get_start().x()<<endl;
cout<<"read finish"<<endl;
}
imshow("img",imgOut);
waitKey(0);
}
int main()
{
test();
return 0;
}