π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Add draw point2d (#2387)
Alex Brooksc54f3c9d1c0f
diff --git a/kornia/utils/__init__.py b/kornia/utils/__init__.pyindex 481e7ddd5..2cce92b03 100644--- a/kornia/utils/__init__.py+++ b/kornia/utils/__init__.py@@ -1,5 +1,5 @@from ._compat import torch_meshgrid-from .draw import draw_convex_polygon, draw_line, draw_rectangle+from .draw import draw_convex_polygon, draw_line, draw_point2d, draw_rectanglefrom .grid import create_meshgrid, create_meshgrid3dfrom .helpers import (_extract_device_dtype,@@ -35,6 +35,7 @@ __all__ = ["draw_convex_polygon","draw_rectangle","draw_line",+ "draw_point2d","_extract_device_dtype","safe_inverse_with_mask","safe_solve_with_mask",diff --git a/kornia/utils/draw.py b/kornia/utils/draw.pyindex 1244e732d..abaff6e0a 100644--- a/kornia/utils/draw.py+++ b/kornia/utils/draw.py@@ -8,6 +8,33 @@ from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE# TODO: implement width of the line+def draw_point2d(image: Tensor, points: Tensor, color: Tensor) -> Tensor:+ r"""Sets one or more coordinates in a Tensor to a color.++ 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.+ 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.+ """+ KORNIA_CHECK(+ (len(image.shape) == 2 and len(color.shape) == 1) or (image.shape[0] == color.shape[0]),+ "Color dim must match the channel dims of the provided image",+ )+ points = points.to(dtype=torch.int64, device=image.device)+ x, y = zip(*points)+ if len(color.shape) == 1:+ color = torch.unsqueeze(color, dim=1)+ color = color.to(dtype=image.dtype, device=image.device)+ if len(image.shape) == 2:+ image[y, x] = color+ else:+ image[:, y, x] = color+ return image++def _draw_pixel(image: torch.Tensor, x: int, y: int, color: torch.Tensor) -> None:r"""Draws a pixel into an image.diff --git a/test/utils/test_draw.py b/test/utils/test_draw.pyindex d9dcee8c4..8bfe3e336 100644--- a/test/utils/test_draw.py+++ b/test/utils/test_draw.py@@ -5,7 +5,70 @@ import torchfrom kornia.testing import assert_closefrom kornia.utils import create_meshgrid, draw_convex_polygon, draw_rectangle-from kornia.utils.draw import draw_line+from kornia.utils.draw import draw_line, draw_point2d+++class TestDrawPoint:+ """Test drawing individual pixels."""++ def test_draw_point2d_rgb(self, dtype, device):+ """Test plotting multiple [x, y] points."""+ points = torch.tensor([(1, 3), (2, 4)], 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)+ for x, y in points:+ assert_close(img[:, y, x], color.to(img.dtype))++ def test_draw_point2d_grayscale_third_order(self, dtype, device):+ """Test plotting multiple [x, y] points on a (1, m, n) image."""+ points = torch.tensor([(1, 3), (2, 4)], device=device)+ color = torch.tensor([100], dtype=dtype, device=device)+ img = torch.zeros(1, 8, 8, dtype=dtype, device=device)+ img = draw_point2d(img, points, color)+ for x, y in points:+ assert_close(img[:, y, x], color.to(img.dtype))++ def test_draw_point2d_grayscale_second_order(self, dtype, device):+ """Test plotting multiple [x, y] points on a (m, n) image."""+ points = torch.tensor([(1, 3), (2, 4)], device=device)+ color = torch.tensor([100], dtype=dtype, device=device)+ img = torch.zeros(8, 8, dtype=dtype, device=device)+ img = draw_point2d(img, points, color)+ for x, y in points:+ assert_close(torch.unsqueeze(img[y, x], dim=0), color.to(img.dtype))++ def test_draw_point2d_with_mismatched_dims(self, dtype, device):+ """Test that we raise if the len of the color tensor != the # of image channels."""+ points = torch.tensor([(1, 3), (2, 4)], device=device)+ color = torch.tensor([100], dtype=dtype, device=device)+ img = torch.zeros(3, 8, 8, dtype=dtype, device=device)+ with pytest.raises(Exception):+ draw_point2d(img, points, color)++ def test_draw_point2d_with_mismatched_dtype(self, device):+ """Test that the color is correctly cast to the image dtype when drawing points."""+ points = torch.tensor([(1, 3), (2, 4)], device=device)+ color = torch.tensor([5, 10, 15], dtype=torch.float32, device=device)+ img = torch.zeros(3, 8, 8, dtype=torch.uint8, device=device)+ img = draw_point2d(img, points, color)+ assert img.dtype is torch.uint8+ for x, y in points:+ assert_close(img[:, y, x], color.to(torch.uint8))++ def test_draw_point2d_with_singleton_color_dims(self, dtype, device):+ """Ensure that plotting behavior is consistent if we have a singleton dim for the color."""+ points = torch.tensor([(1, 3), (2, 4)], device=device)+ # Plot given a color tensor of shape [3]+ color_vec = torch.tensor([5, 10, 15], dtype=torch.float32, device=device)+ vec_img = torch.zeros(3, 8, 8, dtype=torch.uint8, device=device)+ drawn_vec_img = draw_point2d(vec_img, points, color_vec)+ # Plot given a color tensor of shape [3, 1]+ color_mat = torch.unsqueeze(color_vec, dim=1)+ mat_img = vec_img.clone()+ drawn_mat_img = draw_point2d(mat_img, points, color_mat)+ # Ensure that we get the same underlying image back+ assert_close(drawn_vec_img, drawn_mat_img)class TestDrawLine: