-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageSimplify.py
More file actions
165 lines (153 loc) · 7.14 KB
/
Copy pathimageSimplify.py
File metadata and controls
165 lines (153 loc) · 7.14 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
#!/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][0] - 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 score
def findColorIsland (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.findColorIsland (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):
rangeHeight = range (len (self.array))
rangeWidth = range (len (self.array[0]))
seenPoints = set()
for h in rangeHeight:
for w in rangeWidth:
island, neighbourgs = self.findColorIsland ((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 simplifyColors (self):
self.tobw()
self.toArray()
colors = self.getColors()
if len (colors) >12:
colorKmeans = KmeansBw (colors)
colorKmeans.BuildGroup()
groupRange = range (len (colorKmeans.groups))
for g in groupRange: colorKmeans.groups[g][0] = int (colorKmeans.groups[g][0])
for group in colorKmeans.groups:
for color in group:
grey = self.array.T
colorArea = (grey == color)
self.array[colorArea.T] = group[0]
self.eraseColorIslands()
self.findColorFronters()
self.fromArray()
setattr (ImageFile, 'simplifyColors', simplifyColors)
setattr (ImageFile, 'eraseColorIslands', eraseColorIslands)
setattr (ImageFile, 'findColorIsland', findColorIsland)
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