basicsr.data.degradations

basicsr.data.degradations.add_gaussian_noise(img, sigma=10, clip=True, rounds=False, gray_noise=False)[source]

Add Gaussian noise.

Parameters:
  • img (Numpy array) – Input image, shape (h, w, c), range [0, 1], float32.

  • sigma (float) – Noise scale (measured in range 255). Default: 10.

Returns:

Returned noisy image, shape (h, w, c), range[0, 1],

float32.

Return type:

(Numpy array)

basicsr.data.degradations.add_gaussian_noise_pt(img, sigma=10, gray_noise=0, clip=True, rounds=False)[source]

Add Gaussian noise (PyTorch version).

Parameters:
  • img (Tensor) – Shape (b, c, h, w), range[0, 1], float32.

  • scale (float | Tensor) – Noise scale. Default: 1.0.

Returns:

Returned noisy image, shape (b, c, h, w), range[0, 1],

float32.

Return type:

(Tensor)

basicsr.data.degradations.add_jpg_compression(img, quality=90)[source]

Add JPG compression artifacts.

Parameters:
  • img (Numpy array) – Input image, shape (h, w, c), range [0, 1], float32.

  • quality (float) – JPG compression quality. 0 for lowest quality, 100 for best quality. Default: 90.

Returns:

Returned image after JPG, shape (h, w, c), range[0, 1],

float32.

Return type:

(Numpy array)

basicsr.data.degradations.add_poisson_noise(img, scale=1.0, clip=True, rounds=False, gray_noise=False)[source]

Add poisson noise.

Parameters:
  • img (Numpy array) – Input image, shape (h, w, c), range [0, 1], float32.

  • scale (float) – Noise scale. Default: 1.0.

  • gray_noise (bool) – Whether generate gray noise. Default: False.

Returns:

Returned noisy image, shape (h, w, c), range[0, 1],

float32.

Return type:

(Numpy array)

basicsr.data.degradations.add_poisson_noise_pt(img, scale=1.0, clip=True, rounds=False, gray_noise=0)[source]

Add poisson noise to a batch of images (PyTorch version).

Parameters:
  • img (Tensor) – Input image, shape (b, c, h, w), range [0, 1], float32.

  • scale (float | Tensor) – Noise scale. Number or Tensor with shape (b). Default: 1.0.

  • gray_noise (float | Tensor) – 0-1 number or Tensor with shape (b). 0 for False, 1 for True. Default: 0.

Returns:

Returned noisy image, shape (b, c, h, w), range[0, 1],

float32.

Return type:

(Tensor)

basicsr.data.degradations.bivariate_Gaussian(kernel_size, sig_x, sig_y, theta, grid=None, isotropic=True)[source]

Generate a bivariate isotropic or anisotropic Gaussian kernel.

In the isotropic mode, only sig_x is used. sig_y and theta is ignored.

Parameters:
  • kernel_size (int) –

  • sig_x (float) –

  • sig_y (float) –

  • theta (float) – Radian measurement.

  • grid (ndarray, optional) – generated by mesh_grid(), with the shape (K, K, 2), K is the kernel size. Default: None

  • isotropic (bool) –

Returns:

normalized kernel.

Return type:

kernel (ndarray)

basicsr.data.degradations.bivariate_generalized_Gaussian(kernel_size, sig_x, sig_y, theta, beta, grid=None, isotropic=True)[source]

Generate a bivariate generalized Gaussian kernel.

Paper: Parameter Estimation For Multivariate Generalized Gaussian Distributions

In the isotropic mode, only sig_x is used. sig_y and theta is ignored.

Parameters:
  • kernel_size (int) –

  • sig_x (float) –

  • sig_y (float) –

  • theta (float) – Radian measurement.

  • beta (float) – shape parameter, beta = 1 is the normal distribution.

  • grid (ndarray, optional) – generated by mesh_grid(), with the shape (K, K, 2), K is the kernel size. Default: None

Returns:

normalized kernel.

Return type:

kernel (ndarray)

basicsr.data.degradations.bivariate_plateau(kernel_size, sig_x, sig_y, theta, beta, grid=None, isotropic=True)[source]

Generate a plateau-like anisotropic kernel.

1 / (1+x^(beta))

Reference: https://stats.stackexchange.com/questions/203629/is-there-a-plateau-shaped-distribution

In the isotropic mode, only sig_x is used. sig_y and theta is ignored.

Parameters:
  • kernel_size (int) –

  • sig_x (float) –

  • sig_y (float) –

  • theta (float) – Radian measurement.

  • beta (float) – shape parameter, beta = 1 is the normal distribution.

  • grid (ndarray, optional) – generated by mesh_grid(), with the shape (K, K, 2), K is the kernel size. Default: None

Returns:

normalized kernel.

Return type:

kernel (ndarray)

basicsr.data.degradations.cdf2(d_matrix, grid)[source]
Calculate the CDF of the standard bivariate Gaussian distribution.

Used in skewed Gaussian distribution.

Parameters:
  • d_matrix (ndarrasy) – skew matrix.

  • grid (ndarray) – generated by mesh_grid(), with the shape (K, K, 2), K is the kernel size.

Returns:

skewed cdf.

Return type:

cdf (ndarray)

basicsr.data.degradations.circular_lowpass_kernel(cutoff, kernel_size, pad_to=0)[source]

2D sinc filter

Reference: https://dsp.stackexchange.com/questions/58301/2-d-circularly-symmetric-low-pass-filter

Parameters:
  • cutoff (float) – cutoff frequency in radians (pi is max)

  • kernel_size (int) – horizontal and vertical size, must be odd.

  • pad_to (int) – pad kernel size to desired size, must be odd or zero.

basicsr.data.degradations.generate_gaussian_noise(img, sigma=10, gray_noise=False)[source]

Generate Gaussian noise.

Parameters:
  • img (Numpy array) – Input image, shape (h, w, c), range [0, 1], float32.

  • sigma (float) – Noise scale (measured in range 255). Default: 10.

Returns:

Returned noisy image, shape (h, w, c), range[0, 1],

float32.

Return type:

(Numpy array)

basicsr.data.degradations.generate_gaussian_noise_pt(img, sigma=10, gray_noise=0)[source]

Add Gaussian noise (PyTorch version).

Parameters:
  • img (Tensor) – Shape (b, c, h, w), range[0, 1], float32.

  • scale (float | Tensor) – Noise scale. Default: 1.0.

Returns:

Returned noisy image, shape (b, c, h, w), range[0, 1],

float32.

Return type:

(Tensor)

basicsr.data.degradations.generate_poisson_noise(img, scale=1.0, gray_noise=False)[source]

Generate poisson noise.

Reference: https://github.com/scikit-image/scikit-image/blob/main/skimage/util/noise.py#L37-L219

Parameters:
  • img (Numpy array) – Input image, shape (h, w, c), range [0, 1], float32.

  • scale (float) – Noise scale. Default: 1.0.

  • gray_noise (bool) – Whether generate gray noise. Default: False.

Returns:

Returned noisy image, shape (h, w, c), range[0, 1],

float32.

Return type:

(Numpy array)

basicsr.data.degradations.generate_poisson_noise_pt(img, scale=1.0, gray_noise=0)[source]

Generate a batch of poisson noise (PyTorch version)

Parameters:
  • img (Tensor) – Input image, shape (b, c, h, w), range [0, 1], float32.

  • scale (float | Tensor) – Noise scale. Number or Tensor with shape (b). Default: 1.0.

  • gray_noise (float | Tensor) – 0-1 number or Tensor with shape (b). 0 for False, 1 for True. Default: 0.

Returns:

Returned noisy image, shape (b, c, h, w), range[0, 1],

float32.

Return type:

(Tensor)

basicsr.data.degradations.mesh_grid(kernel_size)[source]

Generate the mesh grid, centering at zero.

Parameters:

kernel_size (int) –

Returns:

with the shape (kernel_size, kernel_size, 2) xx (ndarray): with the shape (kernel_size, kernel_size) yy (ndarray): with the shape (kernel_size, kernel_size)

Return type:

xy (ndarray)

basicsr.data.degradations.pdf2(sigma_matrix, grid)[source]

Calculate PDF of the bivariate Gaussian distribution.

Parameters:
  • sigma_matrix (ndarray) – with the shape (2, 2)

  • grid (ndarray) – generated by mesh_grid(), with the shape (K, K, 2), K is the kernel size.

Returns:

un-normalized kernel.

Return type:

kernel (ndarrray)

basicsr.data.degradations.random_add_gaussian_noise(img, sigma_range=(0, 1.0), gray_prob=0, clip=True, rounds=False)[source]
basicsr.data.degradations.random_add_gaussian_noise_pt(img, sigma_range=(0, 1.0), gray_prob=0, clip=True, rounds=False)[source]
basicsr.data.degradations.random_add_jpg_compression(img, quality_range=(90, 100))[source]

Randomly add JPG compression artifacts.

Parameters:
  • img (Numpy array) – Input image, shape (h, w, c), range [0, 1], float32.

  • quality_range (tuple[float] | list[float]) – JPG compression quality range. 0 for lowest quality, 100 for best quality. Default: (90, 100).

Returns:

Returned image after JPG, shape (h, w, c), range[0, 1],

float32.

Return type:

(Numpy array)

basicsr.data.degradations.random_add_poisson_noise(img, scale_range=(0, 1.0), gray_prob=0, clip=True, rounds=False)[source]
basicsr.data.degradations.random_add_poisson_noise_pt(img, scale_range=(0, 1.0), gray_prob=0, clip=True, rounds=False)[source]
basicsr.data.degradations.random_bivariate_Gaussian(kernel_size, sigma_x_range, sigma_y_range, rotation_range, noise_range=None, isotropic=True)[source]

Randomly generate bivariate isotropic or anisotropic Gaussian kernels.

In the isotropic mode, only sigma_x_range is used. sigma_y_range and rotation_range is ignored.

Parameters:
  • kernel_size (int) –

  • sigma_x_range (tuple) – [0.6, 5]

  • sigma_y_range (tuple) – [0.6, 5]

  • range (rotation) – [-math.pi, math.pi]

  • noise_range (tuple, optional) – multiplicative kernel noise, [0.75, 1.25]. Default: None

Return type:

kernel (ndarray)

basicsr.data.degradations.random_bivariate_generalized_Gaussian(kernel_size, sigma_x_range, sigma_y_range, rotation_range, beta_range, noise_range=None, isotropic=True)[source]

Randomly generate bivariate generalized Gaussian kernels.

In the isotropic mode, only sigma_x_range is used. sigma_y_range and rotation_range is ignored.

Parameters:
  • kernel_size (int) –

  • sigma_x_range (tuple) – [0.6, 5]

  • sigma_y_range (tuple) – [0.6, 5]

  • range (rotation) – [-math.pi, math.pi]

  • beta_range (tuple) – [0.5, 8]

  • noise_range (tuple, optional) – multiplicative kernel noise, [0.75, 1.25]. Default: None

Return type:

kernel (ndarray)

basicsr.data.degradations.random_bivariate_plateau(kernel_size, sigma_x_range, sigma_y_range, rotation_range, beta_range, noise_range=None, isotropic=True)[source]

Randomly generate bivariate plateau kernels.

In the isotropic mode, only sigma_x_range is used. sigma_y_range and rotation_range is ignored.

Parameters:
  • kernel_size (int) –

  • sigma_x_range (tuple) – [0.6, 5]

  • sigma_y_range (tuple) – [0.6, 5]

  • range (rotation) – [-math.pi/2, math.pi/2]

  • beta_range (tuple) – [1, 4]

  • noise_range (tuple, optional) – multiplicative kernel noise, [0.75, 1.25]. Default: None

Return type:

kernel (ndarray)

basicsr.data.degradations.random_generate_gaussian_noise(img, sigma_range=(0, 10), gray_prob=0)[source]
basicsr.data.degradations.random_generate_gaussian_noise_pt(img, sigma_range=(0, 10), gray_prob=0)[source]
basicsr.data.degradations.random_generate_poisson_noise(img, scale_range=(0, 1.0), gray_prob=0)[source]
basicsr.data.degradations.random_generate_poisson_noise_pt(img, scale_range=(0, 1.0), gray_prob=0)[source]
basicsr.data.degradations.random_mixed_kernels(kernel_list, kernel_prob, kernel_size=21, sigma_x_range=(0.6, 5), sigma_y_range=(0.6, 5), rotation_range=(-3.141592653589793, 3.141592653589793), betag_range=(0.5, 8), betap_range=(0.5, 8), noise_range=None)[source]

Randomly generate mixed kernels.

Parameters:
  • kernel_list (tuple) – a list name of kernel types, support [‘iso’, ‘aniso’, ‘skew’, ‘generalized’, ‘plateau_iso’, ‘plateau_aniso’]

  • kernel_prob (tuple) – corresponding kernel probability for each kernel type

  • kernel_size (int) –

  • sigma_x_range (tuple) – [0.6, 5]

  • sigma_y_range (tuple) – [0.6, 5]

  • range (rotation) – [-math.pi, math.pi]

  • beta_range (tuple) – [0.5, 8]

  • noise_range (tuple, optional) – multiplicative kernel noise, [0.75, 1.25]. Default: None

Return type:

kernel (ndarray)

basicsr.data.degradations.sigma_matrix2(sig_x, sig_y, theta)[source]

Calculate the rotated sigma matrix (two dimensional matrix).

Parameters:
  • sig_x (float) –

  • sig_y (float) –

  • theta (float) – Radian measurement.

Returns:

Rotated sigma matrix.

Return type:

ndarray