Principle:Tencent Ncnn Letterbox Preprocessing
| Knowledge Sources | |
|---|---|
| Domains | Computer_Vision, Object_Detection |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Preprocessing technique that resizes an image to a target size while preserving its aspect ratio by adding padding borders, preventing distortion of object proportions in detection models.
Description
Letterbox preprocessing is the standard input preparation method for YOLO-family object detection models. Unlike simple resize (which distorts aspect ratio), letterbox preserves the original proportions by: (1) scaling the image so its longer side fits the target dimension, (2) padding the shorter side with a constant value (typically 114 gray) to reach the target square size.
The padding offsets must be tracked and used during post-processing to correctly map predicted bounding box coordinates back to the original image coordinate space. The padding is typically aligned to the model's stride (e.g., 32 pixels for YOLO).
Usage
Use letterbox preprocessing for all YOLO-family detection models (YOLOv5, YOLOv7, YOLOv8, YOLO11, NanoDet). Do not use for classification models, which typically use direct resize without aspect ratio preservation.
Theoretical Basis
Letterbox algorithm:
// Abstract letterbox algorithm
scale = min(target_size / image_w, target_size / image_h)
new_w = image_w * scale
new_h = image_h * scale
pad_w = (target_size - new_w) / 2 // aligned to stride
pad_h = (target_size - new_h) / 2
resized = resize(image, new_w, new_h)
letterboxed = pad(resized, pad_h, pad_w, value=114)
Coordinate adjustment in post-processing:
// Map detection coordinates back to original image
x_original = (x_detected - pad_w) / scale
y_original = (y_detected - pad_h) / scale