Maria Montero

2D Convolution in Image Processing

This article provides information on two-dimensional convolution and zero padding with respect to digital imaging.

In my previous article “Better Information on DSP: Learning About Convolution”, I talked about convolution and its two important applications in the field of signal processing. There, the signals were probably considered one-dimensional in the spatial domain. However, the convolution process can also be carried out on multidimensional signals.

In this article, we will try to better understand the process and consequences of two-dimensional convolution, widely used in the field of image processing.

The definition of 2D convolution

Convolution involving one-dimensional signals is known as 1D convolution or simply convolution. Otherwise, if the convolution is between two signals that extend along two mutually perpendicular dimensions (that is, if the signals are two-dimensional in nature), it will be called a 2D convolution. This concept can be expanded to include multidimensional signals, due to which we can have multidimensional convolution.

In the digital domain, convolution is performed by multiplying and accumulating the instantaneous values ​​of the overlapping samples corresponding to two input signals, one of which is inverted. This definition of 1D convolution is applicable even for 2D convolution, except that in the latter case one of the inputs is inverted twice.

This type of operation is widely used in the field of digital image processing where the 2D matrix representing the image will be agreed with a comparatively smaller matrix called the 2D kernel.

An example of 2D convolution

Let’s try to calculate the pixel value of the output image resulting from the convolution of an image matrix of size 5 × 5 X with the core h 3 × 3 size, shown below in Figure 1.

Figure 1: Input matrices, where x represents the original image and h represents the kernel. Image created by Sneha HL

To achieve this, the step-by-step procedure to be followed is described below.

Step 1: Matrix inversion

This step consists of flipping the kernel along, for example, rows followed by a flip along its columns, as shown in Figure 2.

Figure 2: Pictorial representation of the matrix inversion. Image created by Sneha HL

As a result, each (i, j) th element of the original kernel becomes the (j, i) th element in the new array.

Step 2: slide the kernel over the image and perform the MAC operation at every instant

Overlay the inverted kernel over the image, advancing pixel by pixel.

For each case, find the product of the pixels that overlap each other and find their sum. The result will be the value of the output pixel at that particular location. For this example, the non-overlapping pixels will be assumed to have a value of ‘0’. We will discuss this in more detail in the next section on “Zero padding”.

In the present example, we will start sliding the kernel by columns first and then we will move along the rows.

Pixels row by row