类型错误:"numpy.float32"对象不可调用



我不明白为什么会出现这个错误。有人能帮我吗?我是python的初学者,这个错误一直困扰着我。该函数主要用于连接组件标记,但我发现使用python的max((函数很难获得最大值,如下所示。

我一定错过了一些小错误。任何形式的帮助都将不胜感激。

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import disk
%matplotlib inline
def ccl(img):
##### first pass #####
curr_label = 1;
img = np.array(img)
labels = np.array(img)
# storing label conversions
label_conv = []
label_conv.append([])
label_conv.append([])
count = 0
for i in range(1, len(img)):
for j in range(1, len(img[0])):
if img[i][j] > 0:
label_x = int(labels[i][j - 1])
label_y = int(labels[i - 1][j])
if label_x > 0:
# both x and y have a label
if label_y > 0:
if not label_x == label_y:
labels[i][j] = min(label_x, label_y)
#print("i: ", i, "j: ", j)
#print("label_x: ", type(label_x), "label_y: ", type(label_y))
max_label = max(label_x, label_y)
if max_label not in label_conv[0]:
label_conv[0].append(max(label_x, label_y))
label_conv[1].append(min(label_x, label_y))
elif max(label_x, label_y) in label_conv[0]:
ind = label_conv[0].index(max(label_x, label_y))
if label_conv[1][ind] > min(label_x, label_y):
l = label_conv[1][ind]
label_conv[1][ind] = min(label_x, label_y)
while l in label_conv[0] and count < 100:
count += 1
ind = label_conv[0].index(l)
l = label_conv[1][ind]
label_conv[1][ind] = min(label_x, label_y)
label_conv[0].append(l)
label_conv[1].append(min(label_x, label_y))
else:
labels[i][j] = label_y
# only x has a label
else:
labels[i][j] = label_x
# only y has a label
elif label_y > 0:
labels[i][j] = label_y
# neither x nor y has a label
else:
labels[i][j] = curr_label
curr_label += 1
##### second pass #####
count = 1
for idx, val in enumerate(label_conv[0]):
if label_conv[1][idx] in label_conv[0] and count < 100:
count += 1
ind = label_conv[0].index(label_conv[1][idx])
label_conv[1][idx] = label_conv[1][ind]
for i in range(1, len(labels)):
for j in range(1, len(labels[0])):
if labels[i][j] in label_conv[0]:
ind = label_conv[0].index(labels[i][j])
labels[i][j] = label_conv[1][ind]
return labels

img = cv2.imread("..\disks.png", 0)
img2 = cv2.bitwise_not(img, mask = None)
SE = disk(25)
img_eroded = cv2.erode(img2,SE) #Erode the image
plt.figure(figsize=(16,12))
plt.subplot(1,1,1)
plt.imshow(img_eroded, cmap="gray")
plt.title('Image')
plt.xticks([])
plt.yticks([])
labels = ccl(img_eroded)
print("size: ",  labels)
plt.figure(figsize=(16,12))
plt.subplot(1,1,1)
plt.imshow(img_eroded, cmap="gray")
plt.title('Image')
plt.xticks([])
plt.yticks([])

这是我得到的错误输出。

TypeError                                 Traceback (most recent call last)
<ipython-input-83-04f5b3d4d1e5> in <module>
103 plt.yticks([])
104 
--> 105 labels = ccl(img_eroded)
106 
107 print("size: ",  labels)
<ipython-input-83-04f5b3d4d1e5> in ccl(img)
34                             #print("i: ", i, "j: ", j)
35                             #print("label_x: ", type(label_x), "label_y: ", type(label_y))
---> 36                             max_label = max(label_x, label_y)
37 
38                             if max_label not in label_conv[0]:
TypeError: 'numpy.float32' object is not callable
enter code here

这看起来像是您之前将max指定为指向numpy int的变量。然后,当您尝试调用max()函数时,它会尝试调用您创建的变量。

清除你的命名空间,然后再次运行它。

最新更新