# 中值滤波 3*3 的小方块对像素排序,选取中间的那个来代替
import cv2
import numpy as np
img = cv2.imread('image11.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imshow('src',img)
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): #遍历3*3
for n in range(-1,2):
gray = img[i+m,j+n] #获取当前像素值
collect[k] = gray #放到collect中
k = k+1
# 0 1 2 3 4 5 6 7 8
# 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('dst',dst)
cv2.waitKey(0)
opecv入门:4.7图像美化-中值滤波
最新推荐文章于 2024-09-26 23:00:36 发布