Search code examples
pythonopencvimage-processinggaussiangaussianblur

How do I get to show Gaussian Kernel for 2d? (opencv)


I am using this:

 blur = cv2.GaussianBlur(dst,(5,5),0)

And I wanted to show the kernel matrix by this:

print(cv2.getGaussianKernel(ksize=(5,5),sigma=0))

But I am getting a type error:

TypeError: an integer is required (got type tuple)

If I only put 5, I get a 5x1 matrix. Isn't the blur kernel 5x5? Or am I missing on something fundamental?


Solution

  • The Gaussian kernel is separable. Therefore, the kernel generated is 1D. The GaussianBlur function applies this 1D kernel along each image dimension in turn. The separability property means that this process yields exactly the same result as applying a 2D convolution (or 3D in case of a 3D image). But the amount of work is strongly reduced. For your 5x5 kernel, the 2D convolution does 25 multiplications and additions, the separable implementation does only 5+5=10. For larger kernels, the gains are increasingly significant.

    To see the full 2D kernel, apply the GaussianBlur function to an image that is all zeros and has a single pixel in the middle set to 1. This is the discrete equivalent to the Dirac delta function, which we can use to analyze linear time-invariant functions (==convolution filters).