Winter 2019
Winter 2019

Reputation: 93

What are the values (coefficients) of a Gaussian Kernel?

A Gaussian filter can be applied to an image using the following commands:

  1. cv.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType=BORDER_DEFAULT]]] )

For Example:

import cv2
import numpy as np
a = np.arange(50, step=2).reshape((5,5)).astype('uint8')
print(a)
J1 = cv2.GaussianBlur(a,ksize=(3,3), sigmaX =1, sigmaY=1, 
borderType=cv2.BORDER_CONSTANT)
print(J1)

The Answer is:

[[ 2  4  6  7  6]
[ 8 12 14 16 13]
[15 22 24 26 20]
[22 32 34 36 27]
[20 28 29 31 23]]
  1. from scipy.ndimage import gaussian_filter gaussian_filter(a, sigma=1,mode='constant',cval=0)

For Example, My Code is:

from scipy.ndimage import gaussian_filter
gaussian_filter(a, sigma=1,mode='constant',cval=0)

The answer is:

 array([[ 2,  3,  5,  6,  5],
   [ 6,  9, 12, 14, 11],
   [13, 19, 22, 23, 18],
   [19, 26, 29, 29, 23],
   [17, 23, 26, 26, 19]])
  1. We can create our filter using the equation $G(x,y) = \frac{1}{2\pi \sigma^2}e^{-\frac{x^2+y^2}{2\sigma^2}}$, and then apply this filer on an image using cv2.filter2D command.

For Example, My Code is:

import cv2
import numpy as np
x, y = np.meshgrid(np.linspace(-1,1,3), np.linspace(-1,1,3))
d = x**2 + y**2
sigma, mu = 1.0, 0.0
K = (1/(2*np.pi*sigma**2))*np.exp(- d / ( 2.0 * sigma**2 ) ) 
J2 = cv2.filter2D(a,-1,K, borderType=cv2.BORDER_CONSTANT)
print(J2)

The answer is:

[[ 2  3  4  6  5]
[ 6  9 11 12 10]
[12 17 19 20 15]
[17 25 27 28 21]
[15 22 23 24 18]]

The answers to all these methods are different, so my question is that what kernel values (coefficients) are used for the filter.

Upvotes: 1

Views: 1333

Answers (1)

Alex Alex
Alex Alex

Reputation: 2018

You can find out the filter coefficients like this: Create a zeros matrix (or image), such as 20x20 or more, and set one pixel in the center to 1.0. Make a filter this matrix and print the result. The result will be equal to the coefficients of the filter. See "point spread function (PSF)" in wiki.

Upvotes: 1

Related Questions