Image processing#

Convolution#

Convolution is a common method used in image processing to apply blurring, sharpening, embossing and edge detection to an image. Given an input image we can apply convolution using a kernel (or filter) on the input to produce an output image that has the kernel applied.

The convolution of an image array \(X\) and a kernel array \(Y\) is

(36)#\[\begin{align*} Y(i,j) = K * X(i,j) = \sum_{p = -a}^{a} \sum_{q = -b}^{b} K(i-p,j-q) X(p,q) \end{align*}\]

where \(Y\) is the filtered image, \(X\) is the original image, \(K\) is a kernel filter. \(K\) is usually a square matrix with odd numbers of rows and columns. To calculate the filtered image we align the kernel filter with elements of \(X\), multiply the corresponding elements together and sum the result. For example consider the convolution of the image using the kernel shown below.

../_images/convolution.svg

Here the kernel is aligned to the first 3 rows and columns of the image. Calculating equation (36)

\[\begin{split} \begin{align*} Y(1,1) &= 0(4)-1(5)+0(2)-1(5)+5(4)-1(3)+0(2)-1(1)+0(2) \\ &= 6. \end{align*} \end{split}\]

The kernel is then aligned to another pixel in the image and the calculations are repeated. Since we cannot align the centre of the kernel filter with the pixels on the edge (or near the edge with a larger kernel) the filtered image is slightly smaller than the original image. Since images that convolution is applied to ofter contain hundreds or thousands of pixels in the horizontal and vertical directions this loss of pixels is not usually a problem.

Gaussian blur#

By applying different kernel filters to images can we can process images. A common application is to blur an image which can be done by applying the following kernel filter which is an approximation of a two-dimensional Gaussian curve

\[\begin{split}K_{blur} = \frac{1}{16} \begin{pmatrix} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \end{pmatrix}\end{split}\]

The affects of applying Gaussian blur to an image can be seen in Fig. 107.

../_images/gaussian_blur.png

Fig. 107 The original image (left) and with Gaussian blur applied (right).#

Unsharp masking#

A common tool used in image manipulation software is the sharpen tool. Given a photograph that is slightly out of focus the edges around features in the photograph will appear blurred and blend into each other, sharpening is used to accentuate these edges giving the appearance of a sharper image (this is often the default ‘enhance’ filter). The unsharp masking kernel filter is

\[\begin{split}K_{sharpen} = \begin{pmatrix} 0 & -1 & 0 \\ -1 & 5 & -1 \\ 0 & -1 & 0 \end{pmatrix},\end{split}\]

and the affect of sharpening an image can be seen in Fig. 108.

../_images/unsharp_masking.png

Fig. 108 The original image (left) and with unsharp masking applied (right).#