-->

Gaussian Filter implemetation

2019-08-15 05:11发布

问题:

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?

回答1:

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.