Reputation: 11
I'm not able to figure out why I'm getting this error. Can anyone help me out? I'm a beginner in python and this error is bugging me. The function is basically for connected component labelling but I'm finding it difficult to get the maximum value using python's max() function as shown below.
There must be some trivial mistake I am missing out. Any kind of help will be appreciated.
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([])
Here is the error output that I'm getting.
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
Upvotes: 0
Views: 1530
Reputation: 24341
This looks like you have previously assigned max
as a variable pointing to a numpy int. Then when you try to call the max()
function it tries to call the variable you have created instead.
Clear your namespace, and run it again.
Upvotes: 3