C ++ boost :: archive :: archive_exception:序列化OpenCV对象时输入流错误

时间:2016-03-12 12:30:40

标签: c++ opencv serialization boost zeromq

您好我已经使用text_oarchive将序列化的opencv mat对象发送到另一台计算机,这台计算机运行良好后我发现它很慢我想改成binary_oarchive这给了我错误我已经在标题中写道

这是我的头文件

#include <iostream>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/vector.hpp>

BOOST_SERIALIZATION_SPLIT_FREE(cv::Mat)
namespace boost {
    namespace serialization {

        /*** Mat ***/
        template<class Archive>
        void save(Archive & ar, const cv::Mat& m, const unsigned int version)
        {
            size_t elemSize = m.elemSize(), elemType = m.type();

            ar & m.cols;
            ar & m.rows;
            ar & elemSize;
            ar & elemType; // element type.
            size_t dataSize = m.cols * m.rows * m.elemSize();

            //cout << "Writing matrix data rows, cols, elemSize, type, datasize: (" << m.rows << "," << m.cols << "," << m.elemSize() << "," << m.type() << "," << dataSize << ")" << endl;

            for (size_t dc = 0; dc < dataSize; ++dc) {
                ar & m.data[dc];
            }
        }

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

            ar & cols;
            ar & rows;
            ar & elemSize;
            ar & elemType;

            m.create(rows, cols, elemType);
            size_t dataSize = m.cols * m.rows * elemSize;

            //cout << "reading matrix data rows, cols, elemSize, type, datasize: (" << m.rows << "," << m.cols << "," << m.elemSize() << "," << m.type() << "," << dataSize << ")" << endl;

            for (size_t dc = 0; dc < dataSize; ++dc) {
                ar & m.data[dc];
            }
        }

    }
}

这是我的加载功能

void load(cv::Mat & mat, const char * data_str)
{
    std::stringstream ss;
    ss << data_str;

    boost::archive::binary_iarchive tia(ss);
    tia >> mat;
}

这是我的保存功能

std::string save(const cv::Mat &mat) {
    std::ostringstream oss;
    boost::archive::binary_oarchive toa(oss);
    toa << mat;

    return oss.str().data();
}

0 个答案:

没有答案