def GaussianKernel_1D(x, p): # x is the size
gk = np.zeros(x)
param = int(p)
const = 1 / (2 * math.pi * (param**2))
if (x % 2 == 0): #if size is even
for y in range(-x/2, x/2):
gk[y] = const * math.exp( -(y**2)/(2 * param**2) )
if (x % 2 == 1): #if size is odd
for y in range(-(x-1)/2, (x-1)/2):
gk[y] = const * math.exp( -(y**2)/(2 * param**2) )
return gk
错误是:
for y in range(-x/2, x/2): TypeError: 'float' object cannot be interpreted as an integer
我的代码有什么问题?预先感谢!
-x/2
替换-int(x/2)
,用x/2
替换int(x/2)
。 Python中的除法返回float
,而range
仅取ints
。