数字图像处理——均值滤波器【像素级别处理】(python)
文章目录
数字图像处理——均值滤波器【像素级别处理】(python) 原理 代码实现
原理
简介: 均值滤波器(平滑线性滤波器)是在指定邻域内对其像素点求平均值,然后代替相同位置的像素点,若超过原来的图片范围,则用0来补全。
代码实现
输入:
import cv2import numpy as npimport matplotlib.pyplot as plt img = cv2.imread('Fig0333.tif') # 测试图片H = img.shape[0]W = img.shape[1]img3 = np.zeros((H, W, 3), np.uint8) # 3*3邻域平滑后的图像img5 = np.zeros((H, W, 3), np.uint8) # 5*5邻域平滑后的图像img9 = np.zeros((H, W, 3), np.uint8) # 9*9邻域平滑后的图像img15 = np.zeros((H, W, 3), np.uint8) # 15*15邻域平滑后的图像img35 = np.zeros((H, W, 3), np.uint8) # 35*35邻域平滑后的图像imgs = [img3, img5, img9, img15, img35]size = [1, 2, 4, 7, 17] # 邻域的一半for id in range(5): # 五个图片for i in range(H):for j in range(W):sum = 0count = 0for m in range(-1 * size[id], size[id] + 1):for n in range(-1 * size[id], size[id] + 1):if 0 <= i + m < H and 0 <= j + n < W: # 这个if循环避免了补0的操作count += 1sum += img[i + m, j + n, 0]imgs[id][i, j, 0] = sum // count imgs[id][i, j, 1] = imgs[id][i, j, 0]imgs[id][i, j, 2] = imgs[id][i, j, 0]# 原图plt.subplot(2, 3, 1)plt.axis('off')plt.title('Original image')plt.imshow(img)# 3*3邻域plt.subplot(2, 3, 2)plt.axis('off')plt.title('3*3 smoothing')plt.imshow(img3)# 5*5邻域plt.subplot(2, 3, 3)plt.axis('off')plt.title('5*5 smoothing')plt.imshow(img5)# 9*9邻域plt.subplot(2, 3, 4)plt.axis('off')plt.title('9*9 smoothing')plt.imshow(img9)# 15*15邻域plt.subplot(2, 3, 5)plt.axis('off')plt.title('15*15 smoothing')plt.imshow(img15)# 35*35邻域plt.subplot(2, 3, 6)plt.axis('off')plt.title('35*35 smoothing')plt.imshow(img35)plt.show()
输出:
查看更多关于数字图像处理——均值滤波器【像素级别处理】(python)的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did127598