一维卷积filter,Matlab中的一维高斯卷积函数

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.

解决方案

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值