问题描述
我正在尝试编写一个返回一个二维高斯滤波器的函数.该函数将sigma作为参数.问题在于该函数为所有sigma返回相同的数组.
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);
请注意,支持数组的计算正确,但是应用exp时会出现问题.
Note that support array is calculated correctly but the problem arise when applying the exp.
推荐答案
想法是,滤波器的宽度必须足以表示高斯函数.经验法则是使用至少6*sigma
的过滤器大小.
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
.
由于支撑需要以零为中心,所以您可以在-3*sigma
到+3*sigma
的范围内(更准确地说,在中间将零表示为-/+ round(6*sigma - 1)/2
).因此:
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)
这篇关于Matlab中的一维高斯卷积函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!