Python 随笔(1)类的编写,以高斯模板为例

class GaussTemplate: #l类的定义
    def __init__(self, size, variance):  #初始化函数
        self.size=size
        self.variance=variance
        
    def calculate(self, x, y):  #剩下的每个函数开始都有一个self标志
        return 1/(2*math.pi*self.variance)*math.exp(-(x**2+y**2)/(2*self.variance))


    def get_template(self):
        array=np.ones((self.size,self.size))
        n=int(self.size/2)
        totle=0
        for x in range(self.size):
            for y in range(self.size):
                value=self.calculate(x-n,y-n)
                array[x][y]=value
                totle+=value
        for x in range(self.size):
            for y in range(self.size):
                array[x][y]/=totle
        return array

你可能感兴趣的:(Python随笔)