Convert bitmap to row-major [C, H, W] float tensor, normalized to [0, 1].
| 54 | |
| 55 | // Convert bitmap to row-major [C, H, W] float tensor, normalized to [0, 1]. |
| 56 | std::vector<float> BitmapToInputTensor(const Bitmap& bitmap) { |
| 57 | THROW_CHECK(bitmap.IsRGB()); |
| 58 | |
| 59 | const int width = bitmap.Width(); |
| 60 | const int height = bitmap.Height(); |
| 61 | const int pitch = bitmap.Pitch(); |
| 62 | const int num_pixels = width * height; |
| 63 | |
| 64 | std::vector<float> input(num_pixels * 3); |
| 65 | const std::vector<uint8_t>& data = bitmap.RowMajorData(); |
| 66 | for (int y = 0; y < height; ++y) { |
| 67 | for (int x = 0; x < width; ++x) { |
| 68 | for (int c = 0; c < 3; ++c) { |
| 69 | constexpr float kImageNormalization = 1.0f / 255.0f; |
| 70 | input[c * num_pixels + y * width + x] = |
| 71 | kImageNormalization * data[y * pitch + 3 * x + c]; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return input; |
| 77 | } |
| 78 | |
| 79 | // Pads image dimensions to be divisible by a given factor. |
| 80 | struct InputPadder { |