Every time we apply the convolutional operation, the size of the image shrinks.

For an image going convolution, we observe the the corner pixels come under the filter only once but pixels in the central area of the image come under the filter several times.

To solve the issue of shrinking outputs and loss of information of the edges, we use padding.

In padding we add a layer to the boundary of the image, so that when convolution is applied the image does not shrink much and its corner details are also taken into consideration. By convention we pad with the value 0.

p is the padding value which in this case is 1

Now if we apply the convolutional operation to this matrix the resulting matrix is of size 6 x 6, which is its original size and we also we do not lose any information.

In general if we pad a image by p and then apply a filter f then the output matrix will have dimensions (n + 2p - f + 1).

Valid Convolutions

In this type we do not use padding, So p = 0.

Same Convolutions

Pad so that output size is the same as the input size. So in this case we need

Therefore,

Note

In computer vision problem f is usually odd

Another related topic is Strided Convolution