fix: support grayscale letterbox images (#2297)
- Delete letterbox_image alpha block (lines 266-270): block wrote to caller's input array, not image_with_borders; used wrong coordinate system (resized vs original dims); redundant since cv2.copyMakeBorder already sets alpha=0 in padded regions when given a 3-element value - Add test_letterbox_image_for_rgba_opencv_image: asserts padded alpha=0, interior alpha preserved, and input array not mutated after call - Update letterbox_image docstring: image param lists (H,W,3)/(H,W,4)/ (H,W)/PIL shapes; add Note on BGRA alpha behavior; add grayscale doctest --------- Co-authored-by: jirka <6035284+Borda@users.noreply.github.com> Co-authored-by: Claude Code <noreply@anthropic.com>
This commit is contained in:
co-authored by
jirka
Claude Code
parent
35006d7342
commit
b18a30ea1a
@@ -217,14 +217,20 @@ def letterbox_image(
|
||||
maintaining aspect ratio.
|
||||
|
||||
Args:
|
||||
image: The image to resize and pad.
|
||||
image: The image to resize and pad. Accepts BGR arrays of shape
|
||||
``(H, W, 3)``, BGRA arrays of shape ``(H, W, 4)``, grayscale
|
||||
arrays of shape ``(H, W)``, or a PIL ``Image``.
|
||||
resolution_wh: Target resolution as `(width, height)`.
|
||||
color: Padding color. If tuple, should
|
||||
be in BGR format. Defaults to `Color.BLACK`.
|
||||
color: Padding color. If tuple, should be in BGR format.
|
||||
Defaults to `Color.BLACK`.
|
||||
|
||||
Returns:
|
||||
Letterboxed image matching input
|
||||
type.
|
||||
Letterboxed image matching input type.
|
||||
|
||||
Note:
|
||||
For BGRA inputs, the alpha channel in the padding region is set to
|
||||
0 (fully transparent). Grayscale inputs receive scalar padding
|
||||
from ``color[0]``.
|
||||
|
||||
Examples:
|
||||
```pycon
|
||||
@@ -238,6 +244,9 @@ def letterbox_image(
|
||||
... )
|
||||
>>> letterboxed_image.shape
|
||||
(1000, 1000, 3)
|
||||
>>> gray = np.zeros((4, 6), dtype=np.uint8)
|
||||
>>> sv.letterbox_image(image=gray, resolution_wh=(10, 10)).shape
|
||||
(10, 10)
|
||||
|
||||
```
|
||||
|
||||
@@ -263,12 +272,6 @@ def letterbox_image(
|
||||
value=color,
|
||||
)
|
||||
|
||||
if image.shape[2] == 4:
|
||||
image[:padding_top, :, 3] = 0
|
||||
image[height_new - padding_bottom :, :, 3] = 0
|
||||
image[:, :padding_left, 3] = 0
|
||||
image[:, width_new - padding_right :, 3] = 0
|
||||
|
||||
return image_with_borders
|
||||
|
||||
|
||||
|
||||
@@ -72,6 +72,41 @@ def test_letterbox_image_for_opencv_image() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_letterbox_image_for_grayscale_opencv_image() -> None:
|
||||
image = np.zeros((4, 6), dtype=np.uint8)
|
||||
expected_result = np.concatenate(
|
||||
[
|
||||
np.ones((2, 10), dtype=np.uint8) * 255,
|
||||
np.zeros((6, 10), dtype=np.uint8),
|
||||
np.ones((2, 10), dtype=np.uint8) * 255,
|
||||
],
|
||||
axis=0,
|
||||
)
|
||||
|
||||
result = letterbox_image(image=image, resolution_wh=(10, 10), color=(255, 255, 255))
|
||||
|
||||
assert result.shape == (10, 10)
|
||||
assert np.array_equal(result, expected_result)
|
||||
|
||||
|
||||
def test_letterbox_image_for_rgba_opencv_image() -> None:
|
||||
"""RGBA input: padded alpha=0, interior alpha preserved, input array not mutated."""
|
||||
# given
|
||||
image = np.zeros((4, 6, 4), dtype=np.uint8)
|
||||
image[:, :, 3] = 128
|
||||
image_before = image.copy()
|
||||
|
||||
# when
|
||||
result = letterbox_image(image=image, resolution_wh=(10, 10), color=(0, 0, 0))
|
||||
|
||||
# then
|
||||
assert result.shape == (10, 10, 4)
|
||||
assert np.all(result[:2, :, 3] == 0), "padded top rows must have alpha=0"
|
||||
assert np.all(result[8:, :, 3] == 0), "padded bottom rows must have alpha=0"
|
||||
assert np.all(result[2:8, :, 3] == 128), "interior rows must preserve alpha"
|
||||
assert np.array_equal(image, image_before), "input must not be mutated"
|
||||
|
||||
|
||||
def test_letterbox_image_for_pillow_image() -> None:
|
||||
# given
|
||||
image = Image.new(mode="RGB", size=(640, 480), color=(0, 0, 0))
|
||||
|
||||
Reference in New Issue
Block a user