Skip to content

Instantly share code, notes, and snippets.

@YHaruoka
Created April 16, 2022 07:20
import numpy as np
import matplotlib.pyplot as plt
import cv2
# Loading image data (GreyScale)
image = cv2.imread('data/lenna.png', cv2.IMREAD_GRAYSCALE)
# Generating noise
mean = 0;
sigma = 10;
noise = np.random.normal(0, sigma, np.shape(image))
# Noise is added to image
image = image + noise
# Pixel value adjustment [0,255]
image[image > 255] = 255
image[image < 0] = 0
# Saving image
output_image = image.astype(np.uint8) # Float -> Uint
cv2.imwrite('output.jpg',output_image)
# Creating noise histogram
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hist(noise, bins=120, Color = "b", rwidth=1)
ax.set_title('Noise histgram $\mu=' + str(mean) + ',\ \sigma=' + str(sigma) +'$')
ax.set_xlabel('noise signal value')
ax.set_ylabel('pixel count')
fig.savefig("noise_histgram.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment