Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix draw_point2d crash on a single point or empty set (#4754)

Mohammad Hijjawidf76403f133c
diff --git a/changelog.d/4754.fixed.md b/changelog.d/4754.fixed.md
new file mode 100644
index 000000000..51f2bf56d
--- /dev/null
+++ b/changelog.d/4754.fixed.md
@@ -0,0 +1 @@
+`draw_point2d` accepts a single `[x, y]` vector and an empty `(0, 2)` point set instead of raising `TypeError` / `ValueError`.
diff --git a/kornia/image/draw.py b/kornia/image/draw.py
index a6b38a2e5..e6c19ee14 100644
--- a/kornia/image/draw.py
+++ b/kornia/image/draw.py
@@ -31,11 +31,12 @@ def draw_point2d(image: Tensor, points: Tensor, color: Tensor) -> Tensor:
Args:
image: the input image on which to draw the points with shape :math`(C,H,W)` or :math`(H,W)`.
- points: the [x, y] points to be drawn on the image.
+ points: the [x, y] points to be drawn on the image with shape :math`(N, 2)`, a single
+ point with shape :math`(2,)`, or an empty tensor with shape :math`(0, 2)`.
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.
+ The image with points set to the color. An empty point set leaves the image unchanged.
"""
KORNIA_CHECK(
@@ -43,7 +44,15 @@ def draw_point2d(image: Tensor, points: Tensor, color: Tensor) -> Tensor:
"Color dim must match the channel dims of the provided image",
)
points = points.to(dtype=torch.int64, device=image.device)
- x, y = zip(*points)
+ # A single [x, y] vector is a common call shape; zip(*points) iterated 0-d
+ # scalars and raised TypeError. An empty (0, 2) set used to fail unpacking;
+ # indexing with empty columns now leaves the image unchanged.
+ if points.ndim == 1:
+ KORNIA_CHECK(points.numel() == 2, "A 1D points tensor must have shape (2,) as [x, y]")
+ points = points.unsqueeze(0)
+ KORNIA_CHECK(points.ndim == 2 and points.shape[-1] == 2, "points must have shape (N, 2)")
+ x = points[:, 0]
+ y = points[:, 1]
if len(color.shape) == 1:
color = torch.unsqueeze(color, dim=1)
color = color.to(dtype=image.dtype, device=image.device)
diff --git a/testing/half_precision_eager_rng.py b/testing/half_precision_eager_rng.py
index a011c6f52..35eb6a0de 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",
- 305,
+ 344,
"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 4f7e0387c..dee117cc7 100644
--- a/tests/image/test_draw.py
+++ b/tests/image/test_draw.py
@@ -20,6 +20,7 @@ import math
import pytest
import torch
+from kornia.core.exceptions import BaseError
from kornia.geometry import create_meshgrid
from kornia.image import draw_convex_polygon, draw_rectangle
from kornia.image.draw import draw_line, draw_point2d
@@ -89,6 +90,44 @@ class TestDrawPoint(BaseTester):
# Ensure that we get the same underlying image back
self.assert_close(drawn_vec_img, drawn_mat_img)
+ def test_draw_point2d_single_1d_point(self, dtype, device):
+ """A single [x, y] vector used to TypeError via zip over 0-d scalars."""
+ points = torch.tensor([1, 3], device=device)
+ color = torch.tensor([5, 10, 15], dtype=dtype, device=device)
+ img = torch.zeros(3, 8, 8, dtype=dtype, device=device)
+ img = draw_point2d(img, points, color)
+ self.assert_close(img[:, 3, 1], color.to(img.dtype))
+
+ def test_draw_point2d_empty_points(self, dtype, device):
+ """An empty (0, 2) point set used to fail unpacking zip(*points)."""
+ points = torch.zeros(0, 2, device=device)
+ color = torch.tensor([5, 10, 15], dtype=dtype, device=device)
+ img = torch.arange(3 * 8 * 8, device=device).reshape(3, 8, 8).to(dtype)
+ expected = img.clone()
+ out = draw_point2d(img, points, color)
+ assert out is img
+ self.assert_close(out, expected)
+
+ @pytest.mark.parametrize(
+ "shape, match",
+ [
+ ((0,), "1D points tensor"),
+ ((3,), "1D points tensor"),
+ ((0, 3), "shape \\(N, 2\\)"),
+ ((5, 0), "shape \\(N, 2\\)"),
+ ((2, 3), "shape \\(N, 2\\)"),
+ ((2, 2, 2), "shape \\(N, 2\\)"),
+ ],
+ ids=["empty-1d", "three-1d", "empty-three-columns", "zero-columns", "three-columns", "batched"],
+ )
+ def test_draw_point2d_rejects_malformed_points(self, shape, match, dtype, device):
+ """Only (N, 2) and (2,) are accepted, including when the tensor is empty."""
+ points = torch.zeros(shape, dtype=torch.int64, device=device)
+ color = torch.tensor([5, 10, 15], dtype=dtype, device=device)
+ img = torch.zeros(3, 8, 8, dtype=dtype, device=device)
+ with pytest.raises(BaseError, match=match):
+ draw_point2d(img, points, color)
+
class TestDrawLine(BaseTester):
def test_draw_line_vertical(self, dtype, device):