-
-
Save gutierrezps/f4ddad3bbd2ad5a9b96e3c06378e28b4 to your computer and use it in GitHub Desktop.
Add Salt and Pepper noise to OpenCV Image
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
# Add Salt and Pepper noise to OpenCV image, vectorized approach. | |
# https://stackoverflow.com/questions/22937589/how-to-add-noise-gaussian-salt-and-pepper-etc-to-image-in-python-with-opencv | |
# Forked and fixed from https://gist.github.com/lucaswiman/1e877a164a69f78694f845eab45c381a | |
# Fixed: replaced 'image' with 'output' | |
import numpy as np | |
import cv2 | |
def sp_noise(image, prob): | |
''' | |
Add salt and pepper noise to image | |
prob: Probability of the noise | |
''' | |
output = image.copy() | |
if len(image.shape) == 2: | |
black = 0 | |
white = 255 | |
else: | |
colorspace = image.shape[2] | |
if colorspace == 3: # RGB | |
black = np.array([0, 0, 0], dtype='uint8') | |
white = np.array([255, 255, 255], dtype='uint8') | |
else: # RGBA | |
black = np.array([0, 0, 0, 255], dtype='uint8') | |
white = np.array([255, 255, 255, 255], dtype='uint8') | |
probs = np.random.random(output.shape[:2]) | |
output[probs < (prob / 2)] = black | |
output[probs > 1 - (prob / 2)] = white | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment