forked from trustdigits/Connected-Component-Labeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCC_labeling_8.py
313 lines (222 loc) · 10.6 KB
/
CC_labeling_8.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
##########################################################################################
####
#### Connected-Component labeling Two-Pass algorithm (8-connectivity)
####
##########################################################################################
####
#### Cluster elevation matrix - iso-elevation regions
####
##########################################################################################
import numpy as np
## based on http://en.wikipedia.org/wiki/Connected-component_labeling
## based on
## Connected Component Labeling Algorithms for Gray-Scale Images
## and Evaluation of Performance using Digital Mammograms, R. Dharshana Yapa
## and K. Harada
#######################################################################################################
class disjointSet:
def __init__(self, n):
self.parent = [0]*n
self.rank = [0]*n
def makeSet(self,x):
self.parent[x] = x
self.rank[x] = 0
def union(self,x,y):
xRoot = self.find(x)
yRoot = self.find(y)
if(xRoot == yRoot):
return
if(self.rank[xRoot] < self.rank[yRoot]):
self.parent[xRoot] = yRoot
elif(self.rank[xRoot] > self.rank[yRoot]):
self.parent[yRoot] = xRoot
else:
self.parent[yRoot] = xRoot
self.rank[xRoot] += 1
def find(self, x):
if (self.parent[x] != x):
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
class disjointRegions(disjointSet):
def __init__(self, n):
self.parent = [0]*n
self.rank = [0]*n
self.cell = [0]*n # top left hand corner cell for regions
self.utils = 0 # effective number of new created label
self.neighbors = [[]]*n
def makeSet(self,x,i,j):
self.parent[x] = x
self.rank[x] = 0
self.cell[x] = (i,j)
self.utils += 1
def union(self,x,y):
xRoot,xCell = self.find(x)
yRoot,yCell = self.find(y)
if(xRoot == yRoot):
return
if(self.rank[xRoot] < self.rank[yRoot]):
self.parent[xRoot] = yRoot
elif(self.rank[xRoot] > self.rank[yRoot]):
self.parent[yRoot] = xRoot
else:
self.parent[yRoot] = xRoot
self.rank[xRoot] += 1
def find(self, x):
if (self.parent[x] != x):
self.parent[x] = self.find(self.parent[x])[0]
return self.parent[x], self.cell[self.parent[x]] # return representative for region: label, cell
def topDown(self,x):
'''
Return all labels whose parent is x's parent
'''
tDlabel = []
for i in range(self.utils):
#print ' i ' + str(i) + ' self.find(i)[0] ' + str(self.find(i)[0]) + ' x ' + str(x) + ' ' + str(self.find(x)[0])
if(self.find(i)[0] == self.find(x)[0]):
tDlabel.append(i)
return tDlabel
##########################################################################################################
class CC_lab:
def __init__(self,mat):
self.labels = []
self.forest = 0
self.mat = mat
def connectedComponentLabel(self,n_clusters=0):
'''
Label regions belonging to the same color level
Algorithm uses disjoint-set data structure (equivalence table) and
it is based on
A Run-Based Two-Scan Labeling Algorithm, L. H, Y. Chao, K. Suzuki, IEEE
'''
N, M = len(self.mat), len(self.mat[0])
if(n_clusters==0):
n_clusters = N * M
self.labels = sameDimZeroMatrix(self.mat) # build label matrix with appropriate dimensions
label = 0 # next label to be assigned
self.forest = disjointRegions(n_clusters) # forest to record equivalences
for i in range(N):
for j in range(M):
neighbors = self.connectedNeighbors(i,j) # neighbors with same value
#print 'neighbors ' + str(neighbors) +' for pixel at ' + str(i) + ', '+ str(j)
if (neighbors == [[],[],[],[]]): # no neighbors at all
#print 'case no neighbors at all'
self.labels[i][j] = label # new label
self.forest.makeSet(label,i,j)
label += 1
else:
##find minimum neighbor
lab = [] # labels for neighbor pixels that are available
for pix in range(4):
if(neighbors[pix] != []): # some neighbor in this direction
x,y = neighbors[pix][0][0], neighbors[pix][0][1]
lab.append(self.labels[x][y])
if(len(lab) > 1): # at least two neighbors from north, west, north-west, north-east
#print 'more than 1 neighbors'
self.labels[i][j] = min(lab)
for l in range(len(lab)-1):
self.forest.union(lab[l], lab[l+1]) # union labels, maybe they are different
else:
#print 'only 1 neighbors'
if(neighbors[0] != []):
#print 'adopted west'
self.labels[i][j] = self.labels[neighbors[0][0][0]][neighbors[0][0][1]]
elif(neighbors[1] != []): # some north neighbor only
#print 'adopted north'
self.labels[i][j] = self.labels[neighbors[1][0][0]][neighbors[1][0][1]]
elif(neighbors[2] != []): # some north-west neighbor only
#print 'adopted north-west'
self.labels[i][j] = self.labels[neighbors[2][0][0]][neighbors[2][0][1]]
elif(neighbors[3] != []): # some north-east neighbor only
#print 'adopted north-east'
self.labels[i][j] = self.labels[neighbors[3][0][0]][neighbors[3][0][1]]
if(neighbors[0] == [] and i > 0):
self.updateNeighbors(i,j,i-1,j)
if(neighbors[1] == [] and j > 0):
self.updateNeighbors(i,j,i,j-1)
if(neighbors[2] == [] and i > 0 and j > 0):
self.updateNeighbors(i,j,i-1,j-1)
if(neighbors[3] == [] and i > 0 and j < M-1):
self.updateNeighbors(i,j,i-1,j+1)
# second pass
for i in range(N):
for j in range(M):
self.labels[i][j] = self.forest.find(self.labels[i][j])[0]
def updateNeighbors(self, i, j, k, l):
'''
Region labeled with label(i,j) and region labeled with label(k,l) are neighbor, update
self.forest.neighbors twice
'''
self.forest.neighbors[self.labels[i][j]] = np.unique([x for x in self.forest.neighbors[self.labels[i][j]]] + [self.labels[k][l]])
self.forest.neighbors[self.labels[k][l]] = np.unique([x for x in self.forest.neighbors[self.labels[k][l]]] + [self.labels[i][j]])
def neighborRegions(self,labelij,mat):
'''
Return neighbor regions for the region with label labelij
'''
neighbors = []
R,C = len(mat), len(mat[0])
parentij = self.forest.find(labelij)[0] # representative for the region which the cell i,j belongs to
areas = self.forest.topDown(parentij) # list of areas in the region which the cell i,j belongs to
for area in areas:
neighboor_ = self.forest.neighbors[area]
for k in range(len(neighboor_)):
neighboor_label = self.forest.find(neighboor_[k])[0]
if(neighboor_label != labelij):
neighbors.append(neighboor_label)
return np.unique(neighbors)
def labelToElevation(self, label_i):
'''
From label, retrieve elevation in matrix self.mat
'''
x,y = self.forest.cell[label_i]
return self.mat[x][y]
def connectedNeighbors(self,i, j):
'''
Return coordinates for neighbors of pixel i, j that have same value
as pixel i, j (8-connectivity)
'''
neighbors = []
for z in range(4):
neighbors.append([])
N, M = len(self.mat), len(self.mat[0])
if(i >= N or j >= M or i < 0 or j < 0): # exceed dimensions
return []
val = self.mat[i][j]
if(i == 0 and j == 0): ## top left-hand corner, no neighbors
return neighbors
# i or j is not zero
if(j > 0):
if(val == self.mat[i][j-1]):
neighbors[0].append((i, j-1)) ## west
if(i > 0):
if(val == self.mat[i-1][j]):
neighbors[1].append((i-1, j)) ## north
if(i > 0 and j > 0):
if(val == self.mat[i-1][j-1]):
neighbors[2].append((i-1, j-1)) ## north-west
if(i > 0 and j < M-1):
if(val == self.mat[i-1][j+1]):
neighbors[3].append((i-1, j+1)) ## north-east
return neighbors
def matrixFromLabelsList(self, region_values, N, M):
'''
Input array region_values contains values for each region in self.labels.
Returns the matrix with pixel i,j colored with the value for the region which this pixel belongs to.
'''
toPlot = []
for i in range(N):
tmp = []
for j in range(M):
tmp.append(region_values[self.forest.find(self.labels[i][j])[0]-1]) # watershed label for the region (i,j) belongs to
toPlot.append([x for x in tmp])
return toPlot
def sameDimZeroMatrix(mat):
'''
Create matrix with same dimensions as mat with zeros
'''
zeros = []
for i in range(len(mat)):
tmp = []
for j in range(len(mat[i])):
tmp.append(0)
zeros.append([x for x in tmp])
return zeros