One convolution function of Gaussian Gauss in Matlab

I am trying to write a function that returns a single gauss dimensional filter. the function took sigma as a parameter. The problem is that the function returns the same array for all sigma.

function gaussFilter=gauss(sigma) width = 3 * sigma; support = (-width :sigma: width); gaussFilter= exp( - (support).^2 / (2*sigma^2)); gaussFilter = gaussFilter/ sum(gaussFilter); 

Please note that the support array is calculated correctly, but the problem arises when applying exp.

+7
matlab gaussian
source share
2 answers

The idea is that the filter should be wide enough to represent the Gauss function. A rule of thumb is to use a filter size of at least 6*sigma .

Since support should be centered around zero, this will give you a range of -3*sigma to +3*sigma (to be more precise, -/+ round(6*sigma - 1)/2 should consider zero in the middle). Consequently:

 function gaussFilter = gauss(sigma) width = round((6*sigma - 1)/2); support = (-width:width); gaussFilter = exp( -(support).^2 ./ (2*sigma^2) ); gaussFilter = gaussFilter/ sum(gaussFilter); 

Example : (all of the following are equivalent)

 sigma = 1.2; width = round((6*sigma - 1)/2); gauss(sigma) normpdf( -width:width, 0, sigma ) fspecial('gaussian', [1 2*width+1], sigma) h = gausswin(2*width+1)'; h = h / sum(h) 
+4
source share

There is nothing wrong with the results. Your support vector is essentially

 [-3*sigma -2*sigma -1*sigma 0 1*sigma 2*sigma 3*sigma] 

And if you collect every element of support and multiply by -1, -support.^2

 [-9*sigma^2 -4*sigma^2 -1*sigma^2 0 -1*sigma^2 -4*sigma^2 -9*sigma^2] 

So dividing by 2*sigma^2 will always lead to the same vector,

 [-9/2 -4/2 -1/2 0 -1/2 -4/2 -9/2] 

or

 -4.5000 -2.0000 -0.5000 0 -0.5000 -2.0000 -4.5000 

So why do you always get the same answer.

So, you need to check your algorithm to create a one-dimensional Gaussian filter.

EDIT:

The source code is fine: except I don’t understand why you made support using -3*sigma:sigma:3*sigma - you have to change it to support = -3:3 .

You can also use:

 gaussFilter = fspecial('gaussian',[1 7],sigma) 

EDIT: Check out the Amro solution for the full code and explanation of why support = -3*sigma:3*sigma and not support = -3*sigma:sigma:3*sigma

+4
source share

All Articles