title: Optimization of conversion from opencv mat/Array to to OnnxRuntime Tensor?
body: <p>I am using the ONNXRuntime to inference a UNet model and as a part of preprocessing I have to convert an EMGU OpenCV matrix to OnnxRuntime.Tensor.</p>
  <p>I achieved it using two nested for loops which is unfortunately quite slow:</p>
  <pre><code>            var data = new DenseTensor&lt;float&gt;(new[] { 1, 3, WIDTH, HEIGHT});
  
              for (int y = 0; y &lt; HEIGHT; y++)
              {
                  for (int x = 0; x &lt; WIDTH; x++)
                  {
                      data[0, 0, x, y] = image.GetValue(2, y, x)/255.0;
                      data[0, 1, x, y] = image.GetValue(1, y, x)/255.0;
                      data[0, 2, x, y] = image.GetValue(0, y, x)/255.0;
                  }
              } 
  </code></pre>
  <p>Then I found out that there exists a method which converts Array to DenseTensor. I wanted to use this method as follows:</p>
  <pre><code>        var imgToPredictFloat = new Mat(image.Height, image.Width, DepthType.Cv32F, 3);
          image.ConvertTo(imgToPredictFloat, DepthType.Cv32F, 1/255.0);
          CvInvoke.CvtColor(imgToPredictFloat, imgToPredictFloat, ColorConversion.Bgra2Rgb);
  
          var data = image.GetData().ToTensor&lt;float&gt;;
          var reshaped = data.Reshape(new int[] { 1, 3, WIDTH, HEIGHT});
  </code></pre>
  <p>This would greatly improve the performance however the layout of the output tensor is not correct (the same as from the for loop) and the model obviously won't work. Any suggestions how to reshape the array to the correct layout?</p>
  <p>In the code is also performed converting int 0-255 to float 0-1 and BGR layout to RGB layout.</p>