畳み込みの函数はたくさんあるようだがここではimfilter()
とconv2()
とを試した。imfilter()
は"circular"
を指定すればトーラス形状とみなされたが、引数はdouble
しか受け付けなかった。
kernel=[ 1,1,1; 1,0,1; 1,1,1]; mat_0=[ 1,1,0,0,1,1,0,0; 1,1,1,1,0,0,0,0; 0,0,1,0,0,1,0,1; 0,1,0,1,0,1,0,1]; imfilter(mat_0,kernel,"circular") kernel=uint8(kernel); mat_0=uint8(mat_0); tmp_0=[mat_0(end,:);mat_0;mat_0(1,:)]; %最下行を最上行に、最上行を最下行に、 tmp_1=[tmp_0(:,end),tmp_0,tmp_0(:,1)]; %最右列を最左列に、最左列を最右列に持ってくる。 conv2(tmp_1,kernel,"valid")
実行結果:
↓ scipy.signal.convolve2d
で検算:
import numpy as np from scipy.signal import convolve2d # この配列glider0の全セルのムーア近傍の和を求めてみる。 glider0 = np.array([ [1,1,0,0,1,1,0,0], [1,1,1,1,0,0,0,0], [0,0,1,0,0,1,0,1], [0,1,0,1,0,1,0,1]], dtype=np.uint8) # 畳み込みの重みづけを定義しておく。 weight = np.array([[1,1,1], [1,0,1], [1,1,1]], dtype=np.uint8) # これが配列glider0の全セルのムーア近傍の和である。boundary="wrap"オプションを指定すると配列の上下左右がつながっていることになる。 moore = convolve2d(glider0, weight, boundary="wrap", mode="same") print(glider0) print(moore)