-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageSimplify.py
More file actions
243 lines (226 loc) · 11.9 KB
/
Copy pathimageSimplify.py
File metadata and controls
243 lines (226 loc) · 11.9 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
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
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import numpy
from kmeans import Kmeans, KmeansTablesNb
from imageCls import ImageFile
import loggerFct as log
class KmeansBw (Kmeans):
def __init__ (self, colors):
Kmeans.__init__ (self, 15, colors)
def computeScore (self, groupId, itemId):
score = self.groups [groupId][1] - self.values [itemId]
if score <0: score *=-1
return score
def computeMean (self, groupId):
groupLen = len (self.groups [groupId])
groupRange = range (1, groupLen)
score =0
for g in groupRange: score += self.groups [groupId][g]
groupLen -=1
score /= groupLen
return int (score)
class KmeansCol (Kmeans):
def __init__ (self, colors):
Kmeans.__init__ (self, 30, colors)
def computeScore (self, groupId, itemId):
scoreR = self.groups [groupId][1][0] - self.values [itemId][0]
scoreG = self.groups [groupId][1][1] - self.values [itemId][1]
scoreB = self.groups [groupId][1][2] - self.values [itemId][2]
if scoreR <0: scoreR *=-1
if scoreG <0: scoreG *=-1
if scoreB <0: scoreB *=-1
score = scoreR + scoreG + scoreB
return score
def computeMean (self, groupId):
groupLen = len (self.groups [groupId])
groupRange = range (1, groupLen)
scoreR =0
scoreG =0
scoreB =0
for g in groupRange:
scoreR = scoreR + self.groups [groupId][g][0]
scoreG = scoreG + self.groups [groupId][g][1]
scoreB = scoreB + self.groups [groupId][g][2]
groupLen -=1
scoreR /= groupLen
scoreG /= groupLen
scoreB /= groupLen
return (int (scoreR), int (scoreG), int (scoreB))
def findColorIslandBw (self, coords, island, neighbourgs):
if coords in island: return island, neighbourgs
island.add (coords)
neighbourgList =[]
if coords[0] >0:
if self.array[coords[0]][coords[1]] == self.array[coords[0] -1][coords[1]]: neighbourgList.append ((coords[0] -1, coords[1]))
elif self.array[coords[0] -1][coords[1]] in neighbourgs.keys(): neighbourgs [self.array[coords[0] -1][coords[1]]] +=1
else: neighbourgs [self.array[coords[0] -1][coords[1]]] =1
if coords[0] < len (self.array) -1:
if self.array[coords[0]][coords[1]] == self.array[coords[0] +1][coords[1]]: neighbourgList.append ((coords[0] +1, coords[1]))
elif self.array[coords[0] +1][coords[1]] in neighbourgs.keys(): neighbourgs [self.array[coords[0] +1][coords[1]]] +=1
else: neighbourgs [self.array[coords[0] +1][coords[1]]] =1
if coords[1] >0:
if self.array[coords[0]][coords[1]] == self.array[coords[0]][coords[1] -1]: neighbourgList.append ((coords[0], coords[1] -1))
elif self.array[coords[0]][coords[1] -1] in neighbourgs.keys(): neighbourgs [self.array[coords[0]][coords[1] -1]] +=1
else: neighbourgs [self.array[coords[0]][coords[1] -1]] =1
if coords[1] < len (self.array[0]) -1:
if self.array[coords[0]][coords[1]] == self.array[coords[0]][coords[1] +1]: neighbourgList.append ((coords[0], coords[1] +1))
elif self.array[coords[0]][coords[1] +1] in neighbourgs.keys(): neighbourgs [self.array[coords[0]][coords[1] +1]] +=1
else: neighbourgs [self.array[coords[0]][coords[1] +1]] =1
if len (neighbourgList) >3: # si plus de quatre éléments dans l'îlot, il est considéré comme un continent
for neigh in neighbourgList: island.add (neigh)
else:
for neigh in neighbourgList: island, neighbourgs = self.findColorIslandBw (neigh, island, neighbourgs)
return island, neighbourgs
def findColorIslandCol (self, coords, island, neighbourgs):
if coords in island: return island, neighbourgs
island.add (coords)
neighbourgList =[]
if coords[0] >0:
if self.array[coords[0]][coords[1]][0] == self.array[coords[0] -1][coords[1]][0] and self.array[coords[0]][coords[1]][1] == self.array[coords[0] -1][coords[1]][1] and self.array[coords[0]][coords[1]][2] == self.array[coords[0] -1][coords[1]][2]:
neighbourgList.append ((coords[0] -1, coords[1]))
elif (self.array[coords[0] -1][coords[1]][0], self.array[coords[0] -1][coords[1]][1], self.array[coords[0] -1][coords[1]][2]) in neighbourgs.keys():
neighbourgs [( self.array[coords[0] -1][coords[1]][0], self.array[coords[0] -1][coords[1]][1], self.array[coords[0] -1][coords[1]][2] )] +=1
else: neighbourgs [( self.array[coords[0] -1][coords[1]][0], self.array[coords[0] -1][coords[1]][1], self.array[coords[0] -1][coords[1]][2] )] =1
if coords[0] < len (self.array) -1:
if self.array[coords[0]][coords[1]][0] == self.array[coords[0] +1][coords[1]][0] and self.array[coords[0]][coords[1]][1] == self.array[coords[0] +1][coords[1]][1] and self.array[coords[0]][coords[1]][2] == self.array[coords[0] +1][coords[1]][2]:
neighbourgList.append ((coords[0] +1, coords[1]))
elif (self.array[coords[0] +1][coords[1]][0], self.array[coords[0] +1][coords[1]][1], self.array[coords[0] +1][coords[1]][2]) in neighbourgs.keys():
neighbourgs [( self.array[coords[0] +1][coords[1]][0], self.array[coords[0] +1][coords[1]][1], self.array[coords[0] +1][coords[1]][2] )] +=1
else: neighbourgs [( self.array[coords[0] +1][coords[1]][0], self.array[coords[0] +1][coords[1]][1], self.array[coords[0] +1][coords[1]][2] )] =1
if coords[1] >0:
if self.array[coords[0]][coords[1]][0] == self.array[coords[0]][coords[1] -1][0] and self.array[coords[0]][coords[1]][1] == self.array[coords[0]][coords[1] -1][1] and self.array[coords[0]][coords[1]][2] == self.array[coords[0]][coords[1] -1][2]:
neighbourgList.append ((coords[0], coords[1] -1))
elif (self.array[coords[0]][coords[1] -1][0], self.array[coords[0]][coords[1] -1][1], self.array[coords[0]][coords[1] -1][2]) in neighbourgs.keys():
neighbourgs [(self.array[coords[0]][coords[1] -1][0], self.array[coords[0]][coords[1] -1][1], self.array[coords[0]][coords[1] -1][2])] +=1
else: neighbourgs [( self.array[coords[0]][coords[1] -1][0], self.array[coords[0]][coords[1] -1][1], self.array[coords[0]][coords[1] -1][2] )] =1
if coords[1] < len (self.array[0]) -1:
if self.array[coords[0]][coords[1]][0] == self.array[coords[0]][coords[1] +1][0] and self.array[coords[0]][coords[1]][1] == self.array[coords[0]][coords[1] +1][1] and self.array[coords[0]][coords[1]][2] == self.array[coords[0]][coords[1] +1][2]:
neighbourgList.append ((coords[0], coords[1] +1))
elif (self.array[coords[0]][coords[1] +1][0], self.array[coords[0]][coords[1] +1][1], self.array[coords[0]][coords[1] +1][2]) in neighbourgs.keys():
neighbourgs [( self.array[coords[0]][coords[1] +1][0], self.array[coords[0]][coords[1] +1][1], self.array[coords[0]][coords[1] +1][2] )] +=1
else: neighbourgs [( self.array[coords[0]][coords[1] +1][0], self.array[coords[0]][coords[1] +1][1], self.array[coords[0]][coords[1] +1][2] )] =1
if len (neighbourgList) >3: # si plus de quatre éléments dans l'îlot, il est considéré comme un continent
for neigh in neighbourgList: island.add (neigh)
else:
for neigh in neighbourgList: island, neighbourgs = self.findColorIslandCol (neigh, island, neighbourgs)
return island, neighbourgs
def eraseColorIsland (self, island, neighbourgs):
colors = neighbourgs.keys()
color = list (colors)[0]
for col in colors:
if neighbourgs [col] > neighbourgs [color]: color = col
for h,w in island: self.array[h][w] = color
def eraseColorIslands (self, color):
rangeHeight = range (len (self.array))
rangeWidth = range (len (self.array[0]))
seenPoints = set()
if color == 'col':
for h in rangeHeight:
for w in rangeWidth:
island, neighbourgs = self.findColorIslandCol ((h,w), set(), dict())
seenPoints.update (island)
if len (island) <4: self.eraseColorIsland (island, neighbourgs)
def findColorFronters (self):
lenHeight = len (self.array)
lenWidth = len (self.array[0])
rangeHeight = range (lenHeight)
rangeWidth = range (1, lenWidth)
for h in rangeHeight:
v=0
for w in rangeWidth:
if self.array[h][w] == self.array[h][w-1]: continue
diff = int (self.array[h][w]) - int (self.array[h][w-1])
if diff <-30 or diff >30:
diff = int (self.array[h][v]) - int (self.array[h][w-1])
while v< w-1 and diff >=-30 and diff <=30:
self.array[h][v] = self.array[h][w-1]
v+=1
diff = int (self.array[h][v]) - int (self.array[h][w-1])
v=w
diff = int (self.array[h][v]) - int (self.array[h][-1])
while v< lenWidth -1 and diff >=-30 and diff <=30:
self.array[h][v] = self.array[h][-1]
v+=1
diff = int (self.array[h][v]) - int (self.array[h][-1])
rangeHeight = range (1, len (self.array))
rangeWidth = range (len (self.array[0]))
for h in rangeWidth:
v=0
for w in rangeHeight:
if self.array[h][w] == self.array[h-1][w]: continue
diff = int (self.array[h][w]) - int (self.array[h-1][w])
if diff <-5 or diff >5:
diff = int (self.array[v][w]) - int (self.array[h-1][w])
while v< h-1 and diff >=-30 and diff <=30:
self.array[v][w] = self.array[h-1][w]
v+=1
diff = int (self.array[v][w]) - int (self.array[h-1][w])
v=h
diff = int (self.array[v][w]) - int (self.array[-1][w])
while v< lenHeight -1 and diff >=-30 and diff <=30:
self.array[v][w] = self.array[-1][w]
v+=1
diff = int (self.array[v][w]) - int (self.array[-1][w])
def simplifyColorsBw (self):
self.tobw()
self.toArray()
colors = self.getColors()
if len (colors) >8:
colorKmeans = KmeansBw (colors)
colorKmeans.BuildGroup()
for group in colorKmeans.groups:
for color in group:
grey = self.array.T
colorArea = (grey == color)
self.array[colorArea.T] = group[0]
self.eraseColorIslands ('bw')
# self.findColorFronters()
self.fromArray()
def simplifyColors (self):
self.toArray()
colors = self.getColors()
if len (colors) >8:
colorKmeans = KmeansCol (colors)
colorKmeans.BuildGroup()
for group in colorKmeans.groups:
for r,g,b in group:
red, green, blue = self.array.T
colorArea = (red == r) & (green == g) & (blue == b)
self.array[colorArea.T] = (group[0][0], group[0][1], group[0][2])
# self.eraseColorIslands ('col')
# self.findColorFronters()
self.fromArray()
setattr (ImageFile, 'simplifyColors', simplifyColors)
setattr (ImageFile, 'eraseColorIslands', eraseColorIslands)
setattr (ImageFile, 'findColorIslandCol', findColorIslandCol)
setattr (ImageFile, 'eraseColorIsland', eraseColorIsland)
setattr (ImageFile, 'findColorFronters', findColorFronters)
def eraseLonelyPixelsNb (imageArray):
# seuls les quatre pixels touchant directement le pixel central sont pris en compte
# l'image est en noir et blanc
# les coins de l'image
if imageArray[0][0] != imageArray[0][1] and imageArray[0][0] != imageArray[1][0]: imageArray[0][0] = imageArray[1][0]
if imageArray[-1][0] != imageArray[-1][1] and imageArray[-1][0] != imageArray[-2][0]: imageArray[-1][0] = imageArray[-2][0]
if imageArray[0][-1] != imageArray[0][-2] and imageArray[0][-1] != imageArray[1][-1]: imageArray[0][-1] = imageArray[1][-1]
if imageArray[-1][-1] != imageArray[-1][-2] and imageArray[-1][-1] != imageArray[-2][-1]: imageArray[-1][-1] = imageArray[-2][-1]
# les bords de l'image
rangeWidth = range (len (imageArray[0]))
rangeHeight = range (len (imageArray))
for w in rangeWidth:
if imageArray[0][w] != imageArray[1][w]: imageArray[0][w] = imageArray[1][w]
if imageArray[-1][w] != imageArray[-2][w]: imageArray[-1][w] = imageArray[-2][w]
for h in rangeHeight:
if imageArray[h][0] != imageArray[h][1]: imageArray[h][0] = imageArray[h][1]
if imageArray[h][-1] != imageArray[h][-2]: imageArray[h][-1] = imageArray[h][-2]
# le centre de l'image
rangeWidth = range (1, len (imageArray[0]) -1)
rangeHeight = range (1, len (imageArray) -1)
for h in rangeHeight:
for w in rangeWidth:
if imageArray[h][w] == imageArray[h][w+1] or imageArray[h][w] == imageArray[h][w-1]: continue
elif imageArray[h][w] == imageArray[h+1][w] or imageArray[h][w] == imageArray[h-1][w]: continue
elif imageArray[h+1][w] == imageArray[h-1][w]: imageArray[h][w] = imageArray[h-1][w]
elif imageArray[h][w+1] == imageArray[h][w-1]: imageArray[h][w] = imageArray[h][w-1]
elif imageArray[h][w+1] == imageArray[h+1][w] or imageArray[h][w-1] == imageArray[h+1][w]: imageArray[h][w] = imageArray[h+1][w]
elif imageArray[h][w+1] == imageArray[h-1][w] or imageArray[h][w-1] == imageArray[h-1][w]: imageArray[h][w] = imageArray[h-1][w]
return imageArray