Tôi hơi bối rối với câu hỏi bạn đã hỏi và nhận xét bạn đã đăng. Dường như với tôi rằng bạn muốn sử dụng scipy.ndimage.filters.gaussian_filter
nhưng tôi không hiểu ý bạn là gì:
[...] gaussian functions với các giá trị sigma khác nhau cho mỗi pixel. [...]
Thực tế, vì bạn sử dụng mảng 2 chiều x
, bộ lọc gaussian sẽ có 2 tham số. Quy tắc là: một giá trị sigma cho mỗi thứ nguyên thay vì một giá trị sigma trên mỗi pixel.
Dưới đây là một ví dụ ngắn:
import matplotlib.pyplot as pl
import numpy as np
import scipy as sp
import scipy.ndimage
n = 200 # widht/height of the array
m = 1000 # number of points
sigma_y = 3.0
sigma_x = 2.0
# Create input array
x = np.zeros((n, n))
i = np.random.choice(range(0, n * n), size=m)
x[i/n, i % n] = 1.0
# Plot input array
pl.imshow(x, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.savefig("array.png")
# Apply gaussian filter
sigma = [sigma_y, sigma_x]
y = sp.ndimage.filters.gaussian_filter(x, sigma, mode='constant')
# Display filtered array
pl.imshow(y, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.title("$\sigma_x = " + str(sigma_x) + "\quad \sigma_y = " + str(sigma_y) + "$")
pl.savefig("smooth_array_" + str(sigma_x) + "_" + str(sigma_y) + ".png")
Dưới đây là mảng ban đầu:
Dưới đây là một số kết quả cho các giá trị khác nhau của sigma_x
và sigma_y
:
này cho phép để giải thích đúng cho sự ảnh hưởng của tham số thứ hai của scipy.ndimage.filters.gaussian_filter
.
Tuy nhiên, theo báo trước, bạn có thể quan tâm hơn đến việc xác định các trọng số khác nhau cho mỗi pixel. Trong trường hợp này, scipy.ndimage.filters.convolve là chức năng bạn đang tìm kiếm. Dưới đây là ví dụ tương ứng:
import matplotlib.pyplot as pl
import numpy as np
import scipy as sp
import scipy.ndimage
# Arbitrary weights
weights = np.array([[0, 0, 1, 0, 0],
[0, 2, 4, 2, 0],
[1, 4, 8, 4, 1],
[0, 2, 4, 2, 0],
[0, 0, 1, 0, 0]],
dtype=np.float)
weights = weights/np.sum(weights[:])
y = sp.ndimage.filters.convolve(x, weights, mode='constant')
# Display filtered array
pl.imshow(y, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.savefig("smooth_array.png")
Và kết quả tương ứng:
Tôi hy vọng điều này sẽ giúp bạn.
bạn đã xem [bộ lọc gaussian] (http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.gaussian_filter.html) chưa? – hakanc
Thuật ngữ chung mà bạn đang tìm kiếm là [Convolution] (https://en.wikipedia.org/wiki/Convolution). Numpy có thể làm điều đó, xem ví dụ http://stackoverflow.com/questions/20036663/understanding-numpys-convolve –
Bạn có muốn mảng kết quả của mình là 5x5 không? – wflynny