-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHKAlgorithm.py
More file actions
94 lines (82 loc) · 2.19 KB
/
Copy pathHKAlgorithm.py
File metadata and controls
94 lines (82 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 19 13:26:50 2023
@author: filon
"""
import numpy as np
import matplotlib.pyplot as plt
def Matrix(L, p): #Matrix of ones and zeros
r = np.random.rand(L, L)
R = np.zeros((L, L))
for i in range(0, L):
for j in range(0, L):
if r[i, j] <= p:
R[i, j] = 1
else:
R[i, j] = 0
return R
def Find(L, a, b, c, d, Lw): #Find elements in a matrix
x = L[a, b]
y = L[c, d]
find_rc = Find_plug(L, x, Lw)
row = find_rc[0]
col = find_rc[1]
for i in range(len(col)):
a2 = row[i]
b2 = col[i]
L[a2, b2] = y
return L
def Find_plug(a, b, Lw):
size = a.shape
row = np.array([], dtype=np.int64)
col = np.array([], dtype=np.int64)
for i in range(0,Lw):
for j in range(0, Lw):
if a[i, j] == b:
row = np.append(row, i)
col = np.append(col, j)
return [[row], [col]]
def Label(L,p): #Create matrix for clusters
R = Matrix(L, p)
d = 1
label = np.zeros((L, L))
for i in range(0, L):
for j in range(0, L):
if R[i, j]:
a1 = Above_left(i, j, R)
above = a1[0]
left = a1[1]
if left == 0 and above == 0:
label[i ,j] = d
d = d + 1
elif left != 0 and above == 0:
label[i, j] = label[i, j-1]
elif left ==0 and above != 0:
label[i, j] = label[i-1, j]
else:
Lab_plug = Find(label, i, j-1, i-1, j, L)
label = Lab_plug
label[i, j] = label[i-1, j]
return label
def Above_left(i, j, R):
if i > 0 and j > 0:
above = R[i-1, j]
left = R[i, j-1]
elif i > 0 and j == 0:
above = R[i-1, j]
left = 0
elif i == 0 and j > 0:
above = 0
left = R[i, j-1]
else:
above = 0
left = 0
return (above, left)
def main(): #Executable
Lw = 10
p = 0.5
L = Label(Lw, p)
print(L)
plt.imshow(L)
plt.colorbar()
plt.show()