scipy.ndimage.convolveを使用して 2D フィールドAのラプラシアンを計算しようとしています。
stencil = numpy.array([[0, 1, 0],[1, -4, 1], [0, 1, 0]])
scipy.ndimage.convolve(A, stencil, mode='wrap')
しかし、これは私に正しい答えを与えていないようです。私が間違っているアイデアはありますか、またはnumpyでラプラシアンを計算するより良い方法はありますか?
私は別の考えを得ました: ラプラシアンを近似するために、ステンシルをステップ **2 で分割する必要があることを考慮しましたか?ここで、ステップはグリッドのステップ サイズです。そうして初めて、ndimage.convolve の結果を分析結果と比較できます。
実際、Gaussian を使用すると、ndimage.convolve が非常にうまく機能することを示す結果が得られます。
from scipy import ndimage
stencil = numpy.array([[0, 1, 0],[1, -4, 1], [0, 1, 0]])
x = linspace(-10, 10, 100)
y = linspace(-10, 10, 100)
xx, yy = meshgrid(x, y)
image = exp(-xx**2-yy**2) # Standard deviation in x or y: 1/sqrt(2)
laplaced = ndimage.convolve(image, stencil)/(x[1]-x[0])**2 # stencil from original post
expected_result = -4*image + 8*(xx**2+yy**2)*image # Very close to laplaced, in most points!
のような別のラプラス畳み込みカーネルを試しました[[1,1,1][1,-8,1][1,1,1]]
か?
あなたの例を試してみましたが、私のマシンの最新の SciPy で動作します。
image-ndimage.convolve(…)
畳み込みによって画像がどのように変化するかを確認するために、プロットすることをお勧めします。
例:
image = vstack(arange(-10, 10)**2 for _ in range(20))
ndimage.convolve(image, stencil, mode='wrap')
収量
array([[-38, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,...)
これは非常に正しいです (x**2 の 2 次微分は 2 です)—左の境界を除いて。