using System.Drawing;
using System.Drawing.Imaging;
public static DenseTensor<float> ImageToTensor(Bitmap image)
{
// Resize to model input size
var resized = new Bitmap(image, new Size(224, 224));
// Create tensor in CHW format (channels, height, width)
var tensor = new DenseTensor<float>(new[] { 1, 3, 224, 224 });
// Mean and std for normalization (ImageNet)
var mean = new[] { 0.485f, 0.456f, 0.406f };
var std = new[] { 0.229f, 0.224f, 0.225f };
for (int y = 0; y < 224; y++)
{
for (int x = 0; x < 224; x++)
{
var pixel = resized.GetPixel(x, y);
// Normalize and set RGB channels
tensor[0, 0, y, x] = (pixel.R / 255f - mean[0]) / std[0];
tensor[0, 1, y, x] = (pixel.G / 255f - mean[1]) / std[1];
tensor[0, 2, y, x] = (pixel.B / 255f - mean[2]) / std[2];
}
}
return tensor;
}
// Usage
var image = new Bitmap("image.jpg");
var tensor = ImageToTensor(image);
var input = NamedOnnxValue.CreateFromTensor("input", tensor);