Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix: Missed single pixel instance in upper left corner by `connected_components()` (#3168)

Harald Lykke Joakimsen558c53acecaf
diff --git a/kornia/contrib/connected_components.py b/kornia/contrib/connected_components.py
index 374dc8ff7..011785687 100644
--- a/kornia/contrib/connected_components.py
+++ b/kornia/contrib/connected_components.py
@@ -66,7 +66,7 @@ def connected_components(image: Tensor, num_iterations: int = 100) -> Tensor:
# allocate the output tensors for labels
B, _, _, _ = image_view.shape
- out = torch.arange(B * H * W, device=image.device, dtype=image.dtype).view((-1, 1, H, W))
+ out = torch.arange(1, B * H * W + 1, device=image.device, dtype=image.dtype).view((-1, 1, H, W))
out[~mask] = 0
for _ in range(num_iterations):
diff --git a/tests/contrib/test_connected_component.py b/tests/contrib/test_connected_component.py
index 846c8de5e..f447f042a 100644
--- a/tests/contrib/test_connected_component.py
+++ b/tests/contrib/test_connected_component.py
@@ -78,11 +78,11 @@ class TestConnectedComponents(BaseTester):
[
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
- [0.0, 14.0, 14.0, 0.0, 0.0, 11.0],
- [0.0, 14.0, 14.0, 0.0, 0.0, 0.0],
+ [0.0, 15.0, 15.0, 0.0, 0.0, 12.0],
+ [0.0, 15.0, 15.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0, 34.0, 34.0, 0.0],
- [0.0, 0.0, 0.0, 34.0, 34.0, 0.0],
+ [0.0, 0.0, 0.0, 35.0, 35.0, 0.0],
+ [0.0, 0.0, 0.0, 35.0, 35.0, 0.0],
]
]
],