Last active
June 3, 2021 16:38
-
-
Save Prasad9/077050d9a63df17cb1eaf33df4158b19 to your computer and use it in GitHub Desktop.
Add salt and pepper noise to images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_salt_pepper_noise(X_imgs): | |
# Need to produce a copy as to not modify the original image | |
X_imgs_copy = X_imgs.copy() | |
row, col, _ = X_imgs_copy[0].shape | |
salt_vs_pepper = 0.2 | |
amount = 0.004 | |
num_salt = np.ceil(amount * X_imgs_copy[0].size * salt_vs_pepper) | |
num_pepper = np.ceil(amount * X_imgs_copy[0].size * (1.0 - salt_vs_pepper)) | |
for X_img in X_imgs_copy: | |
# Add Salt noise | |
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in X_img.shape] | |
X_img[coords[0], coords[1], :] = 1 | |
# Add Pepper noise | |
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in X_img.shape] | |
X_img[coords[0], coords[1], :] = 0 | |
return X_imgs_copy | |
salt_pepper_noise_imgs = add_salt_pepper_noise(X_imgs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from copy import deepcopy
then you can do
X_imgs_copy = deepcopy(X_imgs)