Skip to content

Instantly share code, notes, and snippets.

@miku
Forked from Prasad9/add_gaussian_noise.py
Created October 27, 2024 12:52
Show Gist options
  • Save miku/96686a2c241c06ba90b9a023b71f7bf2 to your computer and use it in GitHub Desktop.
Save miku/96686a2c241c06ba90b9a023b71f7bf2 to your computer and use it in GitHub Desktop.
Python code to add random Gaussian noise on images
import cv2
def add_gaussian_noise(X_imgs):
gaussian_noise_imgs = []
row, col, _ = X_imgs[0].shape
# Gaussian distribution parameters
mean = 0
var = 0.1
sigma = var ** 0.5
for X_img in X_imgs:
gaussian = np.random.random((row, col, 1)).astype(np.float32)
gaussian = np.concatenate((gaussian, gaussian, gaussian), axis = 2)
gaussian_img = cv2.addWeighted(X_img, 0.75, 0.25 * gaussian, 0.25, 0)
gaussian_noise_imgs.append(gaussian_img)
gaussian_noise_imgs = np.array(gaussian_noise_imgs, dtype = np.float32)
return gaussian_noise_imgs
gaussian_noise_imgs = add_gaussian_noise(X_imgs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment