fromGrayscaleImageimportGrayscaleImagefromscipy.ndimageimportconvolve1dfrompylabimportceil,zeros,pi,e,exp,sqrt,arrayclassGaussianFilter:def__init__(self,scale):self.scale=scaledefgaussian(self,x):'''Return the value of a 1D Gaussian function for a given x and scale'''returnexp(-(x**2/(2*self.scale**2)))/(sqrt(2*pi)*self.scale)defget_1d_gaussian_kernel(self):'''Sample a one-dimensional Gaussian function of scale s'''radius=int(ceil(3*self.scale))size=2*radius+1result=zeros(size)# Sample the Gaussian function result=array([self.gaussian(x-radius)forxinxrange(size)])# The sum of all kernel values is equal to oneresult/=result.sum()returnresultdefget_filtered_copy(self,image):'''Apply a gaussian blur to an image, to suppress noise.'''kernel=self.get_1d_gaussian_kernel()image=convolve1d(image.data,kernel,axis=0,mode='nearest')returnGrayscaleImage(None,convolve1d(image,kernel,axis=1,mode='nearest'))defget_scale(self):returnself.scaledefset_scale(self,scale):self.scale=float(scale)scale=property(get_scale,set_scale)