我正在尝试使用simpleCV构建一个图像去扭曲工具 和python2.7 。
以下代码完全正常(之前我在Ubuntu 16.04.1 LTS上) 但我最近更新到Ubuntu 17.04,现在我收到了这个错误。
以下是功能:
def buildMap(Ws, Hs, Wd, Hd, R1, R2, Cx, Cy):
map_x = np.zeros((Hd, Wd),np.float32)
map_y = np.zeros((Hd, Wd),np.float32)
rMap = np.linspace(R1, R1 + (R2 - R1), Hd)
thetaMap = np.linspace(0, 0 + float(Wd) * 2.0 * np.pi, Wd)
sinMap = np.sin(thetaMap)
cosMap = np.cos(thetaMap)
for y in xrange(0, int(Hd-1)):
map_x[y] = Cx + rMap[y] * sinMap
map_y[y] = Cy + rMap[y] * cosMap
return map_x, map_y
这是我得到的错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "./fy360.py", line 189, in new_dewarp
xmap, ymap = buildMap(Ws, Hs, Wd, Hd, R1, R2, Cx, Cy)
File "./fy360.py", line 122, in buildMap
map_x = np.zeros((Hd, Wd),np.float32)
TypeError: 'float' object cannot be interpreted as an index
我的代码中有什么问题?
答案 0 :(得分:1)
np.zeros
函数需要Hd
和Wd
为整数,而不是浮点数。以前版本的Python会默默地转换为整数,但较新的版本会出错。尝试添加:
Hd = int(Hd)
Wd = int(Wd)