我得到了一个float64类型的numpy数组a。如何使用高斯滤波器模糊这些数据?
我试过了
from PIL import Image, ImageFilter
image = Image.fromarray(a)
filtered = image.filter(ImageFilter.GaussianBlur(radius=7)),但这会产生ValueError: 'image has wrong mode'。(它有F模式。)
我可以通过将a与一些常量相乘,然后四舍五入到整数来创建合适模式的图像。这应该是可行的,但我希望有一个更直接的方法。
(我使用的是Pillow 2.7.0。)
发布于 2015-04-28 21:29:52
如果你有一个二维Numpy数组a,你可以直接对它使用高斯滤波器,而不是首先使用Pillow将其转换为图像。scipy有一个函数gaussian_filter可以做同样的事情。
from scipy.ndimage.filters import gaussian_filter
blurred = gaussian_filter(a, sigma=7)发布于 2017-11-15 11:00:39
这是我仅使用numpy的方法。它是用一个简单的3x3内核准备的,很小的改动就可以使它与自定义大小的内核一起工作。
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发布于 2021-01-20 15:34:55
使用卷积和高斯滤波器的可分离性的纯numpy解决方案分为两个单独的滤波器步骤(这使得它相对较快):
kernel = np.array([1.0,2.0,1.0]) # Here you would insert your actual kernel of any size
a = np.apply_along_axis(lambda x: np.convolve(x, kernel, mode='same'), 0, a)
a= np.apply_along_axis(lambda x: np.convolve(x, kernel, mode='same'), 1, a)https://stackoverflow.com/questions/29920114
复制相似问题