title: Converting Bitmap to ByteBuffer (float) in Tensorflow-lite Android
body: <p>In tensorflow-lite android demo code for image classification, the images are first converted to  ByteBuffer format for better performance.This conversion from bitmap to floating point format and the subsequent conversion to byte buffer seems to be an expensive operation(loops, bitwise operators, float mem-copy etc).We were trying to implement the same logic with opencv to gain some speed advantage.The following code works without error; but due to some logical error in this conversion, the output of the model(to which this data is fed) seems to be incorrect.The input of the model is supposed to be RGB with data type float[1,197,197,3].</p>
  
  <p>How can we speed up this process of bitmap to byte buffer conversion using opencv (or any other means)? </p>
  
  <p>Standard Bitmap to ByteBuffer Conversion:-</p>
  
  <pre><code>/** Writes Image data into a {@code ByteBuffer}. */
    private void convertBitmapToByteBuffer(Bitmap bitmap) {
      if (imgData == null) {
        return;
      }
      imgData.rewind();
  
  
      bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
  
  
  
      long startTime = SystemClock.uptimeMillis();
  
      // Convert the image to floating point.
      int pixel = 0;
  
      for (int i = 0; i &lt; getImageSizeX(); ++i) {
        for (int j = 0; j &lt; getImageSizeY(); ++j) {
          final int val = intValues[pixel++];
  
          imgData.putFloat(((val&gt;&gt; 16) &amp; 0xFF) / 255.f);
          imgData.putFloat(((val&gt;&gt; 8) &amp; 0xFF) / 255.f);
          imgData.putFloat((val &amp; 0xFF) / 255.f);
        }
      }
  
      long endTime = SystemClock.uptimeMillis();
      Log.d(TAG, "Timecost to put values into ByteBuffer: " + Long.toString(endTime - startTime));
    }
  </code></pre>
  
  <p>OpenCV Bitmap to ByteBuffer :-</p>
  
  <pre><code>    /** Writes Image data into a {@code ByteBuffer}. */
        private void convertBitmapToByteBuffer(Bitmap bitmap) {
          if (imgData == null) {
            return;
          }
          imgData.rewind();
  
  
          bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
  
          long startTime = SystemClock.uptimeMillis();
  
  
          Mat bufmat = new Mat(197,197,CV_8UC3);
          Mat newmat = new Mat(197,197,CV_32FC3);
  
  
          Utils.bitmapToMat(bitmap,bufmat);
          Imgproc.cvtColor(bufmat,bufmat,Imgproc.COLOR_RGBA2RGB);
  
          List&lt;Mat&gt; sp_im = new ArrayList&lt;Mat&gt;(3);
  
  
          Core.split(bufmat,sp_im);
  
          sp_im.get(0).convertTo(sp_im.get(0),CV_32F,1.0/255/0);
          sp_im.get(1).convertTo(sp_im.get(1),CV_32F,1.0/255.0);
          sp_im.get(2).convertTo(sp_im.get(2),CV_32F,1.0/255.0);
  
          Core.merge(sp_im,newmat);
  
  
  
          //bufmat.convertTo(newmat,CV_32FC3,1.0/255.0);
          float buf[] = new float[197*197*3];
  
  
          newmat.get(0,0,buf);
  
          //imgData.wrap(buf).order(ByteOrder.nativeOrder()).getFloat();
          imgData.order(ByteOrder.nativeOrder()).asFloatBuffer().put(buf);
  
  
          long endTime = SystemClock.uptimeMillis();
          Log.d(TAG, "Timecost to put values into ByteBuffer: " + Long.toString(endTime - startTime));
        }
  </code></pre>