title: OpenCV cv::Mat to std::ifstream for base64 encoding
body: <p>To be honest I'm suprised nobody has run into this thus far.
  I'm loading a picture from OpenCV into <strong>cv::Mat</strong>, which I want to base64 encode before I send it over a socket.</p>
  
  <p>For base64 I am using <a href="http://libb64.sourceforge.net/" rel="noreferrer">libb64</a> as it is native to Debian/Ubuntu, and easy to use and very fast. The encoding function takes as a parameter an <strong>std::ifstream</strong>, and outputs an <strong>std::ofstream</strong>.</p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;b64/encode.h&gt;
  #include &lt;fstream&gt;
  
  using namespace cv;
  Mat image;
  image = imread( "picture.jpg", CV_LOAD_IMAGE_COLOR );
  
  if ( image.data )
  {
      std::ifstream instream( ???, std::ios_base::in | std::ios_base::binary);
      std::ofstream outstream;        
  
      // Convert Matrix to ifstream
      // ...
  
      base64::encoder E;
      E.encode( instream, outstream );
  
      // Now put it in a string, and send it over a socket...
  }
  </code></pre>
  
  <p>I don't really know how to populate the instream from the cv::Mat.
  Googling around, I found that I can iterate a cv::Mat, by columns and rows, and get each (pixel I am assuming) RGB values:</p>
  
  <pre><code>for ( int j = 0; j &lt; image.rows; j++ )
  {
      for ( int i = 0; i &lt; image.cols; i++ )
      {
          unsigned char b = input [ image.step * j + i ] ;
          unsigned char g = input [ image.step * j + i + 1 ];
          unsigned char r = input [ image.step * j + i + 2 ];
      }
  }
  </code></pre>
  
  <p>Is this the right way of going on about it? Is there some more elegant way?</p>