mmagic.utils.img_utils
¶
Module Contents¶
Functions¶
|
Judge whether the input value can be converted to image tensor via |
|
Trans image to tensor. |
|
Trans image and sequence of frames to tensor. |
|
Convert torch Tensors into image numpy arrays. |
|
Reorder images to 'HWC' order. |
|
Convert data into numpy arrays of dtype. |
|
|
- mmagic.utils.img_utils.can_convert_to_image(value)[source]¶
Judge whether the input value can be converted to image tensor via
images_to_tensor()
function.- Parameters
value (any) – The input value.
- Returns
- If true, the input value can convert to image with
images_to_tensor()
, and vice versa.
- Return type
bool
- mmagic.utils.img_utils.image_to_tensor(img)[source]¶
Trans image to tensor.
- Parameters
img (np.ndarray) – The original image.
- Returns
The output tensor.
- Return type
Tensor
- mmagic.utils.img_utils.all_to_tensor(value)[source]¶
Trans image and sequence of frames to tensor.
- Parameters
value (np.ndarray | list[np.ndarray] | Tuple[np.ndarray]) – The original image or list of frames.
- Returns
The output tensor.
- Return type
Tensor
- mmagic.utils.img_utils.tensor2img(tensor, out_type=np.uint8, min_max=(0, 1))[source]¶
Convert torch Tensors into image numpy arrays.
After clamping to (min, max), image values will be normalized to [0, 1].
For different tensor shapes, this function will have different behaviors:
- 4D mini-batch Tensor of shape (N x 3/1 x H x W):
Use make_grid to stitch images in the batch dimension, and then convert it to numpy array.
- 3D Tensor of shape (3/1 x H x W) and 2D Tensor of shape (H x W):
Directly change to numpy array.
Note that the image channel in input tensors should be RGB order. This function will convert it to cv2 convention, i.e., (H x W x C) with BGR order.
- Parameters
tensor (Tensor | list[Tensor]) – Input tensors.
out_type (numpy type) – Output types. If
np.uint8
, transform outputs to uint8 type with range [0, 255]; otherwise, float type with range [0, 1]. Default:np.uint8
.min_max (tuple) – min and max values for clamp.
- Returns
3D ndarray of shape (H x W x C) or 2D ndarray of shape (H x W).
- Return type
(Tensor | list[Tensor])
- mmagic.utils.img_utils.reorder_image(img, input_order='HWC')[source]¶
Reorder images to ‘HWC’ order.
If the input_order is (h, w), return (h, w, 1); If the input_order is (c, h, w), return (h, w, c); If the input_order is (h, w, c), return as it is.
- Parameters
img (np.ndarray) – Input image.
input_order (str) – Whether the input order is ‘HWC’ or ‘CHW’. If the input image shape is (h, w), input_order will not have effects. Default: ‘HWC’.
- Returns
Reordered image.
- Return type
np.ndarray