The ugly mathematical truth

Let's start with the mathematical definition of convolution for discrete functions (since in digital image processing all functions are discrete). To form our problem in a signal processing sense, we can define it as passing an input image I, through a Linear Space Invariant (LSI) system, performing convolution with a kernel h (also called a filter), to produce an output image, g. Hence, we get the following block diagram:

The ugly mathematical truth

This process is described mathematically by the following equation:

The ugly mathematical truth

where * is the symbol for convolution and the large Σ denotes a sum. The reason we have two sums is because our process is two-dimensional. Without going into too much detail, we can summarize the process described previously using the following steps, which are also followed in the implementation of conv2:

  1. Rotate the convolution kernel by 180 degrees to abide by the process in the double sum of the equation.
  2. Determine the central pixel of the neighborhood. This is straightforward when the neighborhood has an odd number of rows and columns, but must be based on some rule if either of the dimensions is even.
  3. Apply the rotated kernel to each pixel of the input image. This is a multiplication of each pixel in the rotated kernel by the corresponding pixel on the image neighborhood processed. It can be thought of as the weighted sum of the neighborhood pixels.

The result of conv2 can be either of the following choices:

  • full: Larger than the original image, taking into account all the pixels that can be computed using the convolution kernel, even if their center falls out of the image. This is the default choice for the function.
  • same: Same size as the original image, using zeros to calculate border pixel values.
  • valid: Smaller than the original image, so that it uses only pixels that have full valid neighbors in the computations.

This means that when you want to produce a convolution result with the same size as the original image, you will have to use same as an input, as we did in our previous example.

By now, those of you that are not very much into math may be tempted to stop reading. So, let's stop the mathematical jargon and dive into the practical examples. We know what a convolution does and we have seen an example on the pixels of a very small image, using an averaging convolution kernel. So, what does this process really do to an image?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset