python-2.7 | SimpleCV - TypeError:'float'对象不能解释为索引

时间:2021-10-18 22:51:42

I'm trying to build a Image dewarping tool using simpleCV and python2.7.

我正在尝试使用simpleCV和python2.7构建一个图像去扭曲工具。

The code below worked perfectly fine before(when I was on Ubuntu 16.04.1 LTS) but I recently updated to the Ubuntu 17.04 and I get this error now.

下面的代码完全没问题(当我在Ubuntu 16.04.1 LTS上时),但我最近更新到Ubuntu 17.04,我现在得到这个错误。

Here is the function:

这是功能:

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

and this is the error I get:

这是我得到的错误:

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

what is the problem in my code?

我的代码中有什么问题?

1 个解决方案

#1


1  

the np.zeros function needs Hd and Wd to be integers, not floats. Previous versions of Python would silently cast to integer, but newer versions give an error instead. Try adding this:

np.zeros函数需要Hd和Wd为整数,而不是浮点数。以前版本的Python会默默地转换为整数,但较新的版本会产生错误。尝试添加此:

Hd = int(Hd)
Wd = int(Wd)

#1


1  

the np.zeros function needs Hd and Wd to be integers, not floats. Previous versions of Python would silently cast to integer, but newer versions give an error instead. Try adding this:

np.zeros函数需要Hd和Wd为整数,而不是浮点数。以前版本的Python会默默地转换为整数,但较新的版本会产生错误。尝试添加此:

Hd = int(Hd)
Wd = int(Wd)