高斯滤波器实现
Gaussian Filter implemetation
我正准备在 C# 中实现高斯滤波器,为此我正在阅读相关文献。但是我有不同的消息来源。
一本书(日文:实用图像处理入门 by Uchimura)规定计算模板的方程是
w(u,v)= (1/2*pi*sigma^2) exp(-(x^2+v^2)/(2*sigma^2)).
我认为这是正确的,尽管作者将 size 和 sigma 链接为 SIZE = 3*sigma
。
终于,一本优秀的书(尼克松和阿瓜多的计算机视觉特征提取和图像处理,第 106 页)给出了正确的方程,但是在代码中实现它时给出了不同的实现。
w(u,v)= (1/SUM)* exp(-(x^2+v^2)/(2*sigma^2))
其中 SUM
是所有指数值的总和。下面是他们提供的伪代码——我认为它接近 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;
虽然正确的方程和代码实现在某种程度上给出了类似的结果,但我想知道为什么在同一本书中实现和方程不同。
我的问题是,你们中有人使用过高斯滤波实现吗?方程式是否可以正确实现?知道为什么这本书给出了一个更简单的实现吗?
两种实现方式都在标准化,因此系数之和为 1.0。第二种实现手动计算总和以获得精确数字,这可能是最好的方法。另一个实现给出了过滤器的 continuous 版本的精确总和。然而,离散化滤波器仅给出近似值,因此近似和与连续积分不同。
旁注:您可能需要记住,高斯分布在单个像素上是非线性的。在像素中心采样会得到不准确的结果。最好在子像素处采样,然后在整个像素上取平均值。
我正准备在 C# 中实现高斯滤波器,为此我正在阅读相关文献。但是我有不同的消息来源。
一本书(日文:实用图像处理入门 by Uchimura)规定计算模板的方程是
w(u,v)= (1/2*pi*sigma^2) exp(-(x^2+v^2)/(2*sigma^2)).
我认为这是正确的,尽管作者将 size 和 sigma 链接为 SIZE = 3*sigma
。
终于,一本优秀的书(尼克松和阿瓜多的计算机视觉特征提取和图像处理,第 106 页)给出了正确的方程,但是在代码中实现它时给出了不同的实现。
w(u,v)= (1/SUM)* exp(-(x^2+v^2)/(2*sigma^2))
其中 SUM
是所有指数值的总和。下面是他们提供的伪代码——我认为它接近 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;
虽然正确的方程和代码实现在某种程度上给出了类似的结果,但我想知道为什么在同一本书中实现和方程不同。
我的问题是,你们中有人使用过高斯滤波实现吗?方程式是否可以正确实现?知道为什么这本书给出了一个更简单的实现吗?
两种实现方式都在标准化,因此系数之和为 1.0。第二种实现手动计算总和以获得精确数字,这可能是最好的方法。另一个实现给出了过滤器的 continuous 版本的精确总和。然而,离散化滤波器仅给出近似值,因此近似和与连续积分不同。
旁注:您可能需要记住,高斯分布在单个像素上是非线性的。在像素中心采样会得到不准确的结果。最好在子像素处采样,然后在整个像素上取平均值。