✨博客主页:米开朗琪罗~
✨博主爱好:羽毛球
✨年轻人要:Living for the moment(活在当下)!
推荐专栏:【图像处理】【千锤百炼Python】【深度学习】【排序算法】
高斯噪声指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。
我们需要使用skimage
库中的函数为图像添加高斯噪声。
函数式:skimage.util.random_noise(image, mode=‘gaussian’, seed=None, clip=True, **kwargs)
参数介绍:
函数式:skimage.util.random_noise(image, mode=‘gaussian’, seed=None, clip=True, **kwargs)
参数介绍:
image:输入图像,类型为ndarray;
mode:噪声类别,有以下几种:
'gaussian':高斯噪声;
'localvar':高斯分布的加性噪声,在图像每个点都有特点的局部方差;
'poisson':泊松噪声;
'salt':盐噪声,随机将图像像素值变为1;
'pepper':椒噪声,随机将图像像素值变为0或-1;
's&p':椒盐噪声;
'speckle':均匀噪声,(均值mean方差variance),out=image+n*image,n是具有指定均值和方差的均匀噪声;
seed:可选,int型,如果选择的话,则会在生成噪声前设置随机种子;
clip:可选,bool型,若为True(default)则在加入‘speckle’,‘poisson’,或 ‘gaussian’这三种噪声后,
进行剪切以保证图像数据点都在[0,1]或[-1.1]之间。若为False,则数据可能超出这个范围;
mean:可选,float型,用于’gaussian’和‘speckle’的均值设置,默认为0;
var:可选,float型,用于’gaussian’和‘speckle’的方差设置,默认为0.01;
local_vars:可选,ndarray型,用于‘localvar’的图像每个像素点处的局部方差设置;
amount:可选,float型,用于‘salt’,‘pepper’和‘s&p’的噪声比例,默认为0.05;
salt_vs_pepper:可选,float型,用于's&p'中盐噪声与椒噪声的比例,范围为[0, 1],默认为0.5;
from skimage import util
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
img1 = Image.open(r"G:\picture\lena1.jpg")
img = np.array(img1)
noisy1 = util.random_noise(img, mode='gaussian', mean=0, var=0.01)
noisy2 = util.random_noise(img, mode='gaussian', mean=0.1, var=0.01)
noisy3 = util.random_noise(img, mode='gaussian', mean=0, var=0.2)
plt.subplot(221)
plt.title('original')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(img1)
plt.subplot(222)
plt.title('mean=0, var=0.01')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(noisy1)
plt.subplot(223)
plt.title('mean=0.1, var=0.01')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(noisy2)
plt.subplot(224)
plt.title('mean=0, var=0.2')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(noisy3)
plt.show()
import numpy as np
import cv2
def gaussian_noise(image, 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
image = cv2.imread(r"G:\picture\lena1.jpg")
noisy1 = gaussian_noise(image, mean=0, var=0.01)
noisy2 = gaussian_noise(image, mean=0.1, var=0.01)
noisy3 = gaussian_noise(image, mean=0, var=0.2)
h1 = np.hstack([image, noisy1])
h2 = np.hstack([noisy2, noisy3])
v = np.vstack([h1, h2])
cv2.imshow('out', v)
cv2.waitKey()
椒盐噪声也称为脉冲噪声,是图像中经常见到的一种噪声,它是一种随机出现的白点或者黑点。
from skimage import util
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import cv2
img1 = Image.open(r"G:\picture\lena1.jpg")
img = np.array(img1)
noisy1 = util.random_noise(img, mode='s&p', amount=0.05)
noisy2 = util.random_noise(img, mode='s&p', amount=0.1)
noisy3 = util.random_noise(img, mode='s&p', amount=0.2)
plt.subplot(221)
plt.title('original')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(img1)
plt.subplot(222)
plt.title('amount=0.05')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(noisy1)
plt.subplot(223)
plt.title('amount=0.1')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(noisy2)
plt.subplot(224)
plt.title('amount=0.2')
plt.xticks([]) # remove ticks
plt.yticks([])
plt.imshow(noisy3)
plt.show()
import numpy as np
import cv2
import random
def sp_noise(image, amount):
output = image.copy()
threshold = 1 - amount
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdm = random.random()
if rdm < amount:
output[i][j] = 0
elif rdm > threshold:
output[i][j] = 255
return output
image = cv2.imread(r"G:\picture\lena1.jpg")
noisy1 = sp_noise(image, amount=0.05)
noisy2 = sp_noise(image, amount=0.1)
noisy3 = sp_noise(image, amount=0.2)
h1 = np.hstack([image, noisy1])
h2 = np.hstack([noisy2, noisy3])
v = np.vstack([h1, h2])
cv2.imshow('out', v)
cv2.waitKey()