【问题标题】:TypeError: 'builtin_function_or_method' object is not subscriptable for pythonTypeError:“builtin_function_or_method”对象不可为python下标
【发布时间】:2020-02-03 22:14:35
【问题描述】:

我正在为中轴变换编写一些代码并得到一些类型错误 对于我的数组。

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]))

类型错误: 'builtin_function_or_method' 对象不可下标

【问题讨论】:

    标签: python arrays numpy opencv typeerror


    【解决方案1】:

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

    在你的代码中currentF = fZero.copy 应该是currentF = fZero.copy()

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-09
        • 2020-11-05
        • 2022-01-18
        • 2020-03-26
        • 2018-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多