Kỹ thuật lập trình PYTHON Giảng Viên Hướng Dẫn: TS. Lê Trọng Hiếu
30
# if pixel
is not a background pixel
if(img[i,j]!= bg):
if(nh == 4):
ngbrs = neighborhood4(i,j)
else:
ngbrs = neighborhood8(i,j, ncols)
ngbrs = [ele for ele in ngbrs if(img[ele[0], ele[1]]!= bg)]
# has no occupied
passed neighbors
if(len(ngbrs)==0):
img[i, j] = label
find.append(label)
objs.append((i,i, j,j))
label+=1
else:
finds = list(set([findCluster(img[ele[0], ele[1]]) for ele in ngbrs]))
relObjs = [objs[ele-1] for ele in finds if objs[ele-1] is not None]
leastVal = min(finds)
img[i, j] = leastVal
relObjs.append((i,i,j,j))
combObj = combineObjs(relObjs)
for ele in finds:
find[ele-1] = leastVal
for ele in finds:
objs[ele-1] = None
objs[leastVal-1] = combObj
for i in range(nrows):
for j in range(ncols):
if(img[i,j]!=bg):
img[i,j] = findCluster(img[i,j])
#
return img
objs = [i for i in objs if i is not None]
objs = list(filter(lambda x: x[0]!=x[1] and x[2]!=x[3], objs))
return objs
#
import numpy as np
# img = np.array([0,255,255,0,255,255, 255,0,0,0,0,255, 255,255,0,255,255,255,
0,0,255,0,0,255, 255,255,0,0,255,0, 255,255,255,255,0,0])
# img = img.reshape((6,6))
# hk(img)
Kỹ thuật lập trình PYTHON Giảng Viên Hướng Dẫn: TS. Lê Trọng Hiếu
31
Chia sẻ với bạn bè của bạn: