Image conversion#
- kornia.image.tensor_to_image(tensor, keepdim=False, force_contiguous=False)[source]#
Convert a PyTorch tensor image to a numpy image.
- Convention:
The channels move last, and a single channel is always dropped: \((1, H, W)\) gives \((H, W)\) and \((B, 1, H, W)\) gives \((B, H, W)\).
keepdimkeeps only the batch axis of a \((1, C, H, W)\) input.The array shares memory with a CPU tensor whose dtype NumPy supports. A
bfloat16tensor is returned as afloat32copy, a dtype NumPy cannot represent, such asfloat8_e4m3fn, raisesTypeError, a tensor on another device is copied, andforce_contiguous=Truecopies a result that is not contiguous, such as the channels-last view of a contiguous tensor with more than one channel.The dtype is kept, except
bfloat16, which NumPy lacks: it becomesfloat32.
- Parameters:
tensor (
Tensor) – image of the form \((H, W)\), \((C, H, W)\) or \((B, C, H, W)\).keepdim (
bool, optional) – IfTrue, keep the batch axis of a \((1, C, H, W)\) input. Default:Falseforce_contiguous (
bool, optional) – IfTrue, callcontiguouson the result before converting it to NumPy. Default:False
- Return type:
- Returns:
image of the form \((H, W)\), \((H, W, C)\), \((B, H, W)\) or \((B, H, W, C)\).
- Raises:
TypeError – if
tensoris not atorch.Tensor.ValueError – if
tensoris not two, three or four dimensional.
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 tensor image.
- Convention:
The last axis of a 3-D or 4-D array is always the channels: \((H, W, C)\) becomes \((C, H, W)\) and \((B, H, W, C)\) becomes \((B, C, H, W)\). A channels-first \((C, H, W)\) array is not detected: it is read as \((H, W, C)\).
The dtype and the values are kept, so
uint8stays in [0, 255], and the tensor is a view of the array that shares its memory. An array with a negative stride, such as OpenCV’simg[..., ::-1], raisesValueError; passimg[..., ::-1].copy().keepdimdefaults toTruehere and toFalseinImageToTensor.Files and arrays relates it to torchvision’s
ToTensor.
- Parameters:
- Return type:
- Returns:
tensor of the form \((C, H, W)\) for a 2-D or 3-D input with
keepdim=True(a 2-D input has \(C = 1\)), and \((B, C, H, W)\) otherwise.- Raises:
TypeError – if
imageis not anumpy.ndarray.ValueError – if
imageis not two, three or four dimensional.
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.
- Convention:
See the Convention block of
image_to_tensor(). Unlike it, this function copies the images into a new tensor and takes \((H, W, C)\) images only.
- Parameters:
images (
List[Any]) – list of images, each of the form \((H, W, C)\), all of the same shape.- 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])
- class kornia.image.ImageToTensor(keepdim=False)[source]#
Convert a NumPy image to a PyTorch tensor image with
image_to_tensor().- Convention:
See the Convention block of
image_to_tensor(). Unlike it,keepdimdefaults toFalse, so an \((H, W, C)\) array becomes \((1, C, H, W)\).
- Parameters:
keepdim (
bool, optional) – IfFalse, a 2-D or 3-D input gets a leading batch axis, \((1, C, H, W)\). A 4-D input is always \((B, C, H, W)\). Default:False