Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Definition

Many image processing results come from a modification of one pixel with respect to its neighbors. This is the case, for example, for blurring.

When this modification is similar in the entire image gg, it can be mathematically defined using a second image hh which defines the neighbor relationships. This results in a third image ff. This is the so-called convolution [Jähne 2005, section 4] and it is denoted with ∗*:

f(y,x)=(g∗h)(y,x)=∑m∑ng(y−m,x−n) h(m,n)f(y,x) = (g*h)(y,x) = \sum_m \sum_n g(y-m,x-n) \ h(m,n)

Intuitively, the convolution “spreads” each pixel (m,n)(m,n) in gg following hh and proportionally to the intensity g(m,n)g(m,n). Figure 1 gives an example of the computing of a particular pixel.

Example for computing the pixel (1,1) of f.
Note that the pixel (0,0) in the PSF is the one at the center.

Figure 1:Example for computing the pixel (1,1) of ff. Note that the pixel (0,0) in the PSF is the one at the center.

For the sake of simplicity, the image hh is often:

The image hh is called by different names, depending on the context: filter, mask, kernel, window, pattern or point spread function (PSF).

Some convolution examples are shown above.

Example 1
Example 2
Example 3
Example 4
Example 5
The image to the left is the convolution of the other two images.

Figure 2:The image to the left is the convolution of the other two images.

Since hh has only three non-zero pixels, then the convolution formula has only three non-zero terms, and reads:

f(x,y)=g(x,y) h(0,0)+g(x,y−1) h(0,1)+g(x,y−2) h(0,2)=g(x,y)×1+g(x,y−1)×2+g(x,y−2)×3.\begin{align*} f(x,y) &= g(x,y) \ h(0,0) + g(x,y-1) \ h(0,1) + g(x,y-2) \ h(0,2) \\ &= g(x,y) \times 1 + g(x,y-1) \times 2 + g(x,y-2) \times 3. \end{align*}

Then:

f(1,1)=g(1,1)×1+g(1,0)×2+g(1,1)×3=g(1,1),f(1,2)=g(1,2)×1+g(1,1)×2+g(1,2)×3=g(1,1)×2,f(1,3)=g(1,3)×1+g(1,2)×2+g(1,3)×3=g(1,1)×3.\begin{align*} f(1,1) &= g(1,1) \times 1 + g(1,0) \times 2 + g(1,1) \times 3 = g(1,1), \\ f(1,2) &= g(1,2) \times 1 + g(1,1) \times 2 + g(1,2) \times 3 = g(1,1) \times 2, \\ f(1,3) &= g(1,3) \times 1 + g(1,2) \times 2 + g(1,3) \times 3 = g(1,1) \times 3. \end{align*}

Properties

As a mathematical operation, the convolution has several properties.

Boundaries effects

The convolution formula is not defined on the boundaries of the image: as an example, computing f1,1f_{1,1} in Figure 1 requires the value of g0,0g_{0,0} which is not defined.

Therefore, one has to make assumptions about the pixel values outside the image. Figure 7 shows an image with some possibilities to consider the external pixels, and Figure 8 shows the convolution of these images by a Gaussian.

Several ways to assume the pixels outside the image.
The image is delimited by the green edge.

Figure 7:Several ways to assume the pixels outside the image. The image is delimited by the green edge.

Results of the convolution with the same image.

Figure 8:Results of the convolution with the same image.

One can see on the Figure 8 that the three convolutions are basically identical: only the pixels near the boundaries may be different (darker or brighter on this example). Anyway, there is no perfect choice to set the pixels outside the image, and each choice yields some errors. Also, the best thing is to ensure when acquiring the image that the objects of interest are far from the edges

At last, note that the wrapping hypothesis yields a circular convolution. This is also the result given by a multiplication in the Fourier domain (see Fourier transform).

Separable Convolution

A separable convolution is when the convolution kernel hh can be written as the convolution of two 1D filters (say h1h_1 and h2h_2) defined along the two axes. Let’s give an example.

If the PSF hh can reads

h=[αaαbαcβaβbβcγaγbγc]h = \begin{bmatrix} \alpha a & \alpha b & \alpha c \\ \beta a & \beta b & \beta c \\ \gamma a & \gamma b & \gamma c \\ \end{bmatrix}

then

[αaαbαcβaβbβcγaγbγc]⏟h=[0α00β00γ0]⏟h1∗[000abc000]⏟h2=[αβγ]⏟h1∗[abc]⏟h2\underbrace{\begin{bmatrix} \alpha a & \alpha b & \alpha c \\ \beta a & \beta b & \beta c \\ \gamma a & \gamma b & \gamma c \\ \end{bmatrix}}_{h} = \underbrace{\begin{bmatrix} 0 & \alpha & 0 \\ 0 & \beta & 0 \\ 0 & \gamma & 0 \\ \end{bmatrix}}_{h_1} * \underbrace{\begin{bmatrix} 0 & 0 & 0 \\ a & b & c \\ 0 & 0 & 0 \\ \end{bmatrix}}_{h_2} = \underbrace{\begin{bmatrix} \alpha \\ \beta \\ \gamma \\ \end{bmatrix}}_{h_1} * \underbrace{\begin{bmatrix} a & b & c \\ \end{bmatrix}}_{h_2}

Thus, the convolution of an image gg by a separable filter hh can be calculated by first computing the convolution of gg with h1h_1, then the convolution of the former result with h2h_2 (or the reverse):

g∗h=g∗(h1∗h2)=(g∗h1)∗h2=(g∗h2)∗h1g * h = g * (h_1*h_2) = (g*h_1) * h_2 = (g*h_2) * h_1

The convolution separability saves computation time because the computation of two 1D convolutions requires less operations than the computation of a 2D convolution.

Proof

Consider two images gg and hh of size M×NM \times N.

  • On the one side, the computation of one pixel by using the 2D convolution needs MNMN multiplications and MN−1MN-1 additions. Therefore, computing the convoluted image needs (MN+MN−1)×MN(MN+MN-1) \times MN operations.

  • On the other side, each element of a 1D convolution along one column needs MM multiplications and M−1M-1 additions. Similarly, each element of a 1D convolution along one row needs NN multiplications and N−1N-1 additions. Therefore, computing the convoluted image needs 2(M+N−1)×MN2(M+N-1) \times MN operations.

It is easy to see that 2(M+N−1)×MN<(MN+MN−1)×MN2(M+N-1) \times MN < (MN+MN-1) \times MN operations, highlighting the efficiency of the separability.