Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Ggml Sam image preprocess

From Leeroopedia
Revision as of 15:02, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Ggml_org_Ggml_Sam_image_preprocess.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Template:KapsoPageMeta

Sam Image Preprocess

C++ implementation of image preprocessing for the Segment Anything Model (SAM) in the GGML framework.

Primary API

sam_image_preprocess

Attribute Value
Signature bool sam_image_preprocess(const sam_image_u8 & img, sam_image_f32 & res)
Source examples/sam/sam.cpp:L443-505
Repository https://github.com/ggml-org/ggml
Language C++
Parameters img (input uint8 image), res (output float32 preprocessed image)
Returns bool success; populates res with a 1024x1024 float32 image (bilinear-resized, ImageNet-normalized, zero-padded)

Processing steps performed:

  1. Bilinear resize to 1024x1024 with aspect-ratio preservation
  2. ImageNet normalization (mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375])
  3. Zero-padding of remaining area after resize

sam_image_load_from_file

Attribute Value
Signature bool sam_image_load_from_file(const std::string & fname, sam_image_u8 & img)
Source examples/sam/sam.cpp:L417-433
Purpose Loads a JPEG/PNG image from disk into the sam_image_u8 struct using stb_image

YOLO Equivalents

The YOLO example provides analogous preprocessing functions with a different pipeline:

load_image

Attribute Value
Source examples/yolo/yolo-image.cpp:L66-89
Purpose Loads and decodes an image from file into a YOLO-compatible image struct

letterbox_image

Attribute Value
Source examples/yolo/yolo-image.cpp:L144-160
Purpose Resizes image to 416x416 using letterbox strategy with 0.5 gray padding, preserving aspect ratio; outputs CHW planar layout

Dependencies

  • stb_image.h -- Single-header library used for JPEG/PNG image decoding
  • Standard C++ math -- Used for bilinear interpolation and normalization arithmetic

Related

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment