Ad

TypeError: 'builtin_function_or_method' Object Is Not Subscriptable For Python

- 1 answer

I'm writing some code to Medial Axis Transform and got some type error for my array.

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

And this is the error I'm getting:

line 37, in iterationFunction 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' object is not subscriptable

Ad

Answer

This error means you're using [] instead of () when calling a function.

In your code currentF = fZero.copy should be currentF = fZero.copy()

Not doing so makes currentF a function rather than the result of calling a function. So that produces an error when you try to do currentF[x].

Ad
source: stackoverflow.com
Ad