Shortcuts

mmagic.utils.trans_utils

Module Contents

Functions

make_coord(shape[, ranges, flatten])

Make coordinates at grid centers.

bbox2mask(img_shape, bbox[, dtype])

Generate mask in np.ndarray from bbox.

brush_stroke_mask(img_shape[, num_vertices, ...])

Generate free-form mask.

random_bbox(img_shape, max_bbox_shape[, ...])

Generate a random bbox for the mask on a given image.

random_irregular_mask(img_shape[, num_vertices, ...])

Generate random irregular masks.

get_irregular_mask(img_shape[, area_ratio_range])

Get irregular mask with the constraints in mask ratio.

dtype_limits(image[, clip_negative])

Return intensity limits, i.e. (min, max) tuple, of the image's dtype.

adjust_gamma(image[, gamma, gain])

Performs Gamma Correction on the input image.

add_gaussian_noise(img, mu, sigma)

Add Gaussian Noise on the input image.

random_choose_unknown(unknown, crop_size)

Randomly choose an unknown start (top-left) point for a given crop_size.

Attributes

_integer_types

_integer_ranges

dtype_range

mmagic.utils.trans_utils.make_coord(shape, ranges=None, flatten=True)[source]

Make coordinates at grid centers.

Parameters
  • shape (tuple) – shape of image.

  • ranges (tuple) – range of coordinate value. Default: None.

  • flatten (bool) – flatten to (n, 2) or Not. Default: True.

Returns

coordinates.

Return type

coord (Tensor)

mmagic.utils.trans_utils.bbox2mask(img_shape, bbox, dtype='uint8')[source]

Generate mask in np.ndarray from bbox.

The returned mask has the shape of (h, w, 1). ‘1’ indicates the hole and ‘0’ indicates the valid regions.

We prefer to use uint8 as the data type of masks, which may be different from other codes in the community.

Parameters
  • img_shape (tuple[int]) – The size of the image.

  • bbox (tuple[int]) – Configuration tuple, (top, left, height, width)

  • np.dtype (str) – Indicate the data type of returned masks. Default: ‘uint8’

Returns

Mask in the shape of (h, w, 1).

Return type

mask (np.ndarray)

mmagic.utils.trans_utils.brush_stroke_mask(img_shape, num_vertices=(4, 12), mean_angle=2 * math.pi / 5, angle_range=2 * math.pi / 15, brush_width=(12, 40), max_loops=4, dtype='uint8')[source]

Generate free-form mask.

The method of generating free-form mask is in the following paper: Free-Form Image Inpainting with Gated Convolution.

When you set the config of this type of mask. You may note the usage of np.random.randint and the range of np.random.randint is [left, right).

We prefer to use uint8 as the data type of masks, which may be different from other codes in the community.

TODO: Rewrite the implementation of this function.

Parameters
  • img_shape (tuple[int]) – Size of the image.

  • num_vertices (int | tuple[int]) – Min and max number of vertices. If only give an integer, we will fix the number of vertices. Default: (4, 12).

  • mean_angle (float) – Mean value of the angle in each vertex. The angle is measured in radians. Default: 2 * math.pi / 5.

  • angle_range (float) – Range of the random angle. Default: 2 * math.pi / 15.

  • brush_width (int | tuple[int]) – (min_width, max_width). If only give an integer, we will fix the width of brush. Default: (12, 40).

  • max_loops (int) – The max number of for loops of drawing strokes. Default: 4.

  • np.dtype (str) – Indicate the data type of returned masks. Default: ‘uint8’.

Returns

Mask in the shape of (h, w, 1).

Return type

mask (np.ndarray)

mmagic.utils.trans_utils.random_bbox(img_shape, max_bbox_shape, max_bbox_delta=40, min_margin=20)[source]

Generate a random bbox for the mask on a given image.

In our implementation, the max value cannot be obtained since we use np.random.randint. And this may be different with other standard scripts in the community.

Parameters
  • img_shape (tuple[int]) – The size of a image, in the form of (h, w).

  • max_bbox_shape (int | tuple[int]) – Maximum shape of the mask box, in the form of (h, w). If it is an integer, the mask box will be square.

  • max_bbox_delta (int | tuple[int]) – Maximum delta of the mask box, in the form of (delta_h, delta_w). If it is an integer, delta_h and delta_w will be the same. Mask shape will be randomly sampled from the range of max_bbox_shape - max_bbox_delta and max_bbox_shape. Default: (40, 40).

  • min_margin (int | tuple[int]) – The minimum margin size from the edges of mask box to the image boarder, in the form of (margin_h, margin_w). If it is an integer, margin_h and margin_w will be the same. Default: (20, 20).

Returns

The generated box, (top, left, h, w).

Return type

tuple[int]

mmagic.utils.trans_utils.random_irregular_mask(img_shape, num_vertices=(4, 8), max_angle=4, length_range=(10, 100), brush_width=(10, 40), dtype='uint8')[source]

Generate random irregular masks.

This is a modified version of free-form mask implemented in ‘brush_stroke_mask’.

We prefer to use uint8 as the data type of masks, which may be different from other codes in the community.

TODO: Rewrite the implementation of this function.

Parameters
  • img_shape (tuple[int]) – Size of the image.

  • num_vertices (int | tuple[int]) – Min and max number of vertices. If only give an integer, we will fix the number of vertices. Default: (4, 8).

  • max_angle (float) – Max value of angle at each vertex. Default 4.0.

  • length_range (int | tuple[int]) – (min_length, max_length). If only give an integer, we will fix the length of brush. Default: (10, 100).

  • brush_width (int | tuple[int]) – (min_width, max_width). If only give an integer, we will fix the width of brush. Default: (10, 40).

  • np.dtype (str) – Indicate the data type of returned masks. Default: ‘uint8’

Returns

Mask in the shape of (h, w, 1).

Return type

mask (np.ndarray)

mmagic.utils.trans_utils.get_irregular_mask(img_shape, area_ratio_range=(0.15, 0.5), **kwargs)[source]

Get irregular mask with the constraints in mask ratio.

Parameters
  • img_shape (tuple[int]) – Size of the image.

  • area_ratio_range (tuple(float)) – Contain the minimum and maximum area

  • Default (ratio.) – (0.15, 0.5).

Returns

Mask in the shape of (h, w, 1).

Return type

mask (np.ndarray)

mmagic.utils.trans_utils._integer_types = ()[source]
mmagic.utils.trans_utils._integer_ranges[source]
mmagic.utils.trans_utils.dtype_range[source]
mmagic.utils.trans_utils.dtype_limits(image, clip_negative=False)[source]

Return intensity limits, i.e. (min, max) tuple, of the image’s dtype.

This function is adopted from skimage: https://github.com/scikit-image/scikit-image/blob/ 7e4840bd9439d1dfb6beaf549998452c99f97fdd/skimage/util/dtype.py#L35

Parameters
  • image (np.ndarray) – Input image.

  • clip_negative (bool, optional) – If True, clip the negative range (i.e. return 0 for min intensity) even if the image dtype allows negative values. Default: False.

Returns

tuple: Lower and upper intensity limits.

mmagic.utils.trans_utils.adjust_gamma(image, gamma=1, gain=1)[source]

Performs Gamma Correction on the input image.

This function is adopted from skimage: https://github.com/scikit-image/scikit-image/blob/ 7e4840bd9439d1dfb6beaf549998452c99f97fdd/skimage/exposure/ exposure.py#L439-L494

Also known as Power Law Transform. This function transforms the input image pixelwise according to the equation O = I**gamma after scaling each pixel to the range 0 to 1.

Parameters
  • image (np.ndarray) – Input image.

  • gamma (float, optional) – Non negative real number. Defaults to 1.

  • gain (float, optional) – The constant multiplier. Defaults to 1.

Returns

Gamma corrected output image.

Return type

np.ndarray

mmagic.utils.trans_utils.add_gaussian_noise(img: numpy.ndarray, mu, sigma)[source]

Add Gaussian Noise on the input image.

Parameters
  • img (np.ndarray) – Input image.

  • mu (float) – The mu value of the Gaussian function.

  • sigma (float) – The sigma value of the Gaussian function.

Returns

Gaussian noisy output image.

Return type

noisy_img (np.ndarray)

mmagic.utils.trans_utils.random_choose_unknown(unknown, crop_size)[source]

Randomly choose an unknown start (top-left) point for a given crop_size.

Parameters
  • unknown (np.ndarray) – The binary unknown mask.

  • crop_size (tuple[int]) – The given crop size.

Returns

The top-left point of the chosen bbox.

Return type

tuple[int]

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.