torchgeometry.utils

tensor_to_image(tensor)[source]

Converts a PyTorch tensor image to a numpy image. In case the tensor is in the GPU, it will be copied back to CPU.

Parameters:tensor (torch.Tensor) – image of the form \((C, H, W)\).
Returns:image of the form \((H, W, C)\).
Return type:numpy.ndarray
image_to_tensor(image)[source]

Converts a numpy image to a PyTorch tensor image.

Parameters:image (numpy.ndarray) – image of the form \((H, W, C)\).
Returns:tensor of the form \((C, H, W)\).
Return type:torch.Tensor
create_meshgrid(height: int, width: int, normalized_coordinates: Optional[bool] = True)[source]

Generates a coordinate grid for an image.

When the flag normalized_coordinates is set to True, the grid is normalized to be in the range [-1,1] to be consistent with the pytorch function grid_sample. http://pytorch.org/docs/master/nn.html#torch.nn.functional.grid_sample

Parameters:
  • height (int) – the image height (rows).
  • width (int) – the image width (cols).
  • normalized_coordinates (Optional[bool]) – wether to normalize coordinates in the range [-1, 1] in order to be consistent with the PyTorch function grid_sample.
Returns:

returns a grid tensor with shape \((1, H, W, 2)\).

Return type:

torch.Tensor