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):