Examples of Convolutions¶
Definition and Calculation¶
The definition for the convolution \(G=F\star W\) of two discrete functions \(F\) and \(W\) is:
\[G[i,j] = \sum_{k=-\infty}^\infty \sum_{l=-\infty}^\infty F[i-k,j-l] W[k,l]\]
The recipe to calculate the convolution is:
- Mirror the function \(W\) in the origin to give function \(W^m[i,j]=W[-i,-j]\),
- then shift the weight function \(W^m\) to position \((k,l)\) in the image,
- pixelwise multiply the function and shifted weight function and
- sum all resulting values, this is the result of the convolution at point \((i,j)\).
Let’s do this for a simple example. Below you see a small image \(F\) and a weight function \(W\). Here we use the convention that when drawing weight functions (also called kernels) we assume it is defined over the infinite two dimensional domain, but we indicate only those values different from zero (note that points \((k,l)\) such that \(W[k,l]=0\) do not add to the convolution result, we simply can ignore those points).
In [1]: from scipy.ndimage import convolve;
In [2]: F = np.random.randint(0,10,(15,15)); print(F)
[[5 3 1 0 9 6 1 1 6 0 0 7 6 6 6]
[6 0 7 4 5 5 1 2 5 9 3 0 6 3 2]
[8 2 3 0 4 3 7 2 0 4 7 7 2 1 6]
[4 8 5 8 3 7 0 5 6 9 2 5 2 1 1]
[3 8 5 2 1 7 0 9 0 7 6 7 2 1 4]
[1 7 2 5 0 9 5 0 1 5 0 1 4 2 8]
[3 5 3 8 3 0 7 4 9 0 5 8 8 3 2]
[9 8 3 7 1 4 2 8 4 0 0 9 6 6 3]
[4 1 5 6 7 7 9 1 8 7 0 4 2 7 9]
[3 3 6 9 8 8 3 8 0 8 2 4 4 5 0]
[9 4 2 0 5 8 3 1 8 0 5 5 8 5 9]
[6 9 7 4 7 4 9 0 2 1 0 1 3 5 3]
[3 1 7 7 4 1 0 6 1 4 0 8 7 0 0]
[8 8 0 5 3 4 4 2 4 8 4 6 4 9 7]
[5 4 2 7 8 8 4 7 9 1 7 3 8 4 3]]
In [3]: W = np.ones((3,3))/9.0; print(W)