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 OpenCV matrix, and they need to be sent over inter-process communication and saved into files to be processed on multiple computers. I’d like to serialize them in an endian/architecture-independent, space-efficient way, as quickly as possible. cv::imencode here would be perfect, except that it only works on 8-bit and 16-bit elements, and we don’t want to lose any precision. The files don’t need to be human-readable (although we do that now to ensure data portability, and it’s incredibly slow). Are there best practices for this, or elegant ways to do it?
Thanks!
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.
Now, mat can be serialized and deserialized as following:
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.