How to gauss-filter (blur) a floating point numpy array

后端 未结 3 694
小鲜肉
小鲜肉 2021-02-01 04:39

I have got a numpy array a of type float64. How can I blur this data with a Gauss filter?

I have tried

from PIL import Image,          


        
3条回答
  •  别跟我提以往
    2021-02-01 05:10

    Here is my approach using only numpy. It is prepared with a simple 3x3 kernel, minor changes could make it work with custom sized kernels.

    def blur(a):
        kernel = np.array([[1.0,2.0,1.0], [2.0,4.0,2.0], [1.0,2.0,1.0]])
        kernel = kernel / np.sum(kernel)
        arraylist = []
        for y in range(3):
            temparray = np.copy(a)
            temparray = np.roll(temparray, y - 1, axis=0)
            for x in range(3):
                temparray_X = np.copy(temparray)
                temparray_X = np.roll(temparray_X, x - 1, axis=1)*kernel[y,x]
                arraylist.append(temparray_X)
    
        arraylist = np.array(arraylist)
        arraylist_sum = np.sum(arraylist, axis=0)
        return arraylist_sum
    

提交回复
热议问题