-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
217 lines (168 loc) · 5.73 KB
/
__init__.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
import numpy
from UnionFind import UnionFind
class SRM:
def __init__(self, image, Q=32.0):
self._height = image.shape[0]
self._width = image.shape[1]
if image.ndim == 3:
self._depth = image.shape[2]
else:
self._depth = 1
self._n = self._width * self._height
n = self._n
self._image = image.reshape(n, -1)
self._logdelta = 2.0 * numpy.log(6.0 * n)
self._smallregion = int(0.001 * n)
self._q = Q
def run(self):
self.initialization()
self.segmentation()
return self.finalize()
def initialization(self):
print "init"
self._uf = UnionFind()
uf = self._uf
n = self._n
height = self._height
width = self._width
depth = self._depth
img = self._image
self._data = numpy.empty([n, depth + 2])
self._sizes = numpy.ones(n)
for i in xrange(height):
for j in xrange(width):
idx = i * width + j
uf[idx]
self._data[idx, 0:depth] = img[idx]
self._data[idx, depth ] = i
self._data[idx, depth ] = j
def segmentation(self):
pairs = self.pairs()
print "segmentation"
for (r1, r2) in pairs:
r1 = self._uf[r1]
r2 = self._uf[r2]
if r1 != r2 and self.predicate(r1, r2):
self.merge(r1, r2)
self.merge_small_regions()
def pairs(self):
print "pairs"
pairs = []
height = self._height
width = self._width
# using a C4-connectivity
for i in xrange(height - 1):
for j in xrange(width - 1):
idx = i * width + j
# left
pairs.append( ( idx, i * width + j + 1) )
# below
pairs.append( ( idx, (i + 1) * width + j) )
pairs = self.sort(pairs)
return pairs
def sort(self, pairs):
print "sort"
img = self._image
def diff(p):
(r1, r2) = p
diff = numpy.max(numpy.abs(img[r1] - img[r2]))
return diff
return sorted(pairs, key=diff)
def predicate(self, r1, r2):
g = 256.0
logdelta = self._logdelta
w = self._sizes
out = self._data
d2 = (out[r1] - out[r2])**2
log_r1 = min(g, w[r1]) * numpy.log(1.0 + w[r1])
log_r2 = min(g, w[r2]) * numpy.log(1.0 + w[r2])
q = self._q
dev1 = g**2 / (2.0 * q * w[r1]) * (log_r1 + logdelta)
dev2 = g**2 / (2.0 * q * w[r2]) * (log_r2 + logdelta)
dev = dev1 + dev2
return (d2 < dev).all()
def merge(self, r1, r2):
r = self._uf.union(r1, r2)
s1 = self._sizes[r1]
s2 = self._sizes[r2]
n = s1 + s2
self._data[r] = (s1 * self._data[r1] \
+ s2 * self._data[r2]) / n
self._sizes[r] = n
def merge_small_regions(self):
print "small"
height = self._height
width = self._width
smallregion = self._smallregion
for i in xrange(self._height):
for j in xrange(1, self._width):
idx = i * width + j
r1 = self._uf[idx]
r2 = self._uf[idx - 1]
if r1 != r2 and ( self._sizes[r1] < smallregion or self._sizes[r2] < smallregion):
self.merge(r1, r2)
def finalize(self):
print "finalize"
height = self._height
width = self._width
depth = self._depth
uf = self._uf
data = self._data[:, 0:depth]
out = numpy.empty([self._n, depth])
for i in xrange(height):
for j in xrange(1, width):
idx = i * width + j
r1 = uf[idx]
out[idx] = data[r1]
self._finalized = out.reshape(height, width, -1)
return self._finalized
def map(self):
print "map"
height = self._height
width = self._width
depth = self._depth
classes = {}
uf = self._uf
data = self._data[:, 0:depth]
out = numpy.empty(self._n)
for i in xrange(height):
for j in xrange(1, width):
idx = i * width + j
r1 = uf[idx]
if r1 in classes:
classes[r1] += 1
else:
classes[r1] = 1
out[idx] = r1
return classes, out.reshape(height, width)
def exploded(self):
print "exploded"
out0 = numpy.empty([n, depth])
out = self._data
expl = numpy.zeros([2 * self._height, 2 * self._width, self._depth])
for i in xrange(self._height):
for j in xrange(1, self._width):
r1 = self._uf[r]
x = int(self._more[r1, 0])
y = int(self._more[r1, 1])
expl[i + x, j + y] = out[r]
return expl
if __name__ == "__main__":
import sys
from scipy.misc import imread
from matplotlib import pyplot
q = int(sys.argv[1])
im = imread(sys.argv[2])
srm = SRM(im, q)
# srm.initialization()
# srm.segmentation()
# classes, map = srm.map()
# print classes
# pyplot.imshow(map)
# pyplot.show()
segmented = srm.run()
pyplot.imshow(segmented/256)
pyplot.show()
# exploded = srm.exploded()
# pyplot.imshow(exploded/256)
# pyplot.show()