图像噪声
椒盐噪声
概述: 椒盐噪声(salt & pepper noise)是数字图像的一个常见噪声,所谓椒盐,椒就是黑,盐就是白,椒盐噪声就是在图像上随机出现黑色白色的像素。椒盐噪声是一种因为信号脉冲强度引起的噪声,产生该噪声的算法也比较简单。
给一副数字图像加上椒盐噪声的步骤如下:
- 指定信噪比 SNR (其取值范围在[0, 1]之间)
- 计算总像素数目 SP, 得到要加噪的像素数目 NP = SP * (1-SNR)
- 随机获取要加噪的每个像素位置P(i, j)
- 指定像素值为255或者0。
- 重复3,4两个步骤完成所有像素的NP个像素
- 输出加噪以后的图像
高斯噪声
概述: 加性高斯白噪声(Additive white Gaussian noise,AWGN)在通信领域中指的是一种功率谱函数是常数(即白噪声), 且幅度服从高斯分布的噪声信号. 这类噪声通常来自感光元件, 且无法避免.
去噪算法
中值滤波
概述: 中值滤波是一种非线性空间滤波器, 它的响应基于图像滤波器包围的图像区域中像素的统计排序, 然后由统计排序结果的值代替中心像素的值. 中值滤波器将其像素邻域内的灰度中值代替代替该像素的值. 中值滤波器的使用非常普遍, 这是因为对于一定类型的随机噪声, 它提供了一种优秀的去噪能力, 比小尺寸的均值滤波器模糊程度明显要低. 中值滤波器对处理脉冲噪声(也称椒盐噪声)非常有效, 因为该噪声是以黑白点叠加在图像上面的.
与中值滤波相似的还有最大值滤波器和最小值滤波器.
均值滤波
概述: 均值滤波器的输出是包含在滤波掩模领域内像素的简单平均值. 均值滤波器最常用的目的就是减噪. 然而, 图像边缘也是由图像灰度尖锐变化带来的特性, 所以均值滤波还是存在不希望的边缘模糊负面效应.
均值滤波还有一个重要应用, 为了对感兴趣的图像得出一个粗略描述而模糊一幅图像. 这样, 那些较小物体的强度与背景揉合在一起了, 较大物体变得像斑点而易于检测.掩模的大小由即将融入背景中的物体尺寸决定.
代码
#encoding: utf-8
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import math
import random
import cv2
import scipy.misc
import scipy.signal
import scipy.ndimage
def medium_filter(im, x, y, step):
sum_s=[]
for k in range(-int(step/2),int(step/2)+1):
for m in range(-int(step/2),int(step/2)+1):
sum_s.append(im[x+k][y+m])
sum_s.sort()
return sum_s[(int(step*step/2)+1)]
def mean_filter(im, x, y, step):
sum_s = 0
for k in range(-int(step/2),int(step/2)+1):
for m in range(-int(step/2),int(step/2)+1):
sum_s += im[x+k][y+m] / (step*step)
return sum_s
def convert_2d(r):
n = 3
# 3*3 滤波器, 每个系数都是 1/9
window = np.ones((n, n)) / n ** 2
# 使用滤波器卷积图像
# mode = same 表示输出尺寸等于输入尺寸
# boundary 表示采用对称边界条件处理图像边缘
s = scipy.signal.convolve2d(r, window, mode='same', boundary='symm')
return s.astype(np.uint8)
# def convert_3d(r):
# s_dsplit = []
# for d in range(r.shape[2]):
# rr = r[:, :, d]
# ss = convert_2d(rr)
# s_dsplit.append(ss)
# s = np.dstack(s_dsplit)
# return s
def add_salt_noise(img):
rows, cols, dims = img.shape
R = np.mat(img[:, :, 0])
G = np.mat(img[:, :, 1])
B = np.mat(img[:, :, 2])
Grey_sp = R * 0.299 + G * 0.587 + B * 0.114
Grey_gs = R * 0.299 + G * 0.587 + B * 0.114
snr = 0.9
mu = 0
sigma = 0.12
noise_num = int((1 - snr) * rows * cols)
for i in range(noise_num):
rand_x = random.randint(0, rows - 1)
rand_y = random.randint(0, cols - 1)
if random.randint(0, 1) == 0:
Grey_sp[rand_x, rand_y] = 0
else:
Grey_sp[rand_x, rand_y] = 255
Grey_gs = Grey_gs + np.random.normal(0, 48, Grey_gs.shape)
Grey_gs = Grey_gs - np.full(Grey_gs.shape, np.min(Grey_gs))
Grey_gs = Grey_gs * 255 / np.max(Grey_gs)
Grey_gs = Grey_gs.astype(np.uint8)
# 中值滤波
Grey_sp_mf = scipy.ndimage.median_filter(Grey_sp, (8, 8))
Grey_gs_mf = scipy.ndimage.median_filter(Grey_gs, (8, 8))
# 均值滤波
n = 3
window = np.ones((n, n)) / n ** 2
Grey_sp_me = convert_2d(Grey_sp)
Grey_gs_me = convert_2d(Grey_gs)
plt.subplot(321)
plt.title('Grey salt and pepper noise')
plt.imshow(Grey_sp, cmap='gray')
plt.subplot(322)
plt.title('Grey gauss noise')
plt.imshow(Grey_gs, cmap='gray')
plt.subplot(323)
plt.title('Grey salt and pepper noise (medium)')
plt.imshow(Grey_sp_mf, cmap='gray')
plt.subplot(324)
plt.title('Grey gauss noise (medium)')
plt.imshow(Grey_gs_mf, cmap='gray')
plt.subplot(325)
plt.title('Grey salt and pepper noise (mean)')
plt.imshow(Grey_sp_me, cmap='gray')
plt.subplot(326)
plt.title('Grey gauss noise (mean)')
plt.imshow(Grey_gs_me, cmap='gray')
plt.show()
def main():
img = np.array(Image.open('LenaRGB.bmp'))
add_salt_noise(img)
if __name__ == '__main__':
main()
见https://github.com/wangshub/python-image-process