'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 1:[1]
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).
Solution 2:[2]
As you can see in this example:
taken from https://theailearner.com/tag/cv2-getgaussiankernel/
We can get the dot product of the gaussian kernel with itself transposed.
First we get the 1d kernel:
k = cv2.getGaussianKernel(kSize, -1)
Then we can restore its original form before normalization just from the fact that the first element is always originally equals 1:
k /= k[0,0]
Now we can get the dot product as mentioned:
print(k @ k.T)
If you want it summed up to 1 (normalized), divide it by its sum:
kernel = k @ k.T
kernel /= kernel.sum()
print(kernel)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cris Luengo |
| Solution 2 | achiya zigi |
