e đang lập trình về liên thông 8 sử dụng python, đang bị vướng ở chỗ không thể thay đổi giá trị trong list, cụ thể ở đây là list Labels, ai chỉ giúp e ở đâu được không ạ
import numpy
def ccl4(img_array):
img_array=[[0]*(len(img_array[0])+2)] + img_array + [[0]*(len(img_array[0])+2)]
for i in range(1,len(img_array)-1):
img_array[i]=[0] + img_array[i] + [0]
##### first pass #####
print ("starting first pass")
curr_label = 1;
img_array = numpy.array(img_array)
labels = numpy.array(img_array)
# storing label conversions
label_conv = []
label_conv.append([])
label_conv.append([])
count = 0
for i in range(1, len(img_array)):
for j in range(1, len(img_array[0])):
if img_array[i][j] > 0:
label_x = labels[i][j - 1]
label_y = 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)
if max(label_x, label_y) 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 #####
print ("starting 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]
for i in range(1, len(labels)-1):
for j in range(1, len(labels[0])-1):
xetmin=[]
if labels[i][j] > 0:
if labels[i][j] > 0 and labels[i][j] not in xetmin :
xetmin.append(labels[i][j])
if labels[i][j-1] > 0 and labels[i][j-1] not in xetmin :
xetmin.append(labels[i][j-1])
if labels[i-1][j-1] > 0 and labels[i-1][j-1] not in xetmin :
xetmin.append(labels[i-1][j-1])
if labels[i-1][j] > 0 and labels[i-1][j] not in xetmin :
xetmin.append(labels[i-1][j])
if labels[i-1][j+1] > 0 and labels[i-1][j+1] not in xetmin :
xetmin.append(labels[i-1][j+1])
if labels[i][j+1] > 0 and labels[i][j+1] not in xetmin :
xetmin.append(labels[i][j+1])
if labels[i+1][j+1] > 0 and labels[i+1][j+1] not in xetmin :
xetmin.append(labels[i+1][j+1])
if labels[i+1][j] > 0 and labels[i+1][j] not in xetmin :
xetmin.append(labels[i+1][j])
if labels[i+1][j-1] > 0 and labels[i+1][j-1] not in xetmin :
xetmin.append(labels[i+1][j-1])
labels[i][j] = min(xetmin)
doiso=[]
for i in range(0, len(labels)):
for j in range(0, len(labels[0])):
if labels[i][j] > 0 and labels[i][j] not in doiso :
doiso.append(labels[i][j])
for i in range(len(doiso)):
for x in range(0, len(labels)):
for y in range(0, len(labels[0])):
if labels[x][y] == doiso[i]:
labels[x][y] = i+1
return labels
'''
img = [ [0,0,0,0,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0,1,0],
[0,1,1,0,1,0,1,0,1,0],
[0,1,1,1,1,0,0,0,1,0],
[0,0,0,0,0,0,0,0,1,0],
[0,1,1,1,1,0,1,0,1,0],
[0,0,0,0,1,0,1,0,1,0],
[0,1,1,1,1,0,0,0,1,0],
[0,1,1,1,1,0,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0]]
'''
img=[[1, 1, 1, 0, 1, 1, 0, 1],
[1, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 1],
[0, 1, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 1, 0],
[1, 0, 0, 1, 1, 0, 1, 0],
[1, 0, 1, 1, 1, 0, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 0]]
img = ccl4(img)
print(img)