Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

docs(image): document in-place drawing for points and lines (#4826)

yi111e75048488053
diff --git a/changelog.d/4826.fixed.md b/changelog.d/4826.fixed.md
new file mode 100644
index 000000000..93bab59a5
--- /dev/null
+++ b/changelog.d/4826.fixed.md
@@ -0,0 +1 @@
+Clarify that `draw_point2d` and `draw_line` modify the input image in place and return it for convenience.
diff --git a/kornia/image/draw.py b/kornia/image/draw.py
index e6c19ee14..5d95a9b87 100644
--- a/kornia/image/draw.py
+++ b/kornia/image/draw.py
@@ -36,7 +36,8 @@ def draw_point2d(image: Tensor, points: Tensor, color: Tensor) -> Tensor:
color: the color of the pixel with :math`(C)` where :math`C` is the number of channels of the image.
Return:
- The image with points set to the color. An empty point set leaves the image unchanged.
+ The image with points set to the color. This operation modifies image inplace but also
+ returns the drawn tensor for convenience. An empty point set leaves the image unchanged.
"""
KORNIA_CHECK(
@@ -93,7 +94,8 @@ def draw_line(image: torch.Tensor, p1: torch.Tensor, p2: torch.Tensor, color: to
color: the color of the line with shape :math`(C)` where :math`C` is the number of channels of the image.
Return:
- the image with containing the line.
+ The image containing the line. This operation modifies image inplace but also returns
+ the drawn tensor for convenience.
Examples:
>>> image = torch.zeros(1, 8, 8)
diff --git a/testing/half_precision_eager_rng.py b/testing/half_precision_eager_rng.py
index 35eb6a0de..9940131de 100644
--- a/testing/half_precision_eager_rng.py
+++ b/testing/half_precision_eager_rng.py
@@ -199,7 +199,7 @@ AUDITED_EAGER_RNG_CALLS = tuple(
),
*_audited(
"tests/image/test_draw.py",
- 344,
+ 354,
"torch.rand",
4,
("tests/image/test_draw.py::TestDrawLine::test_point_size",),
diff --git a/tests/image/test_draw.py b/tests/image/test_draw.py
index dee117cc7..4b94a08cc 100644
--- a/tests/image/test_draw.py
+++ b/tests/image/test_draw.py
@@ -108,6 +108,16 @@ class TestDrawPoint(BaseTester):
assert out is img
self.assert_close(out, expected)
+ @pytest.mark.parametrize("points", [[[1, 3], [2, 4]], [1, 3]])
+ @pytest.mark.parametrize("shape", [(3, 8, 8), (8, 8)])
+ def test_draw_point2d_draws_in_place_and_returns_the_input(self, points, shape, dtype, device):
+ """The Return section documents in-place drawing into, and returning, the input tensor."""
+ img = torch.zeros(shape, dtype=dtype, device=device)
+ color = torch.ones(shape[0] if len(shape) == 3 else 1, dtype=dtype, device=device)
+ out = draw_point2d(img, torch.tensor(points, device=device), color)
+ assert out is img
+ assert img.count_nonzero() > 0
+
@pytest.mark.parametrize(
"shape, match",
[
@@ -376,6 +386,14 @@ class TestDrawLine(BaseTester):
assert out.shape == (1, 8, 8)
assert out.count_nonzero() == 0
+ @pytest.mark.parametrize("p1, p2", [([1, 4], [6, 4]), ([[1, 1], [0, 7]], [[6, 6], [7, 0]])])
+ def test_draw_line_draws_in_place_and_returns_the_input(self, p1, p2, dtype, device):
+ """The Return section documents in-place drawing into, and returning, the input tensor."""
+ img = torch.zeros(2, 8, 8, dtype=dtype, device=device)
+ out = draw_line(img, torch.tensor(p1), torch.tensor(p2), torch.ones(2, dtype=dtype, device=device))
+ assert out is img
+ assert img.count_nonzero() > 0
+
def test_draw_line_rejects_points_of_different_shapes(self, dtype, device):
img = torch.zeros(1, 8, 8, dtype=dtype, device=device)
with pytest.raises(ValueError, match="must have the same batch sizes"):