I want to get a Gaussian window of size m rows
and n columns
.
I know how to get to 1-dimension. i.e below.
from scipy.stats import multivariate_normal
multivariate_normal(mean=[1, 5], cov=(2.5))
Now I want two dimensions of a matrix. Purpose: I want fit this filter on top an image. the green color is a matrix of an image. Blue circle is a Gaussian filter. I am not sure how to get the blue window.
I am thinking to apply something like this -
gw = multivariate_normal(mean=[1, 5], cov=(2.5))
for i in range(image.shape[0):
image_gauss_window[i:] = gw
Can you give a way to find out the Gaussian filter for an image? I see opencv many functions apply a Gaussian blur to an image. But here I want the filter before applying/convolving on top of an image.
Using np.fromfunction
:
You can use some code from a basic computer vision library
that I wrote.
So if you have size
and sigma
, you can get the 2d
numpy
array
of the gaussian
kernel
with this one line:
kernel = np.fromfunction(lambda x, y: (1/(2*math.pi*sigma**2)) * math.e ** ((-1*((x-(size-1)/2)**2+(y-(size-1)/2)**2))/(2*sigma**2)), (size, size))
and then to normalise
it, just divide each element
by the sum
:
kernel /= np.sum(kernel)
which (for example) with size
as 5
and sigma
as 1
would give kernel
as:
array([[ 0.00296902, 0.01330621, 0.02193823, 0.01330621, 0.00296902],
[ 0.01330621, 0.0596343 , 0.09832033, 0.0596343 , 0.01330621],
[ 0.02193823, 0.09832033, 0.16210282, 0.09832033, 0.02193823],
[ 0.01330621, 0.0596343 , 0.09832033, 0.0596343 , 0.01330621],
[ 0.00296902, 0.01330621, 0.02193823, 0.01330621, 0.00296902]])
which you can see is a nice symmetric bell-curve
in 2d
that rises in the centre.
You can see this gaussian
filter
visualised with matplotlib
:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With