python实现高斯滤波,如何在python中获取高斯滤波器

I am using python to create a gaussian filter of size 5x5.

I saw this post here where they talk about a similar thing but I didn't find the exact way to get equivalent python code to matlab function fspecial('gaussian', f_wid, sigma)

Is there any other way to do it?

I tried using the following code :

size = 2

sizey = None

size = int(size)

if not sizey:

sizey = size

else:

sizey = int(sizey)

x, y = scipy.mgrid[-size: size + 1, -sizey: sizey + 1]

g = scipy.exp(- (x ** 2/float(size) + y ** 2 / float(sizey)))

print g / np.sqrt(2 * np.pi)

The output obtained is

[[ 0.00730688 0.03274718 0.05399097 0.03274718 0.00730688]

[ 0.03274718 0.14676266 0.24197072 0.14676266 0.03274718]

[ 0.05399097 0.24197072 0.39894228 0.24197072 0.05399097]

[ 0.03274718 0.14676266 0.24197072 0.14676266 0.03274718]

[ 0.00730688 0.03274718 0.05399097 0.03274718 0.00730688]]

What I want is something like this:

0.0029690 0.0133062 0.0219382 0.0133062 0.0029690

0.0133062 0.0596343 0.0983203 0.0596343 0.0133062

0.0219382 0.0983203 0.1621028 0.0983203 0.0219382

0.0133062 0.0596343 0.0983203 0.0596343 0.0133062

0.0029690 0.0133062 0.0219382 0.0133062 0.0029690

解决方案

In general terms if you really care about getting the the exact same result as MATLAB, the easiest way to achieve this is often by looking directly at the source of the MATLAB function.

In this case, edit fspecial:

...

case 'gaussian' % Gaussian filter

siz = (p2-1)/2;

std = p3;

[x,y] = meshgrid(-siz(2):siz(2),-siz(1):siz(1));

arg = -(x.*x + y.*y)/(2*std*std);

h = exp(arg);

h(h

sumh = sum(h(:));

if sumh ~= 0,

h = h/sumh;

end;

...

Pretty simple, eh? It's <10mins work to port this to Python:

import numpy as np

def matlab_style_gauss2D(shape=(3,3),sigma=0.5):

"""

2D gaussian mask - should give the same result as MATLAB's

fspecial('gaussian',[shape],[sigma])

"""

m,n = [(ss-1.)/2. for ss in shape]

y,x = np.ogrid[-m:m+1,-n:n+1]

h = np.exp( -(x*x + y*y) / (2.*sigma*sigma) )

h[ h < np.finfo(h.dtype).eps*h.max() ] = 0

sumh = h.sum()

if sumh != 0:

h /= sumh

return h

This gives me the same answer as fspecial to within rounding error:

>> fspecial('gaussian',5,1)

0.002969 0.013306 0.021938 0.013306 0.002969

0.013306 0.059634 0.09832 0.059634 0.013306

0.021938 0.09832 0.1621 0.09832 0.021938

0.013306 0.059634 0.09832 0.059634 0.013306

0.002969 0.013306 0.021938 0.013306 0.002969

: matlab_style_gauss2D((5,5),1)

array([[ 0.002969, 0.013306, 0.021938, 0.013306, 0.002969],

[ 0.013306, 0.059634, 0.09832 , 0.059634, 0.013306],

[ 0.021938, 0.09832 , 0.162103, 0.09832 , 0.021938],

[ 0.013306, 0.059634, 0.09832 , 0.059634, 0.013306],

[ 0.002969, 0.013306, 0.021938, 0.013306, 0.002969]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值