# [OpenCV complete routine] 93 Histogram of noise model

## [OpenCV complete routine] 93 Histogram of noise model

Welcome to pay attention "100 complete OpenCV routines" Series, continuously updating
Welcome to pay attention "Python Xiaobai's OpenCV learning course" Series, continuously updating

## 2. Noise model

The noise source in digital image mainly comes from the process of image acquisition and transmission. When acquiring an image, the light level and sensor temperature affect the noise in the image. When transmitting an image, the interference in the transmission channel pollutes the image.

### 2.7 summary of image noise

The above noise model provides an effective tool for the modeling of various image noise. For example:

Gaussian noise: it is often used in the modeling of noise caused by electronic circuit and sensor noise (caused by insufficient illumination and / or high temperature);

Rayleigh noise: commonly used for noise modeling in range imaging;

Exponential noise and gamma noise: commonly used in noise modeling in laser imaging;

Impulse noise: it is often used for noise modeling of fast transients (such as switch failure) during imaging;

Uniform noise: it is the most basic description of the actual situation and the basis of various random number generators in digital image processing.

### Routine 9.7: histogram distribution of various noise models

```# OpenCVdemo09.py
# Demo09 of OpenCV
# 9. Image restoration and reconstruction
# Crated: 2021-01-30

# # 9.7: histogram distribution of various noise models

mu, sigma = 0.0, 20.0
noiseGause = np.random.normal(mu, sigma, img.shape)
imgGaussNoise = img + noiseGause
imgGaussNoise = np.uint8(cv2.normalize(imgGaussNoise, None, 0, 255, cv2.NORM_MINMAX))  # Normalized to [0255]

a = 30.0
noiseRayleigh = np.random.rayleigh(a, size=img.shape)
imgRayleighNoise = img + noiseRayleigh
imgRayleighNoise = np.uint8(cv2.normalize(imgRayleighNoise, None, 0, 255, cv2.NORM_MINMAX))  # Normalized to [0255]

a, b = 10.0, 2.5
noiseGamma = np.random.gamma(shape=b, scale=a, size=img.shape)
imgGammaNoise = img + noiseGamma
imgGammaNoise = np.uint8(cv2.normalize(imgGammaNoise, None, 0, 255, cv2.NORM_MINMAX))  # Normalized to [0255]

a = 10.0
noiseExponent = np.random.exponential(scale=a, size=img.shape)
imgExponentNoise = img + noiseExponent
imgExponentNoise = np.uint8(cv2.normalize(imgExponentNoise, None, 0, 255, cv2.NORM_MINMAX))  # Normalized to [0255]

mean, sigma = 10, 100
a = 2 * mean - np.sqrt(12 * sigma)  # a = -14.64
b = 2 * mean + np.sqrt(12 * sigma)  # b = 54.64
noiseUniform = np.random.uniform(a, b, img.shape)
imgUniformNoise = img + noiseUniform
imgUniformNoise = np.uint8(cv2.normalize(imgUniformNoise, None, 0, 255, cv2.NORM_MINMAX))  # Normalized to [0255]

ps, pp = 0.05, 0.02
mask = np.random.choice((0, 0.5, 1), size=img.shape[:2], p=[pp, (1-ps-pp), ps])
imgChoiceNoise = img.copy()

plt.figure(figsize=(12, 8))
plt.subplot(231), plt.title("Gauss noise")
histNP, bins = np.histogram(imgGaussNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.subplot(232), plt.title("Rayleigh noise")
histNP, bins = np.histogram(imgRayleighNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.subplot(233), plt.title("Gamma noise")
histNP, bins = np.histogram(imgGammaNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.subplot(234), plt.title("Exponential noise")
histNP, bins = np.histogram(imgExponentNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.subplot(235), plt.title("Uniform noise")
histNP, bins = np.histogram(imgUniformNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.subplot(236), plt.title("Salt-pepper noise")
histNP, bins = np.histogram(imgChoiceNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.tight_layout()
plt.show()
```

(end of this section)