Matlab中的一维高斯卷积函数

发布于 2024-08-13 01:23:31 字数 330 浏览 7 评论 0原文

我正在尝试编写一个返回一维高斯滤波器的函数。该函数将 sigma 作为参数。问题是该函数为所有西格玛返回相同的数组。

  function gaussFilter=gauss(sigma)  
  width = 3 * sigma;  
  support = (-width :sigma: width);  
  gaussFilter= exp( - (support).^2 / (2*sigma^2));   
  gaussFilter = gaussFilter/ sum(gaussFilter);  

请注意,支持数组计算正确,但应用 exp 时会出现问题。

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);  

Note that support array is calculated correctly but the problem arise when applying the exp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有木有妳兜一样 2024-08-20 01:23:31

这个想法是滤波器需要足够宽才能表示高斯函数。经验法则是使用大小至少为 6*sigma 的过滤器。

由于支持度需要以零为中心,这将为您提供 -3*sigma+3*sigma 的范围(更准确地说,它是 >-/+ round(6*sigma - 1)/2 以解决中间的零)。因此:

function gaussFilter = gauss(sigma)
    width = round((6*sigma - 1)/2);
    support = (-width:width);
    gaussFilter = exp( -(support).^2 ./ (2*sigma^2) );
    gaussFilter = gaussFilter/ sum(gaussFilter);

示例:(以下所有内容都是等效的)

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)

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.

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)
辞取 2024-08-20 01:23:31

结果没有什么问题。你的 support 向量本质上是,

[-3*sigma -2*sigma -1*sigma 0 1*sigma 2*sigma 3*sigma]

如果你对每个支持元素求平方并乘以 -1,-support.^2

[-9*sigma^2 -4*sigma^2 -1*sigma^2 0 -1*sigma^2 -4*sigma^2 -9*sigma^2]

所以将其除以 2*sigma^ 2 总是会产生相同的向量,

[-9/2 -4/2 -1/2 0 -1/2 -4/2 -9/2]

或者

-4.5000   -2.0000   -0.5000         0   -0.5000   -2.0000   -4.5000

所以这就是为什么你总是得到相同的答案。

所以你需要检查你的算法来制作一个 -维高斯滤波器。

编辑:

您的原始代码很好:除了我不明白为什么您使用-3*sigma进行支持: sigma:3*sigma - 您应该将其更改为 support = -3:3

您还可以使用:

gaussFilter = fspecial('gaussian',[1 7],sigma)

编辑:查看Amro 的解决方案,获取完整代码并解释为什么 support = -3*sigma:3*sigma 而不是 support = -3*sigma:sigma:3*sigma< /代码>

There is nothing wrong with the results. Your support vector is essentially,

[-3*sigma -2*sigma -1*sigma 0 1*sigma 2*sigma 3*sigma]

And if you square each element of support and multiply by -1, -support.^2

[-9*sigma^2 -4*sigma^2 -1*sigma^2 0 -1*sigma^2 -4*sigma^2 -9*sigma^2]

So dividing it by 2*sigma^2 will always result in the same vector,

[-9/2 -4/2 -1/2 0 -1/2 -4/2 -9/2]

Or

-4.5000   -2.0000   -0.5000         0   -0.5000   -2.0000   -4.5000

So that's why you always get the same answer.

So you need to check your algorithm for making a one-dimensional gaussian filter.

EDIT:

Your original code is fine: except I don't understand why you've made support with -3*sigma:sigma:3*sigma - you should change it to support = -3:3.

You can also use:

gaussFilter = fspecial('gaussian',[1 7],sigma)

EDIT: Check out Amro's solution for the full code and explanation why support = -3*sigma:3*sigma and not support = -3*sigma:sigma:3*sigma

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文