Lesson 2 of 9 · 26m
Resizing, Cropping, and Coordinate Discipline
Resize and crop images without breaking geometry, aspect ratio, coordinate mappings, or downstream detector expectations.
Resizing looks trivial until a bounding box points forty pixels left of the object. In production pipelines, geometry passes through resize, crop, padding, rotation, tiling, and model-input letterboxing. If you cannot transform coordinates through the exact same operations, you do not have a pipeline; you have a collage of image operations.
Aspect ratio first, resolution second
Naive resizing changes object proportions:
small = cv2.resize(frame, (640, 640), interpolation=cv2.INTER_AREA)That is fine for thumbnails and disastrous for measurements. For model input, letterbox instead: scale uniformly, then pad:
def letterbox(img, size=640, color=(114, 114, 114)):
h, w = img.shape[:2]
scale = min(size / w, size / h)
nw, nh = int(w * scale), int(h * scale)
resized = cv2.resize(img, (nw, nh), interpolation=cv2.INTER_AREA)
out = cv2.copyMakeBorder(
resized,
0, size - nh,
0, size - nw,
cv2.BORDER_CONSTANT,
value=color,
)
return out, scaleThat implementation is intentionally simple and pads only right and bottom. Many production implementations center the image and therefore add left and top offsets too. Either can be correct; mixing them is not.
Coordinates live in frames of reference
A point in the original image, a point in the resized image, and a point in a cropped tile are different values even when they describe the same physical pixel. Write down the mapping:
x_resized = x_original * scale + pad_x
y_resized = y_original * scale + pad_y
x_original = (x_resized - pad_x) / scale
y_original = (y_resized - pad_y) / scaleUse explicit names like x_src, x_model, and x_tile. A bare x is a future bug report.
Crop order changes everything
These are not the same operation:
# crop then resize
tile = frame[y:y+h, x:x+w]
model_input = cv2.resize(tile, (640, 640))
# resize then crop
resized = cv2.resize(frame, (640, 640))
tile = resized[ty:ty+224, tx:tx+224]Both may be useful. The first preserves local detail at the cost of variable input context; the second gives consistent global scale but can crop away context. Decide what the detector or measurement needs.
Interpolation is a quality control
INTER_AREA is usually best for shrinking. INTER_LINEAR is a good default for enlarging. INTER_NEAREST preserves hard labels and segmentation masks because it does not invent intermediate class values. Never bilinearly resize a label map unless you enjoy class IDs that do not exist.
mask_small = cv2.resize(mask, (320, 320), interpolation=cv2.INTER_NEAREST)
image_small = cv2.resize(image, (320, 320), interpolation=cv2.INTER_AREA)Pixel ownership and clipping
Python slicing permits partial crops silently. frame[y:y+h, x:x+w] may be smaller than requested near an edge. If downstream code expects fixed dimensions, explicitly validate or pad. Also remember OpenCV coordinates are normally integer pixel indices with origin at the top-left; geometric centers may use half-pixel conventions in some libraries.
Field notes
- Record every resize scale and padding offset in metadata next to the output image.
- Use nearest-neighbor interpolation for masks and class labels, not photographic interpolation.
- Test coordinate transforms round-trip: original to model space, then back to original.
- Do not mutate frames in place when another consumer still needs the original resolution.
- A detector can be accurate in model coordinates and visibly wrong after display if inverse mapping is missing.
Was this lesson helpful?