import cv2
import numpy as np
img = cv2.imread('D:/pythonob/imageinpaint/img/noise.jpg',1)
gauss = cv2.GaussianBlur(img,(5,5),1.5)#高斯滤波
#均值滤波:将每个像素取周围像素的平均值
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mean = np.zeros((height,width,3),np.uint8)
for i in range(0,height-3):
for j in range(0,width-3):
bm = int(0)
gm = int(0)
rm = int(0)
for m in range(-3,3):
for n in range(-3,3):
(b,g,r) = img[i+m,j+n]
bm = bm + b
gm = gm + g
rm = rm + r
b = np.uint8(bm / 36)
g = np.uint8(gm / 36)
r = np.uint8(rm / 36)
mean[i,j] = (b,g,r)
cv2.imshow('src',img)
cv2.imshow('mean',mean)
cv2.imshow('gauss',gauss)
cv2.waitKey(0)
效果图: