Serializing OpenCV Mat_

前端 未结 7 1197
刺人心
刺人心 2020-11-30 05:29

I\'m working on a robotics research project where I need to serialize 2D matrices of 3D points: basically each pixel is a 3-vector of floats. These pixels are saved in an Op

7条回答
  •  借酒劲吻你
    2020-11-30 05:52

    Edit: Christoph Heindl has commented on this post with a link to his blog where he has improved on this serialisation code. Highly recommended!

    http://cheind.wordpress.com/2011/12/06/serialization-of-cvmat-objects-using-boost/

    --

    For whoever it may benefit: Some code to serialize Mat& with boost::serialization
    I haven't tested with multi-channel data, but everything should work fine.

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    BOOST_SERIALIZATION_SPLIT_FREE(Mat)
    namespace boost {
    namespace serialization {
    
        /*** Mat ***/
        template
        void save(Archive & ar, const 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
        void load(Archive & ar, 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];
            }
        }
    
    }
    }
    

    Now, mat can be serialized and deserialized as following:

        void saveMat(Mat& m, string filename) {
                ofstream ofs(filename.c_str());
                boost::archive::binary_oarchive oa(ofs);
                //boost::archive::text_oarchive oa(ofs);
                oa << m;
        }
    
        void loadMat(Mat& m, string filename) {
                std::ifstream ifs(filename.c_str());
                boost::archive::binary_iarchive ia(ifs);
                //boost::archive::text_iarchive ia(ifs);
                ia >> m;
        }
    

    I've used the binary_oarchive and binary_iarchive here to keep the memory usage down. The binary format doesn't provide portability between platforms, but if desired the text_oarchive/iarchive can be used.

提交回复
热议问题