Image conversion#

kornia.image.tensor_to_image(tensor, keepdim=False, force_contiguous=False)[source]#

Convert 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 (Tensor) – image of the form \((H, W)\), \((C, H, W)\) or \((B, C, H, W)\).

  • keepdim (bool, optional) – If False squeeze the input image to match the shape \((H, W, C)\) or \((H, W)\). Default: False

  • force_contiguous (bool, optional) – If True call contiguous to the tensor before Default: False

Return type:

Any

Returns:

image of the form \((H, W)\), \((H, W, C)\) or \((B, H, W, C)\).

Example

>>> img = torch.ones(1, 3, 3)
>>> tensor_to_image(img).shape
(3, 3)
>>> img = torch.ones(3, 4, 4)
>>> tensor_to_image(img).shape
(4, 4, 3)
kornia.image.image_to_tensor(image, keepdim=True)[source]#

Convert a numpy image to a PyTorch 4d tensor image.

Parameters:
  • image (Any) – image of the form \((H, W, C)\), \((H, W)\) or \((B, H, W, C)\).

  • keepdim (bool, optional) – If False unsqueeze the input image to match the shape \((B, H, W, C)\). Default: True

Return type:

Tensor

Returns:

tensor of the form \((B, C, H, W)\) if keepdim is False,

\((C, H, W)\) otherwise.

Example

>>> img = np.ones((3, 3))
>>> image_to_tensor(img).shape
torch.Size([1, 3, 3])
>>> img = np.ones((4, 4, 1))
>>> image_to_tensor(img).shape
torch.Size([1, 4, 4])
>>> img = np.ones((4, 4, 3))
>>> image_to_tensor(img, keepdim=False).shape
torch.Size([1, 3, 4, 4])
kornia.image.image_list_to_tensor(images)[source]#

Convert a list of numpy images to a PyTorch 4d tensor image.

Parameters:
  • images (List[Any]) – list of images, each of the form \((H, W, C)\).

  • consistent (Image shapes must be)

Return type:

Tensor

Returns:

tensor of the form \((B, C, H, W)\).

Example

>>> imgs = [np.ones((4, 4, 1)), np.zeros((4, 4, 1))]
>>> image_list_to_tensor(imgs).shape
torch.Size([2, 1, 4, 4])
class kornia.image.ImageToTensor(keepdim=False)[source]#

Converts a numpy image to a PyTorch 4d tensor image.

Parameters:

keepdim (bool, optional) – If False unsqueeze the input image to match the shape \((B, H, W, C)\). Default: False