From 57b9de50085aa008c11b49d69eaa4a970a1f7522 Mon Sep 17 00:00:00 2001 From: ww-6 <114972930+ww-6@users.noreply.github.com> Date: Thu, 22 Oct 2026 12:14:04 +0100 Subject: [PATCH] :bug: Fix `imresize` interpolation choice when only `output_size` is given `imresize` documents `scale_factor` as output / input (e.g. 0.5 halves the image), and the "optimize" interpolation picks `INTER_AREA` below 1.0 and `INTER_CUBIC` otherwise. When only `output_size` was passed, the scale factor was derived as input / output, so every downscale was treated as an upscale and got bicubic interpolation. Derive the scale factor as output / input and add a regression test checking that `output_size` and the equivalent `scale_factor` choose the same interpolation. --- tests/test_utils.py | 12 ++++++++++++ tiatoolbox/utils/transforms.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index f27115de5..7c8a822dc 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -163,6 +163,18 @@ def test_imresize_no_scale_factor() -> None: assert resized_img.shape == (100, 100, 3) +def test_imresize_output_size_interpolation() -> None: + """Test imresize selects interpolation from output_size like scale_factor.""" + img = np.zeros((448, 448, 3), dtype=np.uint8) + for output_size, scale_factor in [((224, 224), 0.5), ((896, 896), 2.0)]: + interpolations = [] + for kwargs in ({"output_size": output_size}, {"scale_factor": scale_factor}): + with patch.object(cv2, "resize", wraps=cv2.resize) as mock_resize: + utils.transforms.imresize(img, **kwargs) + interpolations.append(mock_resize.call_args.kwargs["interpolation"]) + assert interpolations[0] == interpolations[1] + + def test_imresize_no_scale_factor_or_output_size() -> None: """Test imresize with no scale_factor or output_size.""" img = np.zeros((2000, 1000, 3)) diff --git a/tiatoolbox/utils/transforms.py b/tiatoolbox/utils/transforms.py index f926df451..13ab3ad78 100644 --- a/tiatoolbox/utils/transforms.py +++ b/tiatoolbox/utils/transforms.py @@ -140,7 +140,7 @@ def imresize( ) if scale_factor_array is None: - scale_factor_array = img.shape[:2][::-1] / np.array(output_size_array) + scale_factor_array = np.array(output_size_array) / img.shape[:2][::-1] # Return original if scale factor is 1 if np.all(scale_factor_array == 1.0):