Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix _se_to_mask (#965)

Roger Shieh16eca0cd1a30
diff --git a/kornia/morphology/basic_operators.py b/kornia/morphology/basic_operators.py
index 81cd76dc5..2757b72d6 100644
--- a/kornia/morphology/basic_operators.py
+++ b/kornia/morphology/basic_operators.py
@@ -12,9 +12,9 @@ def _se_to_mask(se: torch.Tensor) -> torch.Tensor:
num_feats = se_h * se_w
out = torch.zeros(num_feats, 1, se_h, se_w, dtype=se.dtype, device=se.device)
for i in range(num_feats):
- y = i % se_h
- x = i // se_h
- out[i, 0, x, y] = (se_flat[i] >= 0).float()
+ y = i // se_w
+ x = i % se_w
+ out[i, 0, y, x] = (se_flat[i] >= 0).float()
return out
@@ -107,7 +107,7 @@ def erosion(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:
# pad
se_h, se_w = kernel.shape
- pad_e: List[int] = [se_h // 2, se_w // 2, se_h // 2, se_w // 2]
+ pad_e: List[int] = [se_w // 2, se_w // 2, se_h // 2, se_h // 2]
output: torch.Tensor = tensor.view(
tensor.shape[0] * tensor.shape[1], 1, tensor.shape[2], tensor.shape[3])
diff --git a/test/morphology/test_dilation.py b/test/morphology/test_dilation.py
index 67cf8afd2..8fe7994ee 100644
--- a/test/morphology/test_dilation.py
+++ b/test/morphology/test_dilation.py
@@ -15,7 +15,7 @@ class TestDilate():
@pytest.mark.parametrize(
"shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
@pytest.mark.parametrize(
- "kernel", [(3, 3), (5, 5)])
+ "kernel", [(3, 3), (5, 5), (3, 5), (5, 3)])
def test_cardinality(self, device, dtype, shape, kernel):
img = torch.ones(shape, device=device, dtype=dtype)
krnl = torch.ones(kernel, device=device, dtype=dtype)
diff --git a/test/morphology/test_erosion.py b/test/morphology/test_erosion.py
index 3c51a5e47..a8c739759 100644
--- a/test/morphology/test_erosion.py
+++ b/test/morphology/test_erosion.py
@@ -15,7 +15,7 @@ class TestErode():
@pytest.mark.parametrize(
"shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])
@pytest.mark.parametrize(
- "kernel", [(3, 3), (5, 5)])
+ "kernel", [(3, 3), (5, 5), (3, 5), (5, 3)])
def test_cardinality(self, device, dtype, shape, kernel):
img = torch.ones(shape, device=device, dtype=dtype)
krnl = torch.ones(kernel, device=device, dtype=dtype)