π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix meshgrid3d function
Dmytro Mishkina4b2a2b52567
diff --git a/kornia/utils/grid.py b/kornia/utils/grid.pyindex 0957f7386..f16fbffb5 100644--- a/kornia/utils/grid.py+++ b/kornia/utils/grid.py@@ -62,11 +62,18 @@ def create_meshgrid3d(Return:torch.Tensor: returns a grid tensor with shape :math:`(1, D, H, W, 3)`."""- grid2d = create_meshgrid(height, width, normalized_coordinates)+ xs: Optional[torch.Tensor] = None+ ys: Optional[torch.Tensor] = None+ zs: Optional[torch.Tensor] = Noneif normalized_coordinates:- z = torch.linspace(-1, 1, depth)+ xs = torch.linspace(-1, 1, width)+ ys = torch.linspace(-1, 1, height)+ zs = torch.linspace(-1, 1, depth)else:- z = torch.linspace(0, depth - 1, depth)- z = z.view(depth, 1, 1, 1)- grid3d = torch.cat([z.repeat(1, height, width, 1).contiguous(), grid2d.repeat(depth, 1, 1, 1)], dim=3)- return grid3d.unsqueeze(0) # 1xDxHxWx3+ xs = torch.linspace(0, width - 1, width)+ ys = torch.linspace(0, height - 1, height)+ zs = torch.linspace(0, depth - 1, depth)+ # generate grid by stacking coordinates+ base_grid: torch.Tensor = torch.stack(+ torch.meshgrid([zs, xs, ys])).transpose(1, 2) # 3xHxW+ return base_grid.unsqueeze(0).permute(0, 3, 4, 2, 1) # 1xHxWx3diff --git a/test/utils/test_grid.py b/test/utils/test_grid.pyindex 8706e2b00..e0db790ee 100644--- a/test/utils/test_grid.py+++ b/test/utils/test_grid.py@@ -43,3 +43,18 @@ def test_normalize_pixel_grid():grid_norm_to_pix = kornia.transform_points(pix_trans_norm, grid_norm)assert_allclose(grid_pix, grid_norm_to_pix)assert_allclose(grid_norm, grid_pix_to_norm)+++def test_create_meshgrid3d():+ depth, height, width = 5, 4, 6+ normalized_coordinates = False++ # create the meshgrid and verify shape+ grid = kornia.utils.create_meshgrid3d(+ depth, height, width, normalized_coordinates)+ assert grid.shape == (1, depth, height, width, 3)++ # check grid corner values+ assert tuple(grid[0, 0, 0, 0].numpy()) == (0., 0., 0.)+ assert tuple(+ grid[0, depth - 1, height - 1, width - 1].numpy()) == (depth - 1, width - 1, height - 1)