How to convolve two 2-dimensional matrices in python with scipy?

Examples of how to convolve two 2-dimensional matrices in python with scipy :

Create a 2D kernel with numpy

Lets first create a simple 2D kernel with numpy

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns; sns.set()

K = np.zeros((10,10))

K[:,:] = 1

K[:,0:5] = -1.

print(K)

plt.imshow(K)

plt.colorbar()

    plt.title("Kernel 01")

plt.savefig("kernel_01.png", bbox_inches='tight', dpi=100)

plt.show()

gives

[[-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]
 [-1. -1. -1. -1. -1.  1.  1.  1.  1.  1.]]

and

How to do a simple 2D convolution between a kernel and an image in python with scipy ?
How to do a simple 2D convolution between a kernel and an image in python with scipy ?

Create a fake image with numpy

Now lets create a very simple 2D matrix (or image) with numpy

img = np.zeros((100,100))

img[0:50,0:40] = 1.
img[50:100,0:60] = 1.

print(img)

plt.imshow(img)

plt.colorbar()

plt.savefig("img_01.png", bbox_inches='tight', dpi=100)

plt.show()

gives

How to do a simple 2D convolution between a kernel and an image in python with scipy ?
How to do a simple 2D convolution between a kernel and an image in python with scipy ?

Convolve two 2-dimensional arrays

To convolve the above image with a kernel. a solution is to use scipy.signal.convolve2d:

from scipy import signal

f1 = signal.convolve2d(img, K, boundary='symm', mode='same')

plt.imshow(f1)

plt.colorbar()

plt.savefig("img_01_kernel_01_convolve2d.png", bbox_inches='tight', dpi=100)

plt.show()

gives

How to do a simple 2D convolution between a kernel and an image in python with scipy ?
How to do a simple 2D convolution between a kernel and an image in python with scipy ?

Note that here the convolution values are positives.

Another example

Warning: during a convolution the kernel is inverted (see discussion here for example scipy convolve2d outputs wrong values).

Another example of kernel:

K = np.zeros((10,10))

K[:,:] = -1
K[:,0:5] = 1.

print(K)

plt.imshow(K)

plt.colorbar()

plt.title("Kernel 02")

plt.savefig("kernel_02.png", bbox_inches='tight', dpi=100)

plt.show()

gives here

How to do a simple 2D convolution between a kernel and an image in python with scipy ?
How to do a simple 2D convolution between a kernel and an image in python with scipy ?

2d convolution:

f1 = signal.convolve2d(img, K, boundary='symm', mode='same')

plt.imshow(f1)

plt.colorbar()

plt.title("2D Convolution")

plt.savefig("img_01_kernel_02_convolve2d.png", bbox_inches='tight', dpi=100)

plt.show()

returns then

How to do a simple 2D convolution between a kernel and an image in python with scipy ?
How to do a simple 2D convolution between a kernel and an image in python with scipy ?

Note that now the values are negatives!

References

Image

of