[python + opencv image processing 1] Adding noise to the image

several common noises

Gaussian noise

The probability density function obeys the noise of Gaussian distribution.
Causes:
1) The image sensor is not bright enough and the brightness is not uniform enough when shooting;
2) The components of the circuit have their own noise and mutual influence;
3) The image sensor has been working for a long time and the temperature is too high

Code:

def gasuss_noise(image,mean=0,var=0.001):
    '''
    手动添加高斯噪声
    mean : 均值
    var : 方差
    '''
    image = np.array(image/255, dtype=float)
    noise = np.random.normal(mean, var ** 0.5, image.shape)  # 正态分布
    out = image + noise
    if out.min() < 0:
        low_clip = -1.
    else:
        low_clip = 0.
    out = np.clip(out, low_clip, 1.0)
    out = np.uint8(out*255)
    return out

salt and pepper noise

Salt and pepper noise, salt and pepper noise is also called impulse noise, which randomly changes some pixel values, and is black and white bright and dark point noise generated by image sensors, transmission channels, decoding processing, etc.
Salt and pepper noise is often caused by image cutting.

def sp_noise(image,prob):
    '''
    手动添加椒盐噪声
    prob:噪声比例
    '''
    output = np.zeros(image.shape, np.uint8)
    thres = 1 - prob
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            rdn = random.random()
            if rdn < prob:
                output[i][j] = 0
            elif rdn > thres:
                output[i][j] = 255
            else:
                output[i][j] = image[i][j]
    return output

Call the implementation of the library directly

import skimage
from skimage import util

def api_img(img,mode):
    '''
    直接调用skimage库函数,对图片进行加噪
    :param img: 读入图像
    :param mode:
    可选择,str型,表示要添加的噪声类型
        gaussian:高斯噪声
        localvar:高斯分布的加性噪声,在“图像”的每个点处具有指定的局部方差。
        poisson:泊松再生
        salt:盐噪声,随机将像素值变成1
        pepper:椒噪声,随机将像素值变成0或-1,取决于矩阵的值是否带符号
        s&p:椒盐噪声
        speckle:均匀噪声(均值mean方差variance),out=image+n*image
    :return: 加噪后的图像
    '''
    noise_gs_img = util.random_noise(img, mode)
    noise_gs_img = noise_gs_img * 255  # 由于输出是[0,1]的浮点型,先转成灰度图(我的输入就是灰度图)
    noise_gs_img = noise_gs_img.astype(np.int16)  # 再变成整型数组
    return noise_gs_img

Among them, the random noise function is introduced

def random_noise(image, mode='gaussian', seed=None, clip=True, **kwargs):
功能:为浮点型图片添加各种随机噪声
参数:
image:输入图片(将会被转换成浮点型),ndarray型
mode: 可选择,str型,表示要添加的噪声类型
	gaussian:高斯噪声
	localvar:高斯分布的加性噪声,在“图像”的每个点处具有指定的局部方差。
	poisson:泊松再生
	salt:盐噪声,随机将像素值变成1
	pepper:椒噪声,随机将像素值变成0-1,取决于矩阵的值是否带符号
	s&p:椒盐噪声
	speckle:均匀噪声(均值mean方差variance),out=image+n*image
seed: 可选的,int型,如果选择的话,在生成噪声前会先设置随机种子以避免伪随机
clip: 可选的,bool型,如果是True,在添加均值,泊松以及高斯噪声后,会将图片的数据裁剪到合适范围内。如果谁False,则输出矩阵的值可能会超出[-1,1]
mean: 可选的,float型,高斯噪声和均值噪声中的mean参数,默认值=0
var:  可选的,float型,高斯噪声和均值噪声中的方差,默认值=0.01(注:不是标准差)
local_vars:可选的,ndarry型,用于定义每个像素点的局部方差,在localvar中使用
amount: 可选的,float型,是椒盐噪声所占比例,默认值=0.05
salt_vs_pepper:可选的,float型,椒盐噪声中椒盐比例,值越大表示盐噪声越多,默认值=0.5,即椒盐等量
--------
返回值:ndarry型,且值在[0,1]或者[-1,1]之间,取决于是否是有符号数
-------
注意:略(见源码)

main calls the above function using:

if __name__ == "__main__":
    img = cv2.imread('../pic/rabbit.jpg')
    out1 = api_img(img, "gaussian")
    out2 = api_img(img, "s&p")
    out3 = gasuss_noise(img)
    out4 = sp_noise(img, 0.05)
    cv2.imwrite('../out_pic/api_gs.jpg', out1)
    cv2.imwrite('../out_pic/api_sp.jpg', out2)
    cv2.imwrite('../out_pic/gs.jpg', out3)
    cv2.imwrite('../out_pic/sp.jpg', out4)

final result:

Original image:

Manually add Gaussian noise:

call the Gaussian noise of api
API Gaussian
Manually add salt and pepper noise
salt and pepper
Call the salt and pepper noise of api
api salt and pepper

Guess you like

Origin blog.csdn.net/poppyty/article/details/118410338