IndexError:仅整数,切片(`:`),省略号(`...`),numpy.newaxis(`None`)和整数或布尔数组均有效

时间:2019-05-17 14:09:09

标签: python numpy vector metrics

我正在实现这个卷积函数。为什么我得到以下与索引有关的错误。

IndexError:只有整数,切片(:),省略号(...),numpy.newaxis(None)和整数或布尔数组都是有效索引

```
Gx = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])
Gy = np.array([[-1, -2, -1],[0, 0, 0],[1, 2, 1]])

def convolution(X, F):
    X_height = X.shape[0]
    X_width = X.shape[1]
    F_height = F.shape[0]
    F_width = F.shape[1]

    H = (F_height - 1) / 2
    W = (F_width - 1) / 2
    out = np.zeros((X_height, X_width))
    for i in np.arange(H, X_height-H):
        for j in np.arange(W, X_width-W):
            sum = 0
          for k in np.arange(-H, H+1):
                for l in np.arange(-W, W+1):
                    a = X[i+k, j+l]
                    w = F[H+k, W+l]
                    sum += (w * a)
            out[i,j] = sum
     return out

img_x = convolution(imgray, Gx) / 8
img_y = convolution(imgray, Gy) / 8
IndexError                                Traceback (most recent call last)
<ipython-input-110-b7121e4faef4> in <module>
----> 1 img_x = convolution(imgray, Gx) / 8
   2 img_y = convolution(imgray, Gy) / 8

<ipython-input-109-d5d5936a796a> in convolution(X, F)
  23                 for l in np.arange(-W, W+1):
  24                     #get the corresponding value from image and filter
---> 25                     a = X[i+k, j+l]
  26                     w = F[H+k, W+l]
  27                     sum += (w * a)


0 个答案:

没有答案