01: Gaussian noise and salt and pepper noise


foreword

Record the python program of handwritten salt and pepper noise and Gaussian noise.
The renderings are as follows:
insert image description here


1. What are Gaussian noise and salt and pepper noise?

Both salt and pepper noise and Gaussian noise are common types of noise in digital image processing.
1. Salt and pepper noise is random black and white pixels mixed in the image, making some pixels in the image very obvious and irregular. Salt and pepper noise may be generated due to sensor damage, transmission errors, compression algorithms, etc.
2. Gaussian noise is caused by random noise in the image sensor , which is random and follows a Gaussian distribution . It causes small random variations in the brightness and color of the image, as well as blurring and distortion. These two types of noise will affect the quality and accuracy of the image and need to be denoised in digital image processing.

2. Write the program

1. Analyze the characteristics of the two

1. Salt and pepper noise is bright and dark noise in the image, that is, the pixel value of 1, 0 (in the range of 0-1), then the
idea is: in an empty matrix with the same size as the original image, fill a part of 0 , 1 and overlay these on the original image.
The code is as follows (example):

def salt_and_pepper_noise(image, prob):
    """
        噪声函数
       :param image: 原图像,是灰度图
       :param prob: 控制椒盐噪声的数量,这里是0-1的一个概率值
       :return: 处理后的图像
    """
    h , w = image.shape[:2]
    noise = np.zeros((h, w), dtype=np.uint8)
    #将noise随机填充0-255的值
    cv2.randu(noise, 0, 255)
    #将image传给image_copy
    image_copy = image.copy()
    #prob*255就是我们选的那个阈值
    image_copy[np.where(noise < prob*255)] = 0
    image_copy[np.where(noise > (1-prob)*255)] = 255
    return image_copy

2. The most obvious feature of Gaussian noise is that it obeys the Gaussian distribution, and this part of the principle will not be repeated.

def gaussian_noise(image, mean=0, var=0.1):
    """
    给输入的图像添加高斯噪声
    :param image: 输入图像,0-255的灰度图
    :param mean: 高斯噪声的均值,默认为0
    :param var: 高斯噪声的标准差,默认为0.1
    :return: 添加高斯噪声后的图像
    """
    #先把图像转化为0-1,并将类型转化为float32,这样有利于保存数据,
    image = np.asarray(image / 255, dtype=np.float32)
    #为了后面加方便,noise也要转化为float32
    noise = np.random.normal(mean, var,image.shape).astype(np.float32)
    noisy_image = image + noise
    #将noise_image限制在0-255,因为+运算可能有部分会超过255,再转化为整型
    noisy_image = np.clip(noisy_image*255, 0, 255).astype(np.uint8)
    return noisy_image

2. Call two functions to add noise

import numpy as np
import cv2
#椒盐噪声
def salt_and_pepper_noise(image, prob):
    """
        噪声函数
       :param image: 原图像,是灰度图
       :param prob: 控制椒盐噪声的数量,这里是0-1的一个概率值
       :return: 处理后的图像
    """
    h , w = image.shape[:2]
    noise = np.zeros((h, w), dtype=np.uint8)
    #将noise随机填充0-255的值
    cv2.randu(noise, 0, 255)
    #将image传给image_copy
    image_copy = image.copy()
    image_copy[np.where(noise < prob*255)] = 0
    image_copy[np.where(noise > (1-prob)*255)] = 255
    return image_copy
#高斯噪声
def gaussian_noise(image, mean=0, var=0.1):
    """
    给输入的图像添加高斯噪声
    :param image: 输入图像
    :param mean: 高斯噪声的均值,默认为0
    :param var: 高斯噪声的标准差,默认为0.1
    :return: 添加高斯噪声后的图像
    """
    image = np.asarray(image / 255, dtype=np.float32)
    noise = np.random.normal(mean, var,image.shape).astype(np.float32)
    noisy_image = image + noise
    noisy_image = np.clip(noisy_image*255, 0, 255).astype(np.uint8)
    return noisy_image

insert image description here

Summarize

This is the whole content of this article. Two seemingly simple functions, in fact, there are still many problems in handwriting, and the basic skills are still not solid. I hope everyone will also focus on the basic skills of programming.

Guess you like

Origin blog.csdn.net/CSDN_Yangk/article/details/129777855