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
How about just convert your Mat to a vector and use fwrite?
The converting to vector process might affect performance, but it's safe. I suspect that all the answers above, either looping through the image data as in the accepted answer, or using make_array as in Christoph post, assume that your Mat data is contiguous, which is not necessarily the case. When your Mat data is not contiguous, the output from these answers will not be correct.