mindarmour.natural_robustness.transform.image

This package include methods to generate natural perturbation samples.

class mindarmour.natural_robustness.transform.image.Contrast(alpha=1, beta=0, auto_param=False)[source]

Contrast of an image.

Parameters
  • alpha (Union[float, int]) – Control the contrast of an image. \(out_image = in_image*alpha+beta\). Suggested value range in [0.2, 2].

  • beta (Union[float, int]) – Delta added to alpha. Default: 0.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> alpha = 0.1
>>> beta = 1
>>> trans = Contrast(alpha, beta)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.Curve(curves=3, depth=10, mode='vertical', auto_param=False)[source]

Curve picture using sin method.

Parameters
  • curves (union[float, int]) – Divide width to curves of 2*math.pi, which means how many curve cycles. Suggested value range in [0.1. 5].

  • depth (union[float, int]) – Amplitude of sin method. Suggested value not exceed 1/10 of the length of the picture.

  • mode (str) – Direction of deformation. Optional value is ‘vertical’ or ‘horizontal’.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Examples

>>> img = cv2.imread('x.png')
>>> curves =1
>>> depth = 10
>>> trans = Curve(curves, depth, mode='vertical')
>>> img_new = trans(img)
class mindarmour.natural_robustness.transform.image.GaussianBlur(ksize=2, auto_param=False)[source]

Blurs the image using Gaussian blur filter.

Parameters
  • ksize (int) – Size of gaussian kernel, this value must be non-negnative.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> ksize = 5
>>> trans = GaussianBlur(ksize)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.GaussianNoise(factor=0.1, auto_param=False)[source]

Add gaussian noise of an image.

Parameters
  • factor (float) – Noise density, the proportion of noise points per unit pixel area. Suggested value range in [0.001, 0.15].

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> factor = 0.1
>>> trans = GaussianNoise(factor)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.GradientBlur(point, kernel_num=3, center=True, auto_param=False)[source]

Gradient blur.

Parameters
  • point (union[tuple, list]) – 2D coordinate of the Blur center point.

  • kernel_num (int) – Number of blur kernels. Suggested value range in [1, 8].

  • center (bool) – Blurred or clear at the center of a specified point.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('xx.png')
>>> img = np.array(img)
>>> number = 5
>>> h, w = img.shape[:2]
>>> point = (int(h / 5), int(w / 5))
>>> center = True
>>> trans = GradientBlur(point, number,  center)
>>> new_img = trans(img)
class mindarmour.natural_robustness.transform.image.GradientLuminance(color_start=(0, 0, 0), color_end=(255, 255, 255), start_point=(10, 10), scope=0.5, pattern='light', bright_rate=0.3, mode='circle', auto_param=False)[source]

Gradient adjusts the luminance of picture.

Parameters
  • color_start (union[tuple, list]) – Color of gradient center. Default:(0, 0, 0).

  • color_end (union[tuple, list]) – Color of gradient edge. Default:(255, 255, 255).

  • start_point (union[tuple, list]) – 2D coordinate of gradient center.

  • scope (float) – Range of the gradient. A larger value indicates a larger gradient range. Default: 0.3.

  • pattern (str) – Dark or light, this value must be in [‘light’, ‘dark’].

  • bright_rate (float) – Control brightness. A larger value indicates a larger gradient range. If parameter ‘pattern’ is ‘light’, Suggested value range in [0.1, 0.7], if parameter ‘pattern’ is ‘dark’, Suggested value range in [0.1, 0.9].

  • mode (str) – Gradient mode, value must be in [‘circle’, ‘horizontal’, ‘vertical’].

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Examples

>>> img = cv2.imread('x.png')
>>> height, width = img.shape[:2]
>>> point = (height // 4, width // 2)
>>> start = (255, 255, 255)
>>> end = (0, 0, 0)
>>> scope = 0.3
>>> pattern='light'
>>> bright_rate = 0.3
>>> trans = GradientLuminance(start, end, point, scope, pattern, bright_rate, mode='circle')
>>> img_new = trans(img)
class mindarmour.natural_robustness.transform.image.MotionBlur(degree=5, angle=45, auto_param=False)[source]

Motion blur for a given image.

Parameters
  • degree (int) – Degree of blur. This value must be positive. Suggested value range in [1, 15].

  • angle – (union[float, int]): Direction of motion blur. Angle=0 means up and down motion blur. Angle is counterclockwise.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> angle = 0
>>> degree = 5
>>> trans = MotionBlur(degree=degree, angle=angle)
>>> new_img = trans(img)
class mindarmour.natural_robustness.transform.image.NaturalNoise(ratio=0.0002, k_x_range=(1, 5), k_y_range=(3, 25), auto_param=False)[source]

Add natural noise to an image.

Parameters
  • ratio (float) – Noise density, the proportion of noise blocks per unit pixel area. Suggested value range in [0.00001, 0.001].

  • k_x_range (union[list, tuple]) – Value range of the noise block length.

  • k_y_range (union[list, tuple]) – Value range of the noise block width.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Examples

>>> img = cv2.imread('xx.png')
>>> img = np.array(img)
>>> ratio = 0.0002
>>> k_x_range = (1, 5)
>>> k_y_range = (3, 25)
>>> trans = NaturalNoise(ratio, k_x_range, k_y_range)
>>> new_img = trans(img)
class mindarmour.natural_robustness.transform.image.Perspective(ori_pos, dst_pos, auto_param=False)[source]

Perform perspective transformation on a given picture.

Parameters
  • ori_pos (list) – Four points in original image.

  • dst_pos (list) – The point coordinates of the 4 points in ori_pos after perspective transformation.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> ori_pos = [[0, 0], [0, 800], [800, 0], [800, 800]]
>>> dst_pos = [[50, 0], [0, 800], [780, 0], [800, 800]]
>>> trans = Perspective(ori_pos, dst_pos)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.Rotate(angle=20, auto_param=False)[source]

Rotate an image of counter clockwise around its center.

Parameters
  • angle (Union[float, int]) – Degrees of counter clockwise. Suggested value range in [-60, 60].

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> angle = 20
>>> trans = Rotate(angle)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.SaltAndPepperNoise(factor=0, auto_param=False)[source]

Add salt and pepper noise of an image.

Parameters
  • factor (float) – Noise density, the proportion of noise points per unit pixel area. Suggested value range in [0.001, 0.15].

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> factor = 0.1
>>> trans = SaltAndPepperNoise(factor)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.Scale(factor_x=1, factor_y=1, auto_param=False)[source]

Scale an image in the middle.

Parameters
  • factor_x (Union[float, int]) – Rescale in X-direction, x=factor_x*x. Suggested value range in [0.5, 1] and abs(factor_y - factor_x) < 0.5.

  • factor_y (Union[float, int]) – Rescale in Y-direction, y=factor_y*y. Suggested value range in [0.5, 1] and abs(factor_y - factor_x) < 0.5.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> factor_x = 0.7
>>> factor_y = 0.6
>>> trans = Scale(factor_x, factor_y)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.Shear(factor=0.2, direction='horizontal', auto_param=False)[source]

Shear an image, for each pixel (x, y) in the sheared image, the new value is taken from a position (x+factor_x*y, factor_y*x+y) in the origin image. Then the sheared image will be rescaled to fit original size.

Parameters
  • factor (Union[float, int]) – Shear rate in shear direction. Suggested value range in [0.05, 0.5].

  • direction (str) – Direction of deformation. Optional value is ‘vertical’ or ‘horizontal’.

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> factor = 0.2
>>> trans = Shear(factor, direction='horizontal')
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.Translate(x_bias=0, y_bias=0, auto_param=False)[source]

Translate an image.

Parameters
  • x_bias (Union[int, float]) – X-direction translation, x = x + x_bias*image_width. Suggested value range in [-0.1, 0.1].

  • y_bias (Union[int, float]) – Y-direction translation, y = y + y_bias*image_length. Suggested value range in [-0.1, 0.1].

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> x_bias = 0.1
>>> y_bias = 0.1
>>> trans = Translate(x_bias, y_bias)
>>> dst = trans(img)
class mindarmour.natural_robustness.transform.image.UniformNoise(factor=0.1, auto_param=False)[source]

Add uniform noise of an image.

Parameters
  • factor (float) – Noise density, the proportion of noise points per unit pixel area. Suggested value range in [0.001, 0.15].

  • auto_param (bool) – Auto selected parameters. Selected parameters will preserve semantics of image.

Example

>>> img = cv2.imread('1.png')
>>> img = np.array(img)
>>> factor = 0.1
>>> trans = UniformNoise(factor)
>>> dst = trans(img)