环境:(ubuntu18.04LTS)Anaconda3+python3.7.4+opencv3.4.2
图像滤波等效于对图像的每个像素进行卷积运算,原函数为图像像素,卷积核为滤波器,计算结果为滤波后图像,如下图所示。
常用的滤波器包括:
线性滤波器
均值滤波:滤波结果=原像素邻域内所有像素的平均值
高斯滤波:滤波结果=原像素邻域内所有像素的加权平均值
非线性滤波器
中值滤波:滤波结果=原像素邻域内所有像素的中值(可以有效去除椒盐噪声)
"""
Greated on Mon Nov.24 2019
Test - different filters
@author: Lihoon
"""
import cv2 as cv
ksize = (5, 5)
#read source image and check
filename = "/home/lihoon/code/pycharm/lena.jpg"
img = cv.imread(filename)
if img is None:
print("Image read error!")
else:
print("Image read successful!")
#show source image
cv.imshow('source', img)
#box filter
box = cv.boxFilter(img, -1, ksize)
cv.imshow('box filter', box)
#mean filter
mean = cv.blur(img, ksize)
cv.imshow('mean filter',mean)
#median filter
median = cv.medianBlur(img, 5)
cv.imshow('median filter', median)
#Gaussian filter
gaussian = cv.GaussianBlur(img, ksize, 0, 0)
cv.imshow('Gaussion filter', gaussian)
cv.waitKey()
cv.destroyAllWindows()