fromPixels static method
Create a tensor from RGB pixel values (not normalized).
pixels: flat array of RGB values R, G, B, R, G, B, ...
in row-major order, values in range 0, 255
width: image width
height: image height
Returns normalized tensor of shape (1, 3, height, width).
Implementation
static Tensor fromPixels(List<int> pixels, int width, int height) {
assert(pixels.length == width * height * 3);
final data = Float32List(3 * height * width);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
final srcIdx = (y * width + x) * 3;
final dstIdx = y * width + x;
final r = pixels[srcIdx] / 255.0;
final g = pixels[srcIdx + 1] / 255.0;
final b = pixels[srcIdx + 2] / 255.0;
data[0 * height * width + dstIdx] = (r - mean[0]) / std[0];
data[1 * height * width + dstIdx] = (g - mean[1]) / std[1];
data[2 * height * width + dstIdx] = (b - mean[2]) / std[2];
}
}
return Tensor(data, [1, 3, height, width]);
}