π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix: fixes IndexError in draw_line method (#3116)
Christie Jacob5cebbcab716b
diff --git a/kornia/utils/draw.py b/kornia/utils/draw.pyindex af179e079..9f8914458 100644--- a/kornia/utils/draw.py+++ b/kornia/utils/draw.py@@ -190,8 +190,16 @@ def draw_line(image: torch.Tensor, p1: torch.Tensor, p2: torch.Tensor, color: tofor y_i, s, dy_i in zip(y1[rest_mask], dy_sign[rest_mask], dy[rest_mask])for y in (y_i + s * torch.arange(0, dy_i + 1, 1, device=image.device))]- x_coords = torch.tensor(dx_zero_x_coords + dy_zero_x_coords + dx_gt_dy_x_coords + rest_x_coords).long()- y_coords = torch.tensor(dx_zero_y_coords + dy_zero_y_coords + dx_gt_dy_y_coords + rest_y_coords).long()+ x_coords = torch.clamp(+ torch.tensor(dx_zero_x_coords + dy_zero_x_coords + dx_gt_dy_x_coords + rest_x_coords).long(),+ min=0,+ max=image.shape[-1] - 1,+ )+ y_coords = torch.clamp(+ torch.tensor(dx_zero_y_coords + dy_zero_y_coords + dx_gt_dy_y_coords + rest_y_coords).long(),+ min=0,+ max=image.shape[-2] - 1,+ )image[:, y_coords, x_coords] = color.view(-1, 1)return imagediff --git a/tests/utils/test_draw.py b/tests/utils/test_draw.pyindex 8907ba51e..c8c45a5e4 100644--- a/tests/utils/test_draw.py+++ b/tests/utils/test_draw.py@@ -276,6 +276,15 @@ class TestDrawLine:assert "p2 is out of bounds." == str(excinfo.value)+ @pytest.mark.parametrize(+ "p1,p2", [(torch.tensor([0, 0]), torch.tensor([0, 0])), (torch.tensor([0, 0]), torch.tensor([7, 7]))]+ )+ def test_p1_p2_bounds(self, p1, p2, dtype, device):+ """Tests to verify that points on the bounds do not trigger errors."""+ img = torch.zeros(1, 8, 8, dtype=dtype, device=device)+ img = draw_line(img, p1, p2, torch.tensor([255]))+ assert img.shape == (1, 8, 8)+@pytest.mark.parametrize("img_size", [(200, 100), (32, 3, 20, 20)])def test_image_size(self, img_size, dtype, device):img = torch.zeros(*img_size, dtype=dtype, device=device)