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) – IfFalsesqueeze the input image to match the shape \((H, W, C)\) or \((H, W)\). Default:Falseforce_contiguous (
bool, optional) – IfTruecall contiguous to the tensor before Default:False
- Return type:
- 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:
- Return type:
- Returns:
- tensor of the form \((B, C, H, W)\) if keepdim is
False, \((C, H, W)\) otherwise.
- tensor of the form \((B, C, H, W)\) if keepdim is
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:
- Return type:
- 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])