#选取周围几个像素点,并对其进行排序,选取中间那个值
import cv2
import numpy as np
img = cv2.imread('D:/pythonob/imageinpaint/img/noise.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,3),np.uint8)
collect = np.zeros(9,np.uint8)
for i in range (1,height-1):
for j in range(1,width-1):
k = 0
for m in range(-1,2):
for n in range(-1,2):
collect[k] = gray[i+m,j+n]
k = k+1
for k in range(0,9):
p1 = collect[k]
for t in range(k+1,9):
if p1<collect[t]:
mid = collect[t]
collect[t] = p1
p1 = mid
dst[i,j] = collect[4]
cv2.imshow('gray',gray)
cv2.imshow('mid',dst)
cv2.waitKey(0)
效果图: