No account yet ?
Tested in Anaconda and Python 3.7
import cv2 import numpy as np import math def uniform_noise_add(img, prob_noise, mean, variance): height, width = img.shape[:2] height, width, depth = np.shape(img) noise_array = np.zeros(20, np.float) noise_mat = np.zeros(shape = (img.shape[0],img.shape[1],img.shape[2]),dtype = np.float) count = 0 b = ((2 * mean) + math.sqrt(12 * variance)) / 2 a = (2 * mean) - b for i in range(-10,10): if i >= a and i <= b: fx = 1 / (b - a) else: fx = 0 noise_array[count] = fx count += 1 cdf_g = np.cumsum(noise_array) for i in range(img.shape[0]): for j in range(img.shape[1]): random_number = np.random.rand() count = -10 for k in cdf_g: if random_number <= k: noise_mat[i,j] = count*23 break count += 1 return noise_mat if __name__ == "__main__": img = cv2.imread("Lenna.jpg") mean = 2 variance = 2.28 prob_noise = 0.50 noise_mat = uniform_noise_add(img, prob_noise, mean, variance) img = img + noise_mat cv2.imwrite("Lenna_uniform_noise.jpg", img)
Image-Restoration-in-Digital-Image-Processing - GitHub
Original image
Uniform image noise
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License