Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): sharpen integer images in float32 in unsharp_mask (#5946)

Chandan Kumard73fd16f90ed
diff --git a/changelog.d/5946.fixed.md b/changelog.d/5946.fixed.md
new file mode 100644
index 000000000..1bf706a79
--- /dev/null
+++ b/changelog.d/5946.fixed.md
@@ -0,0 +1 @@
+`unsharp_mask` and `UnsharpMask` filter an integer or bool image in float32 and return float32 on its own scale, like `gaussian_blur2d`, instead of raising a dtype error.
diff --git a/docs/source/get-started/conventions.rst b/docs/source/get-started/conventions.rst
index 4feca84a4..547a42e27 100644
--- a/docs/source/get-started/conventions.rst
+++ b/docs/source/get-started/conventions.rst
@@ -714,13 +714,12 @@ border names and the even-kernel anchor do not. :func:`~kornia.filters.filter2d`
:func:`~kornia.filters.box_blur`, :func:`~kornia.filters.gaussian_blur2d`, :func:`~kornia.filters.laplacian`,
:func:`~kornia.filters.motion_blur`, :func:`~kornia.filters.bilateral_blur`,
:func:`~kornia.filters.joint_bilateral_blur`, :func:`~kornia.filters.guided_blur`, :func:`~kornia.filters.canny`,
-:func:`~kornia.filters.spatial_gradient3d` and their modules filter an integer or bool image in float32 and return
+:func:`~kornia.filters.unsharp_mask`, :func:`~kornia.filters.spatial_gradient3d` and their modules filter an integer or bool image in float32 and return
float32 on its own scale, neither rescaled to :math:`[0, 1]` nor rounded and clamped to its dtype, where
``cv2.filter2D(img, -1, k)``, ``cv2.GaussianBlur`` and ``cv2.blur`` return a uint8 image rounded and saturated to
uint8; a floating image keeps its dtype. :func:`~kornia.filters.guided_blur` returns the promoted dtype of its
``guidance`` and ``input``, an integer one counting as float32. :func:`~kornia.filters.median_blur`,
-:func:`~kornia.filters.unsharp_mask`, :func:`~kornia.filters.spatial_gradient`, :func:`~kornia.filters.sobel` and
-the blur pools do not follow this yet: most of them raise on an integer image
+:func:`~kornia.filters.spatial_gradient`, :func:`~kornia.filters.sobel` and the blur pools do not follow this yet: most of them raise on an integer image
(`#5155 <https://github.com/kornia/kornia/issues/5155>`_).
- :func:`~kornia.filters.filter2d`, :func:`~kornia.filters.filter3d` and :func:`~kornia.filters.fft_conv`
diff --git a/kornia/filters/unsharp.py b/kornia/filters/unsharp.py
index 36be49caf..621b3a1fe 100644
--- a/kornia/filters/unsharp.py
+++ b/kornia/filters/unsharp.py
@@ -20,6 +20,7 @@ from __future__ import annotations
import torch
from torch import nn
+from .filter import _to_floating
from .gaussian import gaussian_blur2d
@@ -39,6 +40,8 @@ def unsharp_mask(
Convention block on :func:`~kornia.filters.gaussian_blur2d` for the argument order.
- The output is not clamped: next to an edge it overshoots on both sides, so a step from 0 to 1 comes back
below 0 and above 1. :ref:`Filtering <filtering-conventions>` maps the call onto scikit-image.
+ - An integer or bool image is filtered in float32 and the result is float32 on its own scale, like
+ :func:`~kornia.filters.gaussian_blur2d`; a floating image keeps its dtype.
Args:
input: the input torch.Tensor with shape :math:`(B,C,H,W)`.
@@ -58,6 +61,7 @@ def unsharp_mask(
torch.Size([2, 4, 5, 5])
"""
+ input = _to_floating(input)
data_blur: torch.Tensor = gaussian_blur2d(input, kernel_size, sigma, border_type)
return torch.lerp(data_blur, input, weight=2.0)
diff --git a/tests/filters/test_unsharp_mask.py b/tests/filters/test_unsharp_mask.py
index 3b60af308..166783931 100644
--- a/tests/filters/test_unsharp_mask.py
+++ b/tests/filters/test_unsharp_mask.py
@@ -46,6 +46,22 @@ class Testunsharp(BaseTester):
actual = unsharp_mask(data, kernel_size, sigma, "replicate")
assert actual.shape == shape
+ @pytest.mark.parametrize("input_dtype", [torch.uint8, torch.int16, torch.bool])
+ def test_integer_input_matches_float32_5155(self, input_dtype: torch.dtype, device: torch.device) -> None:
+ # An integer image is sharpened in float32 on its own scale, like gaussian_blur2d.
+ generator = torch.Generator().manual_seed(0)
+ low = -128 if input_dtype == torch.int16 else 0
+ high = 2 if input_dtype == torch.bool else 256
+ data = torch.randint(low, high, (2, 3, 9, 13), generator=generator).to(device, input_dtype)
+
+ expected = unsharp_mask(data.float(), (5, 5), (1.5, 1.5))
+ actual = unsharp_mask(data, (5, 5), (1.5, 1.5))
+
+ assert actual.dtype == torch.float32
+ assert actual.device == data.device
+ self.assert_close(actual, expected)
+ self.assert_close(UnsharpMask((5, 5), (1.5, 1.5))(data), expected)
+
@pytest.mark.skip(reason="nothing to test")
def test_exception(self): ...