Convolution Filters

Ian Ormesher
3 min readMay 9, 2020

--

Convolution Filters (also known as kernels) are used with images for blurring, sharpening, embossing, edge detection, and more. This is accomplished by doing a convolution between a kernel and an image. Kernels are typically 3x3 matrices and the convolution process can be expressed mathematically like this:

Convolution expression

Where g(x,y) is the output filtered image, f(x,y) is the original image and w is the filter kernel.

To explain how this works here’s some example pixels from the top left-hand corner of an image. We’ll apply the convolution kernel to the value in the pixel at location (1,1):

applying a convolution kernel to the pixel (1,1) of an image

The filter is taking values from around the pixel of interest — from locations (x-1, y-1) to (x+1, y+1). It is multiplying those values by the corresponding value in the kernel matrix and then summing up these values to give the new pixel value. The original value was 100 but after applying the convolution kernel it becomes 160.

This process is performed for all the pixels in the image. Well, almost all! The astute amongst you will see that there are problems when we are at the edge pixels of the image. For example, what do we do for pixel (0,0)? There are various strategies to get round this which include extending, wrapping, mirroring or cropping the image (or even the kernel). If you’re interested to know more about them, then please checkout the Wiki page link at the end of the article.

Depending on the filter values, the convolution can have a variety of effects. Some of these filters actually have names too. Here are some examples using the astronaut image of Eileen Collins that is included in scikit-learn:

Convolution kernels and their resultant images

Check out the Wiki page link at the bottom of this article for more examples.

Sobel Filters

Sobel filters are a great way of detecting edges in an image.

The horizontal kernel has the following values and results in the image shown:

Sobel kernel — horizontal

The vertical kernel has the following values and results in the image shown:

Sobel kernel — vertical

Combining these two filters allows us to see all the edges in an image.

Pyto

I have an iPhone and wanted to do some Python development on it without having to pay for an Apple Developer licence. There are a number of options available at the moment including a great app called Pythonista. But I wanted to do some Computer Vision stuff and this app doesn’t support OpenCV. That’s when I came across a brilliant app called Pyto. This has full support for OpenCV and other cool things.

Using Pyto I created a script that passes the live camera feed through a dual Sobel filter. Here’s a screenshot I took when it was running:

Sobel filter applied to the camera on my iPhone — done with Pyto

Having done this I’m excited about other things I can try out thanks to Pyto. It’s a great app and well worth having on your phone. The author has even made it open source - so I’m hoping to upload my sample there.

Code

I’ve create a Jupyter Notebook that shows all the convolutions I’ve talked about here together with my sample script for Pyto. It’s on my GitHub and you can see it here.

Useful Links

--

--

Ian Ormesher

I am a Data Scientist experienced in machine learning and computer vision.