var xPadding = 0;
var yPadding = 0;
int targetWidth;
int targetHeight;
if (_parameters.ProcessWithOriginalAspectRatio)
{
var xRatio = (float)modelSize.Width / image.Width;
var yRatio = (float)modelSize.Height / image.Height;
var ratio = Math.Min(xRatio, yRatio);
targetWidth = (int)(image.Width * ratio);
targetHeight = (int)(image.Height * ratio);
xPadding = (modelSize.Width - targetWidth) / 2;
yPadding = (modelSize.Height - targetHeight) / 2;
}
else
{
targetWidth = modelSize.Width;
targetHeight = modelSize.Height;
}
image.Mutate(x => x.Resize(targetWidth, targetHeight));
float[] inputArray = new float[image.Width * image.Height * 3];
var TotalPixel = image.Width * image.Height;
Parallel.For(0, image.Height, y =>
{
image.ProcessPixelRows(row =>
{
var pixelSpan = row.GetRowSpan(y);
int rowOffset = y * image.Width * 3;
for (int x = 0; x < image.Width; x++)
{
var R_offset = y * image.Width + x;
var G_offset = TotalPixel + y * image.Height + x;
var B_offset = TotalPixel * 2 + y * image.Width + x;
inputArray[R_offset] = pixelSpan[x].R / 255f;
inputArray[G_offset] = pixelSpan[x].G / 255f;
inputArray[B_offset] = pixelSpan[x].B / 255f;
}
});
});
var dimensions = new int[] { 1, 3, image.Height, image.Width };
var input = new DenseTensor<float>(inputArray, dimensions);
//var dimensions = new int[] { 1, 3, modelSize.Height, modelSize.Width };
//var input = new DenseTensor<float>(dimensions);
//image.ForEachPixel((point, pixel) =>
//{
// var x = point.X + xPadding;
// var y = point.Y + yPadding;
// var r = pixel.R / 255f;
// var g = pixel.G / 255f;
// var b = pixel.B / 255f;
// input[0, 0, y, x] = r;
// input[0, 1, y, x] = g;
// input[0, 2, y, x] = b;
//});
return input;