forked from Hi-king/SLIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLIC.py
executable file
·299 lines (243 loc) · 11 KB
/
SLIC.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
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
================================
SLIC superpixel
--------------------------------
input
|- $image/videofile
|- $stepsize
|- $M
output
=================================
2012/09/07
'''
import cv2
import sys
import scipy
import scipy.linalg
import random
import math
import os.path
def argmin(_list):
return _list.index(min(_list))
def L2norm(vec):
return sum([item*item for item in vec])
def L2norm_2d(vec):
return vec[0]*vec[0]+vec[1]*vec[1]
def L2norm_3d(vec):
return vec[0]*vec[0]+vec[1]*vec[1]+vec[2]*vec[2]
def norm2d(vec):
return math.sqrt(vec[0]*vec[0]+vec[1]*vec[1])
def norm3d(vec):
return math.sqrt(vec[0]*vec[0]+vec[1]*vec[1]+vec[2]*vec[2])
def gradient_img(colorsrc):
'''
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html
'''
SCALE = 1
DELTA = 0
DDEPTH = cv2.CV_16S ## to avoid overflow
graysrc = cv2.cvtColor(colorsrc, cv2.cv.CV_BGR2GRAY)
graysrc = cv2.GaussianBlur(graysrc, (3,3), 0)
## gradient X ##
gradx = cv2.Sobel(graysrc, DDEPTH, 1, 0, ksize=3, scale=SCALE, delta=DELTA)
gradx = cv2.convertScaleAbs(gradx)
## gradient Y ##
grady = cv2.Sobel(graysrc, DDEPTH, 0, 1, ksize=3, scale=SCALE, delta=DELTA)
grady = cv2.convertScaleAbs(grady)
grad = cv2.addWeighted(gradx, 0.5, grady, 0.5, 0)
return grad
class SlicCalculator(object):
## Superparameter
M = 20 ##weight of color in distance
INITCENTER_SEARCHDIFF = 1 ## minimum gradientを探す範囲
ERROR_THRESHOLD = 50
## Config
INIFINITY_DISTANCE = 1<<23
DEBUGFLAG = False
def __init__(self, img, step=50, outfilename="SLICsuperpixel.img", stepsize=None, M=None, outparamfilename=None):
if not M is None:
self.M = M
self.MM = M*M
if stepsize is None:
stepsize = (int(step), int(step))
if outparamfilename is None:
base,ext=os.path.splitext(outfilename)
outparamfilename = base+"_params.dat"
self.stepsize = stepsize
self.filename = outfilename
self.outparamfilename = outparamfilename
self.img = img
if self.DEBUGFLAG:
cv2.imshow("test", self.img)
cv2.waitKey(10)
self.labimg = cv2.cvtColor(self.img, cv2.cv.CV_BGR2Lab)
self.xylab = scipy.concatenate(([[[x, y] for y in xrange(self.labimg.shape[1])] for x in xrange(self.labimg.shape[0])], self.labimg), 2)
def _initialize_center_grid(self, cluster_size):
xs = range( cluster_size[0]/2, self.img.shape[0], cluster_size[0] )
ys = range( cluster_size[1]/2, self.img.shape[1], cluster_size[1] )
return [ scipy.array([x,y]) for x in xs for y in ys ]
def _getneighborhood(self, point2d, distanceset):
'''
Neighbor coordinates from points2d
'''
return scipy.array([ [px, py]
for px in range( max(int(point2d[0])-distanceset[0], 0), min(int(point2d[0])+distanceset[0]+1, self.img.shape[0]) )
for py in range( max(int(point2d[1])-distanceset[1], 0), min(int(point2d[1])+distanceset[1]+1, self.img.shape[1]) )
])
def _getneighborhood_in_image(self, point2d, distanceset):
'''
distanceset: [distancex, distancey]
'''
points = self._getneighborhood(point2d, distanceset)
labs = self.labimg[points[0][0]:points[-1][0]+1, points[0][1]:points[-1][1]+1]
return scipy.concatenate((points,labs.reshape((len(points),3))),1)
def _search_minimum_gradient(self, point2d, distance):
searchpoints = self._getneighborhood_in_image(point2d, [distance, distance] )
searchvals = [ self.grad[point[0], point[1]] for point in searchpoints ]
return searchpoints[ argmin(searchvals) ][:2]
def _initialize_center_avoidedge(self, centers, distance):
self.grad = gradient_img(self.img)
return [ self._search_minimum_gradient(point, distance) for point in centers ]
def _initialize_center(self, cluster_size):
centers = self._initialize_center_grid(cluster_size)
centers = self._initialize_center_avoidedge(centers, self.INITCENTER_SEARCHDIFF )
centers = [scipy.concatenate((center,self.labimg[center[0]][center[1]])) for center in centers]
return centers
def _initassignments(self):
width, height = self.img.shape[:2]
self.assignedindex = scipy.array([ [ 0 for i in xrange(height) ] for j in xrange(width) ])
self.assigneddistance = scipy.array([ [ self.INIFINITY_DISTANCE for i in xrange(height) ] for j in xrange(width) ])
def calcdistance(self, point, center, spatialmax):
'''
Great Problem:
Which distance should we use?
'''
## -- new: L2norm optimized -- ##
p1,p2,p3,p4,p5 = point
c1,c2,c3,c4,c5 = center
spatialdist = (c1-p1)*(c1-p1) + (c2-p2)*(c2-p2)
colordist = (c3-p3)*(c3-p3) + (c4-p4)*(c4-p4) + (c5-p5)*(c5-p5)
return colordist + spatialdist * self.MM /(spatialmax*spatialmax)
## -- old: euclid distance -- ##
spatialdist = norm2d(center[:2]-point)
spatialmax = max(stepsize)
colordist = norm3d(center[2:] - self.labimg[point[0], point[1]])
return colordist + spatialdist/spatialmax * self.M
def calcdistance_mat(self, points, center, spatialmax):
## -- L2norm optimized -- ##
center = scipy.array(center)
difs = points-center
norm = difs**2
norm[:, :, 0:2]*=(float(self.MM) /(spatialmax*spatialmax))
norm = scipy.sum(norm, 2)
return norm
def assignment(self, centers, stepsize):
stepmax = max(stepsize)
for assignment_index, center in enumerate(centers):
points = self._getneighborhood(center[:2], stepsize)
searchpoints = self.xylab[points[0][0]:points[-1][0]+1, points[0][1]:points[-1][1]+1]
searchassignedindex= self.assignedindex[points[0][0]:points[-1][0]+1, points[0][1]:points[-1][1]+1]
searchassigneddistance = self.assigneddistance[points[0][0]:points[-1][0]+1, points[0][1]:points[-1][1]+1]
distancemat = self.calcdistance_mat(searchpoints, center, stepmax)
searchassignedindex[searchassigneddistance>distancemat] = assignment_index
searchassigneddistance[searchassigneddistance>distancemat] = distancemat[searchassigneddistance>distancemat]
def update(self, centers):
sums = [scipy.zeros(5) for i in range(len(centers))]
nums = [0 for i in range(len(centers))]
width, height = self.img.shape[:2]
print "E step"
return [ scipy.mean(self.xylab[self.assignedindex==i], 0) for i in xrange(len(centers))]
def calcerror(self, centers, prevcenters):
'''
L2 norm of location
'''
print "error:",sum([ scipy.dot(now[:2]-prev[:2],now[:2]-prev[:2]) for now,prev in zip(centers, prevcenters)])
return sum([ scipy.dot(now[:2]-prev[:2],now[:2]-prev[:2]) for now,prev in zip(centers, prevcenters)])
def iteration(self, centers, stepsize):
error = sum([ scipy.dot(center[:2],center[:2]) for center in centers])
while error > self.ERROR_THRESHOLD:
self.assignment( centers, stepsize ) ## M-step
prevcenters, centers = centers, self.update(centers) ## E-step
error = self.calcerror(centers, prevcenters)
print "L2 error:",error
if self.DEBUGFLAG:
base, ext = os.path.splitext(self.filename)
self.filename = base.split("_error")[0]+"_error"+str(error)+ext
self.resultimg(centers)
return (centers, self.assignedindex)
def resultimg(self, centers):
print "show result"
result = scipy.zeros( self.img.shape, scipy.uint8)
width, height = result.shape[:2]
colors = [ scipy.array([int(random.uniform(0, 255)) for i in xrange(3)]) for j in xrange(len(centers))]
for x in xrange(width):
for y in xrange(height):
result[x,y]=colors[self.assignedindex[x][y]]
if self.DEBUGFLAG:
cv2.imshow("result", result)
cv2.waitKey(10)
cv2.imwrite(self.filename, result)
def saveparams(self, centers, filename=None):
if filename is None: filename=self.outparamfilename
import cPickle
cPickle.dump((centers, self.assignedindex), open(filename, "w+"))
def calc(self):
centers = self._initialize_center(self.stepsize)
self._initassignments()
self.iteration(centers, self.stepsize)
self.resultimg(centers)
self.saveparams(centers)
class SlicVideoCalculator(SlicCalculator):
def __init__(self, videofilename, *args, **kwargs):
self.video = cv2.VideoCapture(videofilename)
dummyimg = scipy.zeros((int(self.video.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)), int(self.video.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), 3), scipy.uint8)
super(SlicVideoCalculator, self).__init__(dummyimg, *args, **kwargs)
self.outfilebase=os.path.splitext(videofilename)[0]
def reinit(self, frame, outfilename):
self.img = frame
self.filename = outfilename
self.outparamfilename=os.path.splitext(self.filename)[0]+"_params.dat"
if self.DEBUGFLAG:
cv2.imshow("test", self.img)
cv2.waitKey(10)
self.labimg = cv2.cvtColor(self.img, cv2.cv.CV_BGR2Lab)
print self.xylab[:, :, 2:].shape
print self.labimg.shape
self.xylab[:, :, 2:]=self.labimg
def calc(self):
## init ##
i=0
centers = self._initialize_center(self.stepsize)
while True:
## load ##
frame = self.video.read()[1]
if frame is None: break
## init ##
self.reinit(frame, self.outfilebase+"_slic%d.png" % i)
#centers = self._initialize_center(self.stepsize)
self._initassignments()
## use previous centers as init ##
centers,assignedindex=self.iteration(centers, self.stepsize)
## output ##
self.resultimg(centers)
self.saveparams(centers)
i+=1
if __name__ == '__main__':
##======##
## init ##
##======##
NUMARGS = len( __doc__.split("input")[1].split("output\n")[0].split("\n") ) - 2
if(len(sys.argv)<=NUMARGS):
print("error: need more argument")
print(__doc__)
exit(1)
base,ext = os.path.splitext(sys.argv[1])
if ext in ('.MP4', '.m4v', '.m4f'):
calculator = SlicVideoCalculator(sys.argv[1], step=sys.argv[2], M=int(sys.argv[3]))
else:
calculator = SlicCalculator(cv2.imread(sys.argv[1]), step=sys.argv[2], M=int(sys.argv[3]))
#calculator.DEBUGFLAG = True
calculator.calc()