AL B
AL B

Reputation: 223

Gaussian derivative - Matlab

I have an RGB image and I am trying to calculate its Gaussian derivative. Image is a greyscale image and the Gaussian window is 5x5,st is the standard deviation This is the code i am using in order to find a 2D Gaussian derivative,in Matlab:

  N=2
  [X,Y]=meshgrid(-N:N,-N:N)
  G=exp(-(x.^2+y.^2)/(2*st^2))/(2*pi*st)
  G_x = -x.*G/(st^2);
  G_x_s = G_x/sum(G_x(:));
  G_y = -y.*G/(st^2);
  G_y_s = G_y/sum(G_y(:));

where st is the standard deviation i am using. Before I proceed to the convolution of the Image using G_x_s and G_y_s, i have the following problem. When I use a standard deviation that is an even number(2,4,6,8) the program works and gives results as expected. But when i use an odd number for standard deviation (3 or 5) then the G_y_s value becomes Inf because sum(G_y(:))=0. I do not understand that behavior and I was wondering if there is some problem with the code or if in the above formula the standard deviation can only be an even number. Any help will be greatly appreciated.

Thank you.

Upvotes: 0

Views: 829

Answers (1)

Steffen
Steffen

Reputation: 2431

Your program doesn't work at all. The results you find when using an even number is just because of some numerical errors.

Your G will be a matrix symmetrical to the center. x and y are both point symmetrical to the center. So the multiplication (G times x or y) will result in a matrix with a sum of zero. So a division by that sum is a division by zero. Everything else you observe is because of some roundoff errors. Here, I see a sum og G_xof about 1.3e-17.

I think your error is in the multiplication x.*G and y.*G. I can not figure out why you would do that.

I assume you want to do edge detection rigth? You can use fspecialto create several edge filters. Laplace of gaussian for instance. You could also create two gaussian filters with different standard deviations and subtract them from another to get an edge filter.

Upvotes: 0

Related Questions