一. 中值滤波:
中值滤波器是一种可以使图像平滑的滤波器。它使用滤波器范围内的像素的中值去代表该范围内所有的像素。中值滤波是消除图像噪声最常见的手段之一,特别是消除椒盐噪声,中值滤波的效果要比均值滤波更好。
二. python实现中值滤波和均值滤波,并用两种滤波器对受到椒盐噪声污染的图像进行去噪
import cv2 import numpy as np # Median filter def median_filter(img, K_size=3): H, W, C = img.shape ## Zero padding pad = K_size // 2 out = np.zeros((H + pad*2, W + pad*2, C), dtype=np.float) out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float) tmp = out.copy() # filtering for y in range(H): for x in range(W): for c in range(C): out[pad+y, pad+x, c] = np.median(tmp[y:y+K_size, x:x+K_size, c]) out = out[pad:pad+H, pad:pad+W].astype(np.uint8) return out # Average filter def average_filter(img, G=3): out = img.copy() H, W, C = img.shape Nh = int(H / G) Nw = int(W / G) for y in range(Nh): for x in range(Nw): for c in range(C): out[G*y:G*(y+1), G*x:G*(x+1), c] = np.mean(out[G*y:G*(y+1), G*x:G*(x+1), c]).astype(np.int) return out # Read image img = cv2.imread("../paojie_sp.jpg") # Median Filter and Average Filter out1 = median_filter(img, K_size=3) out2 = average_filter(img,G=3) # Save result cv2.imwrite("out1.jpg", out1) cv2.imwrite("out2.jpg", out2) cv2.waitKey(0) cv2.destroyAllWindows()
三. 实验结果
可以明显看出,对于受到椒盐噪声污染的图像,中值滤波往往比均值滤波的去噪效果要好!
四. 参考内容: