PDF (Probability Density Function) Noise

Gaussian Noise: Gaussian Noise | Hasty.ai

from matplotlib import pyplot
from PIL import Image
import numpy as np

url=r"C:\Users\jimore\Pictures\girl.jpeg"
img=np.random.rand(100,100)
# img=np.array(Image.open(url))
print(img)
pyplot.figure(1)
pyplot.imshow(img)
Gaussian_noise=np.random.normal(22,10,img.shape)
print(Gaussian_noise)
noisy_img=img+Gaussian_noise
print(noisy_img)
pyplot.figure(2)
pyplot.imshow(noisy_img)
clean_img=noisy_img-Gaussian_noise
pyplot.figure(3)
pyplot.imshow(clean_img)
pyplot.show()

Rayleigh Noise: How To Make Rayleigh Noise On Image - C# Guide - Epoch Abuse

from matplotlib import pyplot
from PIL import Image
import numpy as np

url=r"C:\Users\jimore\Pictures\girl.jpeg"
import numpy as np

def generate_rayleigh_noise(amplitude, size):
    """
    生成瑞利噪声

    参数:
    - amplitude: 瑞利噪声的振幅(决定了噪声强度)
    - size: 生成噪声的大小(即生成数组的形状)

    返回值:
    - rayleigh_noise: 生成的瑞利噪声数组
    """

    # 生成高斯分布的随机数
    gaussian_noise = np.random.randn(*size)

    # 计算瑞利噪声
    rayleigh_noise = amplitude * np.sqrt(2 * (gaussian_noise**2))

    return rayleigh_noise

# 设置参数
amplitude = 0.1  # 振幅

url=r"C:\Users\jimore\Pictures\girl.jpeg"
img=np.array(Image.open(url))/255
pyplot.figure(1)
pyplot.imshow(img)

# 生成瑞利噪声
noise = generate_rayleigh_noise(amplitude, img.shape)

# 打印噪声数组
print(noise)
print(noise.shape)
pyplot.figure(2)
pyplot.imshow(noise)

noisy_img=img+noise
pyplot.figure(3)
pyplot.imshow(noisy_img)

pyplot.show()

Salt-and-Pepper Noise: Add a "salt and pepper" noise to an image with Python - GeeksforGeeks

import random
import cv2
from matplotlib import pyplot


def add_noise(img):
    # Getting the dimensions of the image
    row, col = img.shape

    # Randomly pick some pixels in the
    # image for coloring them white
    # Pick a random number between 300 and 10000
    number_of_pixels = random.randint(300, 10000)
    for i in range(number_of_pixels):
        # Pick a random y coordinate
        y_coord = random.randint(0, row - 1)

        # Pick a random x coordinate
        x_coord = random.randint(0, col - 1)

        # Color that pixel to white
        img[y_coord][x_coord] = 255

    # Randomly pick some pixels in
    # the image for coloring them black
    # Pick a random number between 300 and 10000
    number_of_pixels = random.randint(300, 10000)
    for i in range(number_of_pixels):
        # Pick a random y coordinate
        y_coord = random.randint(0, row - 1)

        # Pick a random x coordinate
        x_coord = random.randint(0, col - 1)

        # Color that pixel to black
        img[y_coord][x_coord] = 0

    return img


# salt-and-pepper noise can
# be applied only to grayscale images
# Reading the color image in grayscale image
img = cv2.imread(r"C:\Users\jimore\Pictures\girl.jpeg",cv2.IMREAD_GRAYSCALE)
pyplot.figure(1)
pyplot.imshow(img)

salt_and_pepper_img=add_noise(img)
# Showing the image
pyplot.figure(2)
pyplot.imshow(salt_and_pepper_img)
pyplot.show()

Gamma Noise: How To Make Gamma Noise On Images - C# Guide - Epoch Abuse

import cv2
import numpy as np

def generate_gamma_noise(shape, alpha, beta):
    """
    生成Gamma噪声

    参数:
    shape:噪声数组的形状
    alpha:Gamma分布的形状参数
    beta:Gamma分布的尺度参数

    返回值:
    gamma_noise:生成的Gamma噪声数组
    """
    gamma_noise = np.random.gamma(alpha, beta, shape)
    return gamma_noise

def add_gamma_noise_to_image(image, alpha, beta):
    """
    将Gamma噪声添加到图像

    参数:
    image:输入的图像数组
    alpha:Gamma分布的形状参数
    beta:Gamma分布的尺度参数

    返回值:
    noisy_image:添加Gamma噪声后的图像数组
    """
    noise = generate_gamma_noise(image.shape, alpha, beta)
    noisy_image = np.clip(image + noise, 0, 255).astype(np.uint8)
    return noisy_image

# 读取图像
image = cv2.imread(r"C:\Users\jimore\Pictures\girl.jpeg", cv2.IMREAD_COLOR)

# 转换为浮点数类型,并将像素值缩放到[0, 1]范围
image = image.astype(np.float32) / 255.0

# 添加Gamma噪声
alpha = 1.0  # Gamma分布的形状参数
beta = 1.0  # Gamma分布的尺度参数
noisy_image = add_gamma_noise_to_image(image, alpha, beta)

# 将图像还原为整数类型(0-255范围)
noisy_image = (noisy_image * 255).astype(np.uint8)

# 显示原始图像和添加噪声后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Noisy Image', noisy_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Exponential Noise: How To Make Exponential Noise On Image - C# Guide - Epoch Abuse

from matplotlib import pyplot as plt
import cv2
import numpy as np

# # 9.4:指数噪声 (Exponential noise)
img = cv2.imread(r"C:\Users\jimore\Pictures\girl.jpeg", 0)  # flags=0 读取为灰度图像
# img = np.ones([256, 256]) * 128

a = 50.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))  # 归一化为 [0,255]

plt.figure(figsize=(9, 3))
plt.subplot(131), plt.title("Origin"), plt.axis('off')
plt.imshow(img, 'gray', vmin=0, vmax=255)
plt.subplot(132), plt.title("Exponential noise"), plt.axis('off')
plt.imshow(imgExponentNoise, 'gray')
plt.subplot(133), plt.title("Gray hist")
histNP, bins = np.histogram(imgExponentNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.tight_layout()
plt.show()

Uniform Noise: How To Make Uniform Noise On Images - C# Guide - Epoch Abuse

from matplotlib import pyplot as plt
import cv2
import numpy as np

# # 9.4:指数噪声 (Exponential noise)
img = cv2.imread(r"C:\Users\jimore\Pictures\girl.jpeg", 0)  # flags=0 读取为灰度图像
# img = np.ones([256, 256]) * 128

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))  # 归一化为 [0,255]

plt.figure(figsize=(9, 3))
plt.subplot(131), plt.title("Origin"), plt.axis('off')
plt.imshow(img, 'gray', vmin=0, vmax=255)
plt.subplot(132), plt.title("Uniform noise"), plt.axis('off')
plt.imshow(imgUniformNoise, 'gray')
plt.subplot(133), plt.title("Gray hist")
histNP, bins = np.histogram(imgUniformNoise.flatten(), bins=255, range=[0, 255], density=True)
plt.bar(bins[:-1], histNP[:])
plt.tight_layout()
plt.show()

猜你喜欢

转载自blog.csdn.net/qq_55126913/article/details/131578332