Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(io): ensure grayscale images are saved with channel dim (HxWx1) (#3280) (#3284)

Sanskar Modif2d56306e850
diff --git a/kornia/feature/sold2/sold2_detector.py b/kornia/feature/sold2/sold2_detector.py
index 33354e034..835803b5f 100644
--- a/kornia/feature/sold2/sold2_detector.py
+++ b/kornia/feature/sold2/sold2_detector.py
@@ -49,7 +49,7 @@ class SOLD2_detector(Module):
The raw junction and line heatmaps, as well as the list of detected line segments (ij coordinates convention).
Example:
- >>> img = torch.rand(1, 1, 512, 512)
+ >>> img = torch.rand(1, 1, 128, 128)
>>> sold2_detector = SOLD2_detector(pretrained=False)
>>> line_segments = sold2_detector(img)["line_segments"]
diff --git a/kornia/io/io.py b/kornia/io/io.py
index 9ec6039ba..bc2db64f7 100644
--- a/kornia/io/io.py
+++ b/kornia/io/io.py
@@ -202,7 +202,8 @@ def write_image(path_file: str | Path, image: Tensor, quality: int = 80) -> None
KORNIA_CHECK(image.dim() >= 2, f"Invalid image shape: {image.shape}. Must be at least 2D.")
img_np = tensor_to_image(image, keepdim=True, force_contiguous=True) # HxWxC
-
+ if img_np.ndim == 2:
+ img_np = img_np[..., None] # ensures channel dimension
if image.dtype == torch.uint8:
_write_uint8_image(path_file, img_np, quality)
elif image.dtype == torch.uint16:
diff --git a/tests/feature/test_hardnet.py b/tests/feature/test_hardnet.py
index a5f18e62e..dbc4657a6 100644
--- a/tests/feature/test_hardnet.py
+++ b/tests/feature/test_hardnet.py
@@ -69,7 +69,7 @@ class TestHardNet8(BaseTester):
@pytest.mark.skip("jacobian not well computed")
def test_gradcheck(self, device):
- patches = torch.rand(2, 1, 32, 32, device=device, dtype=torch.float64)
+ patches = torch.rand(2, 1, 32, 32, device=device, dtype=torch.float32)
hardnet = HardNet8().to(patches.device, patches.dtype)
self.gradcheck(hardnet, (patches,), eps=1e-4, atol=1e-4)