如何在opencv python中使用boxFilter()和ndi.median_filter()来过滤20%的盐和胡椒图像?

时间:2019-04-07 16:41:59

标签: python opencv

我不确定是否正在使用cv.boxFilter()和ndi.median_filter()函数正确过滤20%的盐和胡椒图像?高斯滤波器似乎正在工作。我不知道中值过滤器是否正常工作,因为图像没有盐和胡椒粉。并且“框”过滤器图像在窗口中被涂黑。任何帮助,将不胜感激。到目前为止,我有这个:

import cv2 as cv
import numpy as np
import skimage.util.noise as noise
import scipy.ndimage as ndi

img = cv.imread('opencv-logo.jpg',0)

# Add 20 percent salt and peppernoise
spImg = noise.random_noise(img, mode='s&p', amount=0.2)
cv.imshow('Salt and Pepper image', spImg)

# Box Filter
box = cv.boxFilter(spImg,0,(5,5), spImg, (-1,-1))
cv.imshow('Box Filter', box)

# Median Filter
median = ndi.median_filter(spImg,5)
cv.imshow('Median Filter', median)

# Gaussian Filter
blur = cv.GaussianBlur(spImg,(5,5),0)
cv.imshow('Gaussian Filter', blur)

cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)  # If I dont have this line then the window won't close for me on my Mac when I enter any key

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确,但是中值滤波器可以消除所有盐和胡椒粉的噪音,不是吗?我刚运行了程序,“噪点”图像在中值滤波器输出中被去噪了,不是您想要的吗?