中值滤波器属于非线性滤波器,其响应基于滤波器所在区域像素的排序中值来决定,中值滤波器对椒盐噪声有比较优秀的降噪能力,同时相比于线性平滑滤波器对图像的模糊程度小很多
中值滤波器计算过程为,对图像某点执行中值滤波,首先要对邻域内像素进行排序,确定它们的中值,并将中值赋值给滤波后的中心像素,例如在一个 滤波器中,中值是第5大的值,在滤波器中,中值是第13大的值
下面以被椒盐噪声污染的电路板图像为例,展示中值滤波的效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import numpy as np import cv2 as cv from matplotlib import pyplot as plt
def median_kernel(matrix): return int(np.median(matrix))
def median_filtering(im, kernel_size): n = kernel_size im_padding = np.zeros((im.shape[0]+2*(n-1), im.shape[1]+2*(n-1))) im_padding[(n-1):(n+im.shape[0]-1), (n-1):(n+im.shape[1]-1)] = im im_conv = np.zeros((im_padding.shape[0]-n+1, im_padding.shape[1]-n+1)) for i in range(im_padding.shape[0]-n+1): for j in range(im_padding.shape[1]-n+1): neighbor = im_padding[i:i+n, j:j+n] im_conv[i, j] = median_kernel(neighbor) pos = int((n-1)/2) im_new = im_conv[pos:(pos+im.shape[0]), pos:(pos+im.shape[1])] return im_new
|
读取原始图像并显示
1 2 3
| im = cv.imread('images/01.tif', 0) plt.title('origin') plt.imshow(im, cmap='gray')
|
进行中值滤波处理
1 2 3 4
| im_k3 = median_filtering(im, 3) plt.figure() plt.title('kernel size: 3') plt.imshow(im_k3, cmap='gray')
|