36 lines
1005 B
Python
36 lines
1005 B
Python
"""
|
|
BBox Expander Module.
|
|
|
|
Expands bounding boxes by a uniform pixel padding on all sides,
|
|
clamped to image boundaries. No field-specific or directional logic.
|
|
"""
|
|
|
|
from .scale_strategy import UNIFORM_PAD
|
|
|
|
|
|
def expand_bbox(
|
|
bbox: tuple[float, float, float, float],
|
|
image_width: float,
|
|
image_height: float,
|
|
pad: int = UNIFORM_PAD,
|
|
) -> tuple[int, int, int, int]:
|
|
"""Expand bbox by uniform pixel padding, clamped to image bounds.
|
|
|
|
Args:
|
|
bbox: (x0, y0, x1, y1) in pixels.
|
|
image_width: Image width for boundary clamping.
|
|
image_height: Image height for boundary clamping.
|
|
pad: Uniform pixel padding on all sides (default: UNIFORM_PAD).
|
|
|
|
Returns:
|
|
Expanded bbox (x0, y0, x1, y1) as integers, clamped to image bounds.
|
|
"""
|
|
x0, y0, x1, y1 = bbox
|
|
|
|
nx0 = max(0, int(x0 - pad))
|
|
ny0 = max(0, int(y0 - pad))
|
|
nx1 = min(int(image_width), int(x1 + pad))
|
|
ny1 = min(int(image_height), int(y1 + pad))
|
|
|
|
return (nx0, ny0, nx1, ny1)
|