imageToFloatBuffer static method

Float64List imageToFloatBuffer(
  1. Image image,
  2. List<double> mean,
  3. List<double> std, {
  4. bool contiguous = true,
})

Implementation

static Float64List imageToFloatBuffer(
    Image image, List<double> mean, List<double> std,
    {bool contiguous = true}) {
  var bytes = Float64List(1 * image.height * image.width * 3);
  var buffer = Float64List.view(bytes.buffer);

  if (contiguous) {
    int offsetG = image.height * image.width;
    int offsetB = 2 * image.height * image.width;
    int i = 0;
    for (var y = 0; y < image.height; y++) {
      for (var x = 0; x < image.width; x++) {
        Pixel pixel = image.getPixel(x, y);
        buffer[i] = ((pixel.r / 255) - mean[0]) / std[0];
        buffer[offsetG + i] = ((pixel.g / 255) - mean[1]) / std[1];
        buffer[offsetB + i] = ((pixel.b / 255) - mean[2]) / std[2];
        i++;
      }
    }
  } else {
    int i = 0;
    for (var y = 0; y < image.height; y++) {
      for (var x = 0; x < image.width; x++) {
        Pixel pixel = image.getPixel(x, y);
        buffer[i++] = ((pixel.r / 255) - mean[0]) / std[0];
        buffer[i++] = ((pixel.g / 255) - mean[1]) / std[1];
        buffer[i++] = ((pixel.b / 255) - mean[2]) / std[2];
      }
    }
  }

  return bytes;
}