1

I am preparing to implement a Gaussian Filter in C#, and in preparation I am reading literature on this. However I have disagreeing sources.

One book (Japanese text: Practical Image Processing Introduction by Uchimura) specifies that the equation to calculate the template is

w(u,v)= (1/2*pi*sigma^2) exp(-(x^2+v^2)/(2*sigma^2)). 

I think that is correct, although the author links size and sigma as SIZE = 3*sigma.

Finally an excellent book (Feature Extraction & Image Processing for Computer Vision by Nixon and Aguado, p.106) gives the correct equation, but when implementing it in code gives a different implementation.

w(u,v)= (1/SUM)* exp(-(x^2+v^2)/(2*sigma^2)) 

where SUM is the sum of all values of the exponentials. Below is the pseudo code they provide - I think it is close to MATLAB.

function template=gaussian_template(winsize,sigma)
%Template for Gaussian averaging
%Usage:[template]=gaussian_template(number, number)
%Parameters: winsize-size of template (odd, integer)
% sigma-variance of Gaussian function
%Author: Mark S. Nixon
%centre is half of window size
centre=floor(winsize/2)+1;
%we'll normalise by the total sum
sum=0;

%so work out the coefficients and the running total
for i=1:winsize
    for j=1:winsize
        template(j,i)=exp(-(((j-centre)*(j-centre))+((i-centre)*(i-centre)))/(2*sigma*sigma))
        sum=sum+template(j,i);
    end
end
%and then normalise
template=template/sum; 

Although the correct equation and the code implementation give similar results to some degree, I am wondering why in the same book the implementation and the equation are different.

My question is, have any of you worked with Gaussian filtering implementations, and is the equation correct to implement? Any idea why the book gives a simpler implementation?

Wolfie
  • 27,562
  • 7
  • 28
  • 55
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

3

Both implementations are normalizing so that the sum of the coefficients is 1.0. The second implementation computes the sum manually to get an exact figure and is probably the best way to go. The other implementation gives an exact sum for a continuous version of the filter. However, discretizing the filter only gives an approximation so the approximated sum is not the same as the continuous integral.

Side note: you might want to keep in mind that the Gaussian is non-linear across a single pixel. Sampling at the pixel centers will give an inaccurate result. It is better to sample at the sub-pixel and then average over the entire pixel.

jaket
  • 9,140
  • 2
  • 25
  • 44
  • I have read that the gaussian is separable, and one implementation (the magazine one) actually compute it like this (one for the rows and one for the columns) instead of the one above. This seems to "compute it efficiently" but I don't see why this is efficient. Any thought on this? – KansaiRobot Aug 18 '17 at 06:28
  • 1
    Say you have a 9x9 filter kernel. Doing a single pass over the image with a 9x9 kernel requires many more operations than a pass of 1x9 plus a pass of 9x1. For this example it is (9^2)/(9+9) = 4.5 times faster to separate the kernel. – jaket Aug 18 '17 at 14:14
  • Thanks. I have one more question about gaussian implementation. Would appreciate it if you take a look – KansaiRobot Aug 21 '17 at 09:02