1
$\begingroup$

I was reading about the 2D Gaussian-blur convolution kernel. A Gaussian vector is a vector where the elements follow a Gaussian distribution. In my case, the kernel is symmetric and hence we take a dyadic product (outer product) of the Gaussian vector to obtain the kernel for 2D. A sample Gaussian kernel implementation obtained from here is:

import numpy as np
   
def gkern(l=5, sig=1.):
    """
    creates gaussian kernel with side length `l` and a sigma of `sig`
    """
    ax = np.linspace(-(l - 1) / 2., (l - 1) / 2., l)
    gauss = np.exp(-0.5 * np.square(ax) / np.square(sig))
    kernel = np.outer(gauss, gauss)
    return kernel / np.sum(kernel)

It can be noted that at the end of the kernel computation, we normalize the matrix using the total sum of the kernel elements.

I understand the point that we are using a normalized matrix to enforce some form of conservation(?) This being the case, is the dyadic product of a normalized Gaussian vector same as computing Gaussian matrix and dividing it by the sum as in above? A simple evaluation showed that the normalization factor is (a^2+b^2+c^2) against (a+b+c)^2 for the first and second method respectively. Can anyone help me understand why we are doing total sum of the matrix for this normalization and also difference between the two normalization approaches?

$\endgroup$
3
  • 1
    $\begingroup$ You might want to add the details of what a Gaussian vector is, and how the convolution kernel is computed, here instead of asking readers to go to an external link. Make it easy for people to provide answers to your questions! $\endgroup$ Nov 3, 2023 at 3:13
  • $\begingroup$ Thanks. Added some more details. Hope it is more clear now. $\endgroup$
    – SKPS
    Nov 3, 2023 at 3:35
  • $\begingroup$ I see no randomness here. How would that be related to a Gaussian matrix? Or perhaps we mean different things by the term "Gaussian matrix"? $\endgroup$ Nov 3, 2023 at 4:22

1 Answer 1

1
$\begingroup$

For a blur operation, one should expect that blurring a constant color should retain the exact color values for all pixels. This requirement translates to the sum of the discrete values of the smoothing kernel approximation (which is also truncated at some sufficiently large distance) to sum up to exactly 1.

If we don't use this normalization the blurring operation would also make the picture darker or lighter each pass. This applies to whatever kernel you use for blurring, whether it stems from a Gaussian blur, or just a discrete kernel you made up yourself. For example, I could make this one up, which mostly blurs the image in the horizontal axis, less so vertically, and isn't even symmetric (intentionally awful):

ugly_blur_kernel = np.array([[0.1, 0.2, 0.1],
                             [0.5, 1.0, 0.6],
                             [0.1, 0.3, 0.2]]);
ugly_blur_kernel /= np.sum(ugly_blur_kernel)  # at least it is normalized

As for your follow up question about whether normalizing the 1D case before the outer product; $$ \mathrm{guass} = x_i \;e_i\\ \mathrm{kernel} = x_i x_j \;e_i \otimes e_j \\ $$ and $$ \sum\mathrm{kernel} = \sum_i \sum_j x_i x_j = (\sum_i x_i) (\sum_j x_j) = (\sum \mathrm{guass})^2 $$ Yes, it would be the same. You'd maybe see a tiny difference when considering floating point math and the order of operations here, so it may be desirable to perform the normalization last for that reason.

I'm not sure sure what you mean by your a, b, c examples.

$\endgroup$
1
  • $\begingroup$ For the second part, v = [ai $\endgroup$
    – SKPS
    Nov 5, 2023 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.