import numpy as np
import scipy.misc
import matplotlib.pyplot as pt
Let's load an image file and apply a simple image processing operation: Blurring. In mathematical terms, we will replace each pixel with a (weighted) average of its neighbors and itself.
u = scipy.misc.imread("cat.jpeg").astype(np.float32).sum(axis=2)/(3*256)
pt.imshow(u, cmap="gray")
Now make blurred_u
:
blurred_u = np.zeros_like(u)
blurred_u[1:-1, 1:-1] = (
2*u[1:-1, 1:-1]
+ u[2:, 1:-1]
+ u[:-2, 1:-1]
+ u[1:-1, 2:]
+ u[1:-1, 2:]
)/6
u = blurred_u
pt.imshow(blurred_u, cmap="gray")
As it turns out:
So here we go: Let's see what would go into designing our own image processing language.