Python:'float'对象不能解释为整数? [关闭]

问题描述 投票:-2回答:1
我正在编写将滤镜应用于图像的代码,而我的代码这一部分将返回错误。参数x是一个整数,p是一个浮点数:

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

我的代码有什么问题?预先感谢!
python numpy
1个回答
1
投票
-x/2替换-int(x/2),用x/2替换int(x/2)。 Python中的除法返回float,而range仅取ints
© www.soinside.com 2019 - 2024. All rights reserved.