How to calculate a Gaussian kernel matrix efficiently in numpy?

前端 未结 12 2268
名媛妹妹
名媛妹妹 2020-11-29 20:54
def GaussianMatrix(X,sigma):
    row,col=X.shape
    GassMatrix=np.zeros(shape=(row,row))
    X=np.asarray(X)
    i=0
    for v_i in X:
        j=0
        for v_j i         


        
12条回答
  •  自闭症患者
    2020-11-29 21:00

    You may simply gaussian-filter a simple 2D dirac function, the result is then the filter function that was being used:

    import numpy as np
    import scipy.ndimage.filters as fi
    
    def gkern2(kernlen=21, nsig=3):
        """Returns a 2D Gaussian kernel array."""
    
        # create nxn zeros
        inp = np.zeros((kernlen, kernlen))
        # set element at the middle to one, a dirac delta
        inp[kernlen//2, kernlen//2] = 1
        # gaussian-smooth the dirac, resulting in a gaussian filter mask
        return fi.gaussian_filter(inp, nsig)
    

提交回复
热议问题