数学之路-python计算实战(15)-机器视觉-滤波去噪(归一化块滤波)
# -*- coding: utf-8 -*- #code:[email protected] #归一化块滤波 import cv2 import numpy as np fn="test3.jpg" myimg=cv2.imread(fn) img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) #加上高斯噪声,能够參考曾经博文中的内容 ...... ...... #滤波去噪 lbimg=cv2.blur(newimg,(3,3)) cv2.imshow(‘src‘,newimg) cv2.imshow(‘dst‘,lbimg) cv2.waitKey() cv2.destroyAllWindows()
右图是加上噪声,左图是去除噪声后,尽管进行了图像模糊,但仍能比較清晰
依据原理,使用第3个脉冲响应函数(也有人称它为核函数),例如以下:
本博客全部内容是原创,假设转载请注明来源
http://blog.csdn.net/myhaspl/
用python实现这个算法
#code:[email protected] #归一化块滤波 ... ... #用第3个脉冲响应函数 a=1/16.0 kernel=a*np.array([[1,2,1],[2,4,2],[1,2,1]]) for y in xrange(1,myh-1): for x in xrange(1,myw-1): lbimg[y,x]=np.sum(kernel*tmpimg[y-1:y+2,x-1:x+2]) print ".",
效果例如以下图
Blurs an image using the normalized box filter.
- C++: void blur(InputArray src, OutputArray dst, Size ksize, Pointanchor=Point(-1,-1), int borderType=BORDER_DEFAULT )
- Python: cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) → dst
Parameters: - src – input image; it can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U,CV_16S, CV_32F or CV_64F.
- dst – output image of the same size and type as src.
- ksize – blurring kernel size.
- anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
- borderType – border mode used to extrapolate pixels outside of the image.
注意,blur函数使用了第1个脉冲响应函数,例如以下:
The function smoothes an image using the kernel:
# -*- coding: utf-8 -*- #code:[email protected] #归一化块滤波 import cv2 import numpy as np fn="test3.jpg" myimg=cv2.imread(fn) img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) #加上椒盐噪声 #灰阶范围 w=img.shape[1] h=img.shape[0] newimg=np.array(img) #噪声点数量 noisecount=100000 for k in xrange(0,noisecount): xi=int(np.random.uniform(0,newimg.shape[1])) xj=int(np.random.uniform(0,newimg.shape[0])) newimg[xj,xi]=255 #滤波去噪 lbimg=cv2.blur(newimg,(5,5)) cv2.imshow(‘src‘,newimg) cv2.imshow(‘dst‘,lbimg) cv2.waitKey() cv2.destroyAllWindows()
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。