TypeError:“ builtin_function_or_method”对象不可用于python下标

时间:2019-10-06 08:42:41

标签: python arrays numpy opencv typeerror

我正在向Medial Axis Transform写一些代码,但遇到类型错误 用于我的阵列。

import cv2 
import numpy as np

img = cv2.imread('cvSmall.png',0)
print(img.shape)
thresh = 230
maxValue = 255
th, dst = cv2.threshold(img, thresh, maxValue, cv2.THRESH_BINARY_INV);

cv2.imshow('image',dst)

fZero = np.zeros((len(dst),len(dst[0])))

for y in range(0,len(dst)-1):
        for x in range(0,len(dst[0])-1):
            if dst[y][x] == 255:
                fZero[y][x] = 1

print(fZero.shape)

global currentF
global nextF
currentF = np.zeros((len(dst),len(dst[0])))
currentF =  fZero.copy
nextF = np.zeros((len(dst),len(dst[0])))

def iterationFunction():
    for y in range(1,len(dst)-2):
        for x in range(1,len(dst[0])-2):
            nextF[y][x] += fZero[y][x]
            nextF[y][x] +=  min((currentF[y-1][x]), (currentF[y+1][x]), (currentF[y][x-1]),(currentF[y][x+1]))
#Error in this line

    print (nextF)

iterationFunction()

这是我得到的错误:

  

第37行,在迭代函数中       nextF [y] [x] + = min((currentF [y-1] [x]),(currentF [y + 1] [x]),(currentF [y] [x-1]),(currentF [ y] [x + 1]))

     

TypeError:   'builtin_function_or_method'对象不可下标

2 个答案:

答案 0 :(得分:0)

我认为您需要将currentF = fZero.copy更改为currentF = fZero.copy()。您正在将currentF设置为功能copy,而不是调用copy()并将结果设置为currentF

答案 1 :(得分:0)

此错误表示调用函数时,您使用的是[]而不是()

在您的代码中,currentF = fZero.copy应该为currentF = fZero.copy()

不这样做会使currentF成为函数,而不是调用函数的结果。因此,当您尝试执行currentF[x]时会产生错误。