TrưỜng đẠi họC ĐIỆn lực khoa đIỆn tử viễn thông tiểu luận môn họC: KỸ thuật lập trìNH TROng đtvt



tải về 0.86 Mb.
Chế độ xem pdf
trang18/20
Chuyển đổi dữ liệu22.06.2022
Kích0.86 Mb.
#52442
1   ...   12   13   14   15   16   17   18   19   20
Nguyễn-Việt-Dũng-18810540050-D13DTKTMT
DeCuongOnThi NhapMonTinHoc K1 2021 D15 Đã sửa
5.3.2. Thuật toán Hoshen Kopelman: là một thuật toán đơn giản và hiệu quả để ghi 
nhãn các cụm trên lưới, trong đó lưới là một mạng lưới các ô thông thường, với các 
ô bị chiếm hoặc không sử dụng. 
- Phân đoạn các thành phần được kết nối của hình ảnh:  
- Thuật toán quét qua từng pixel của hình ảnh và gắn nhãn nó với giá trị nhỏ 
nhất trong số các neighbors đã được chuyển và sau đó gọi là một hợp nhất. 
- Thuật toán Hoshen Kopelman bao gồm quét raster của lưới đề cập: 
Code: 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
# 4 Neighborhood,
# 8 Neighborhood 
def printMat(imag):
 
def findCluster(x): 
import copy 
import cv2 
def hk(img, bg = 255, nh = 8): 
# 4 Neighborhood 
def neighborhood4(i,j): 
lst = [] 
if(i==0): 
if(j==0): 
return lst 
else: 
lst.append((i, j-1)) 
return lst 
elif(j==0): 
lst.append((i-1, j)) 
return lst 
else: 
lst.append((i-1,j)) 
lst.append((i, j-1)) 
return lst 
# 8 Neighborhood 
def neighborhood8(i,j, ncols): 
lst = [] 
if (i == 0): 
if (j == 0): 
return lst 
else: 


Kỹ thuật lập trình PYTHON Giảng Viên Hướng Dẫn: TS. Lê Trọng Hiếu 
29 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
lst.append((i, j - 1)) 
return lst 
elif (j == 0): 
lst.append((i - 1, j)) 
lst.append((i-1, j+1)) 
return lst 
elif(j == ncols-1): 
lst.append((i - 1, j)) 
lst.append((i, j - 1)) 
lst.append((i-1, j-1)) 
return lst 
else: 
lst.append((i, j-1)) 
lst.append((i-1, j-1)) 
lst.append((i-1, j)) 
lst.append((i-1, j+1)) 
return lst 
def combineObjs(objList): 
rmin = min([ele[0] for ele in objList]) 
rmax = max([ele[1] for ele in objList]) 
cmin = min([ele[2] for ele in objList]) 
cmax = max([ele[3] for ele in objList]) 
return (rmin,rmax,cmin,cmax) 
def printMat(imag): 
for row in imag: 
for col in row: 
print(col, end= " ", file=text_file) 
print(file=text_file) 
print(file=text_file) 
print(file=text_file) 
label = 1 
objs = [] 
find = [] 
def findCluster(x): 
while(find[x-1]!=x): 
x = find[x-1] 
return x 
img = copy.deepcopy(img) 
nrows,ncols = img.shape 
for i in range(nrows): 
for j in range(ncols): 


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 

tải về 0.86 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   12   13   14   15   16   17   18   19   20




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương