SAM2 preprocessing: resize + ImageNet normalization. Returns [C, H, W] float, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225].
| 3520 | // SAM2 preprocessing: resize + ImageNet normalization. |
| 3521 | // Returns [C, H, W] float, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]. |
| 3522 | static std::vector<float> sam2_preprocess_image(const sam3_image& image, int img_size) { |
| 3523 | static const float mean[3] = {0.485f, 0.456f, 0.406f}; |
| 3524 | static const float std_d[3] = {0.229f, 0.224f, 0.225f}; |
| 3525 | const int C = 3; |
| 3526 | std::vector<float> result(C * img_size * img_size); |
| 3527 | |
| 3528 | std::vector<uint8_t> resized; |
| 3529 | const uint8_t* pixels = image.data.data(); |
| 3530 | int w = image.width, h = image.height; |
| 3531 | |
| 3532 | if (w != img_size || h != img_size) { |
| 3533 | resized.resize(img_size * img_size * 3); |
| 3534 | sam3_resize_bilinear(pixels, w, h, resized.data(), img_size, img_size); |
| 3535 | pixels = resized.data(); |
| 3536 | } |
| 3537 | |
| 3538 | for (int c = 0; c < C; ++c) { |
| 3539 | for (int y = 0; y < img_size; ++y) { |
| 3540 | for (int x = 0; x < img_size; ++x) { |
| 3541 | float v = pixels[(y * img_size + x) * 3 + c] / 255.0f; |
| 3542 | result[c * img_size * img_size + y * img_size + x] = (v - mean[c]) / std_d[c]; |
| 3543 | } |
| 3544 | } |
| 3545 | } |
| 3546 | |
| 3547 | return result; |
| 3548 | } |
| 3549 | |
| 3550 | /***************************************************************************** |
| 3551 | ** RoPE — 2D axial rotary positional embeddings |
no test coverage detected