Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

one Dimensional gauss convolution function in Matlab

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

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

Note that support array is calculated correctly but the problem arise when applying the exp.

like image 590
Hani Avatar asked Dec 02 '09 14:12

Hani


People also ask

How do you define a Gaussian function in Matlab?

You can create and evaluate a fismf object that implements the gaussmf membership function. mf = fismf("gaussmf",P); Y = evalmf(mf,X); Here, X , P , and Y correspond to the x , params , and y arguments of gaussmf , respectively.

What is Gaussian convolution?

Brief Description. The Gaussian smoothing operator is a 2-D convolution operator that is used to `blur' images and remove detail and noise. In this sense it is similar to the mean filter, but it uses a different kernel that represents the shape of a Gaussian (`bell-shaped') hump.

What is a 1d Gaussian filter?

The one-dimensional Gaussian filter has an impulse response given by. and the frequency response is given by the Fourier transform. with the ordinary frequency. These equations can also be expressed with the standard deviation as parameter.

What is the conv2 in Matlab?

The conv2 function allows you to control the size of the output. Create a 3-by-3 random matrix A and a 4-by-4 random matrix B . Compute the full convolution of A and B , which is a 6-by-6 matrix. A = rand(3); B = rand(4); Cfull = conv2(A,B)


2 Answers

The idea is that the filter needs to be wide enough to represent the Gaussian function. The rule of thumb is to use filter size of at least 6*sigma.

Since the support needs to be centered around zero, that would give you the range of -3*sigma to +3*sigma (to be more accurate, it is -/+ round(6*sigma - 1)/2 to account for the zero in the middle). Hence:

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 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)
like image 181
Amro Avatar answered Nov 15 '22 09:11

Amro


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 square each 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 it by 2*sigma^2 will always result in 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 that's why you always get the same answer.

So you need to check your algorithm for making a one-dimensional gaussian filter.

EDIT:

Your original code is fine: except I don't understand why you've made support with -3*sigma:sigma:3*sigma - you should change it to support = -3:3.

You can also use:

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

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

like image 25
Jacob Avatar answered Nov 15 '22 09:11

Jacob