1滤波
1:均值滤波
均值滤波是一种线性滤波器,处理思路也很简单,就是将一个窗口区域中的像素计算平均值,然后将窗口中计算得到的均值设置为锚点上的像素值。
该算法有优点在于效率高,思路简单。同样,缺点也很明显,计算均值会将图像中的边缘信息以及特征信息“模糊”掉,会丢失很多特征。
均值滤波使用简单的卷积方案来实现,既然是计算窗口区域中的像素和,即使用如下卷积核即可。图像的边界部分采用padding操作处理。另外,得到的锚点像素值要进行归一化,即除以窗口尺寸大小。在OpenCV中,我们使用cv.blur()这个函数即可实现。
2:中值滤波
中值滤波是一种非线性滤波,在处理脉冲噪声以及椒盐噪声时效果极佳,能够有效的保护好图像的边缘信息。
在OpenCV中,我们使用cv.medianBlur()这个函数即可实现。
3.filter2D

典型代码如下
#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#均值滤波,求平均值
def blur_test(img):
#dst = cv.blur(img,(1,3))
#dst = cv.blur(img,(1,15))
#dst = cv.blur(img,(15,1))
dst = cv.blur(img,(5,5))
return dst
#中值滤波,找中值
def median_blur_test(img):
dst = cv.medianBlur(img,5)
return dst
#定义核滤波
def filter2d_blur_test(img):
#kernel = np.ones([5,5],np.float32)/25
#kernel = np.array([[1,1,1],[1,1,1],[1,1,1]],np.float32)/9
#下面的核为锐化
#kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]],np.float32)/1
kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]],np.float32)
dst = cv.filter2D(img,-1,kernel = kernel)
return dst
def img_test():
img = cv.imread('E:/chenopencvblogimg/lena.jpg')
#判断是否读取成功
if img is None:
print("Could not read the image,may be path error")
return
cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
cv.imshow("origin Pic",img)
img_show = blur_test(img)
cv.namedWindow("blur_test",cv.WINDOW_NORMAL)
cv.imshow("blur_test",img_show)
img_show = median_blur_test(img)
cv.namedWindow("median_blur_test",cv.WINDOW_NORMAL)
cv.imshow("median_blur_test",img_show)
img_show = filter2d_blur_test(img)
cv.namedWindow("filter2d_blur_test",cv.WINDOW_NORMAL)
cv.imshow("filter2d_blur_test",img_show)
#让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
cv.waitKey(0)
#销毁窗口
cv.destroyAllWindows()
if __name__ == '__main__':
sys.exit(img_test() or 0)
2添加高斯噪声
#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#0--255之间的限定
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
#添加高斯噪声
def gaussian_noise(img):
h,w,c = img.shape
for row in range(h):
for col in range(w):
s = np.random.normal(0,20,3)
b = img[row,col,0]
g = img[row,col,1]
r = img[row,col,2]
img[row,col,0] = clamp(b+s[0])
img[row,col,1] = clamp(g+s[1])
img[row,col,2] = clamp(r+s[2])
return img
def img_test():
img = cv.imread('E:/chenopencvblogimg/lena.jpg')
#判断是否读取成功
if img is None:
print("Could not read the image,may be path error")
return
cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
cv.imshow("origin Pic",img)
t1 = cv.getTickCount()
img_show = gaussian_noise(img)
t2 = cv.getTickCount()
time = (t2- t1)/cv.getTickFrequency()
print("time used: %s ms"%(time*1000))
cv.namedWindow("gaussian_noise",cv.WINDOW_NORMAL)
cv.imshow("gaussian_noise",img_show)
#让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
cv.waitKey(0)
#销毁窗口
cv.destroyAllWindows()
if __name__ == '__main__':
sys.exit(img_test() or 0)

3对添加高斯噪声的图像滤波

#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#0--255之间的限定
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
#添加高斯噪声
def gaussian_noise(img):
h,w,c = img.shape
for row in range(h):
for col in range(w):
s = np.random.normal(0,20,3)
b = img[row,col,0]
g = img[row,col,1]
r = img[row,col,2]
img[row,col,0] = clamp(b+s[0])
img[row,col,1] = clamp(g+s[1])
img[row,col,2] = clamp(r+s[2])
return img
def img_test():
img = cv.imread('E:/chenopencvblogimg/lena.jpg')
#判断是否读取成功
if img is None:
print("Could not read the image,may be path error")
return
cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
cv.imshow("origin Pic",img)
t1 = cv.getTickCount()
img_show = gaussian_noise(img)
t2 = cv.getTickCount()
time = (t2- t1)/cv.getTickFrequency()
print("time used: %s ms"%(time*1000))
cv.namedWindow("gaussian_noise",cv.WINDOW_NORMAL)
cv.imshow("gaussian_noise",img_show)
#img_show = cv.GaussianBlur(img,(0,0),15)
#img_show = cv.GaussianBlur(img,(0,0),1)
img_show = cv.GaussianBlur(img,(5,5),0)
cv.namedWindow("GaussianBlur",cv.WINDOW_NORMAL)
cv.imshow("GaussianBlur",img_show)
#让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
cv.waitKey(0)
#销毁窗口
cv.destroyAllWindows()
if __name__ == '__main__':
sys.exit(img_test() or 0)

本文介绍了图像处理中的滤波技术,包括均值滤波、中值滤波及自定义滤波器,并演示了如何使用OpenCV实现这些滤波器。此外,还展示了如何添加并去除高斯噪声。
1590

被折叠的 条评论
为什么被折叠?



