Shortcuts

mmagic.utils.img_utils

Module Contents

Functions

can_convert_to_image(value)

Judge whether the input value can be converted to image tensor via

image_to_tensor(img)

Trans image to tensor.

all_to_tensor(value)

Trans image and sequence of frames to tensor.

tensor2img(tensor[, out_type, min_max])

Convert torch Tensors into image numpy arrays.

reorder_image(img[, input_order])

Reorder images to 'HWC' order.

to_numpy(img[, dtype])

Convert data into numpy arrays of dtype.

get_box_info(pred_bbox, original_shape, final_size)

param pred_bbox

The bounding box for the instance

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:

  1. 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.

  2. 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

mmagic.utils.img_utils.to_numpy(img, dtype=np.float64)[source]

Convert data into numpy arrays of dtype.

Parameters
  • img (Tensor | np.ndarray) – Input data.

  • dtype (np.dtype) – Set the data type of the output. Default: np.float64

Returns

Converted numpy arrays data.

Return type

img (np.ndarray)

mmagic.utils.img_utils.get_box_info(pred_bbox, original_shape, final_size)[source]
Parameters
  • pred_bbox – The bounding box for the instance

  • original_shape – Original image shape

  • final_size – Size of the final output

Returns

[L_pad, R_pad, T_pad, B_pad, rh, rw]

Return type

List

Read the Docs v: latest
Versions
latest
stable
0.x
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.