@@ -36,10 +36,8 @@ def findSeam(self, direction):
36
36
# - row-indexed seam
37
37
# horizontal seam = sequence of rows; seam[0] is row of col 0
38
38
# - col-indexed seam
39
- if direction == "horizontal" :
40
- self ._exchDims ()
41
39
seam = [- 1 ] * self ._height
42
- self ._buildGraph (transposed )
40
+ self ._buildGraph (direction )
43
41
row = self ._height - 1
44
42
v = self ._edgeTo [self ._sink ]
45
43
while (v != self ._source ):
@@ -48,8 +46,6 @@ def findSeam(self, direction):
48
46
row -= 1
49
47
#self._edgeTo = []
50
48
#self._distTo = []
51
- if direction == "horizontal" :
52
- self ._exchDims ()
53
49
return seam
54
50
55
51
def _shiftImgUp (self , (col , row )):
@@ -216,12 +212,6 @@ def _energyGrad(self, index, width):
216
212
def _diff_squared (self , x , y ):
217
213
return (x - y )** 2
218
214
219
- def _exchDims (self ):
220
- """exchange self._width and self._height"""
221
- swap = self ._width
222
- self ._width = self ._height
223
- self ._height = swap
224
-
225
215
def _toLinear (self , col , row ):
226
216
"""converts pixel from (col, row) to single index"""
227
217
if self ._isValid (col , row ):
@@ -246,7 +236,7 @@ def _isValid(self, col, row=None):
246
236
else :
247
237
return True
248
238
249
- def _buildGraph (self , transposed ):
239
+ def _buildGraph (self , direction ):
250
240
"""pixels are nodes; edges define precedence constraints in a seam"""
251
241
# graph data structures
252
242
self ._edgeTo = [_SENTINEL for _ in range (self ._num_pixels + 2 )] # add 2 for source, sink pixels
@@ -262,52 +252,58 @@ def _buildGraph(self, transposed):
262
252
# for each vertex (pixel), calculate edgeTo[], distTo[]
263
253
# start at row 1
264
254
for v in range (self ._width , self ._num_pixels ):
265
- if (v % self ._width == 0 ):
266
- # pixel is on left edge
267
- self ._edgeTodistTo (v , transposed , edgeL = True )
268
- elif (v % self ._width == self ._width - 1 ):
269
- # pixel is on right edge
270
- self ._edgeTodistTo (v , transposed , edgeR = True )
271
- else :
272
- self ._edgeTodistTo (v , transposed )
255
+ edges = []
256
+ if v % self ._width == 0 :
257
+ edges .append ('left' )
258
+ if v % self ._width == self ._width - 1 :
259
+ edges .append ("right" )
260
+ if v / self ._width == 0 :
261
+ edges .append ("top" )
262
+ if v / self ._width == self ._height - 1 :
263
+ edges .append ("bottom" )
264
+
265
+ if direction == "vertical" and "top" in edges :
266
+ continue
267
+ if direction == "horizontal" and "left" in edges :
268
+ continue
269
+
270
+ self ._edgeTodistTo (v , direction , edges )
271
+
273
272
# edgeTo[sink] is vertex in last row with min energy
274
273
index , min_energy = min (enumerate (self ._distTo [self ._num_pixels - self ._width :self ._num_pixels ]), key = lambda (x , y ): y )
275
274
self ._distTo [self ._sink ] = min_energy
276
275
self ._edgeTo [self ._sink ] = (self ._height - 1 ) * self ._width + index
277
276
278
277
279
278
280
- def _edgeTodistTo (self , pixel , transposed , edgeL = False , edgeR = False ):
279
+ def _edgeTodistTo (self , pixel , direction , edges ):
281
280
# returns pixel connected to v with min energy
282
- up_pixel = pixel - self ._width
283
- right_up_diagonal_pixel = pixel - self ._width + 1
284
- left_up_diagonal_pixel = pixel - self ._width - 1
285
-
286
- if edgeL :
287
- # left edge
288
- left_up_diagonal_pixel = up_pixel
289
- elif edgeR :
290
- # right edge
291
- right_up_diagonal_pixel = up_pixel
292
-
293
- # energy of pixels connected to pixel
294
- if transposed :
295
- (colU , rowU ) = self ._toGrid (left_up_diagonal_pixel )
296
- (colC , rowC ) = self ._toGrid (up_pixel )
297
- (colD , rowD ) = self ._toGrid (right_up_diagonal_pixel )
298
- # read energy
299
- eLU = self ._energy [self ._height * colU + rowU ]
300
- eC = self ._energy [self ._height * colC + rowC ]
301
- eRD = self ._energy [self ._height * colD + rowD ]
302
- else :
303
- # read energy directly from energy array
304
- eLU = self ._energy [left_up_diagonal_pixel ]
305
- eC = self ._energy [up_pixel ]
306
- eRD = self ._energy [right_up_diagonal_pixel ]
307
- #print (eLU, left_up_diagonal_pixel), (eC, up_pixel), (eRD, right_up_diagonal_pixel)
281
+
282
+ if direction == "vertical" :
283
+ ancestor_one = pixel - self ._width - 1
284
+ ancestor_two = pixel - self ._width
285
+ ancestor_three = pixel - self ._width + 1
286
+ if 'left' in edges :
287
+ ancestor_one = ancestor_two
288
+ elif 'right' in edges :
289
+ ancestor_three = ancestor_two
290
+
291
+ else : #horizontal
292
+ ancestor_one = pixel + self ._width - 1
293
+ ancestor_two = pixel - 1
294
+ ancestor_three = pixel - self ._width - 1
295
+ if 'top' in edges :
296
+ ancestor_three = ancestor_two
297
+ elif 'bottom' in edges :
298
+ ancestor_one = ancestor_two
299
+
300
+ eLU = self ._energy [ancestor_one ]
301
+ eC = self ._energy [ancestor_two ]
302
+ eRD = self ._energy [ancestor_three ]
303
+ #print (eLU, ancestor_one), (eC, ancestor_two), (eRD, ancestor_three)
308
304
# find min distance and its associated vertex
309
- dist , from_vertex = min ((self ._distTo [left_up_diagonal_pixel ] + eLU , left_up_diagonal_pixel ), (self ._distTo [up_pixel ] + eC , up_pixel ), (self ._distTo [right_up_diagonal_pixel ] + eRD , right_up_diagonal_pixel ))
310
- #e, vertex = min([(eC, up_pixel ), (eLU, left_up_diagonal_pixel ), (eRD, right_up_diagonal_pixel )])
305
+ dist , from_vertex = min ((self ._distTo [ancestor_one ] + eLU , ancestor_one ), (self ._distTo [ancestor_two ] + eC , ancestor_two ), (self ._distTo [ancestor_three ] + eRD , ancestor_three ))
306
+ #e, vertex = min([(eC, ancestor_two ), (eLU, ancestor_one ), (eRD, ancestor_three )])
311
307
self ._edgeTo [pixel ] = from_vertex
312
308
self ._distTo [pixel ] = dist
313
309
0 commit comments