@@ -254,7 +254,7 @@ def __init__(self, img, unaries, numlabel):
254
254
self .label = range (numlabel )
255
255
256
256
# Initial labeling. All = 0
257
- self .y = np .empty ((img .shape [0 ], img .shape [1 ]))
257
+ self .y = np .zeros ((img .shape [0 ], img .shape [1 ]))
258
258
259
259
self .l = 0.5
260
260
self .w = 3.5
@@ -363,8 +363,8 @@ def pairwiseenergy(self, y1, y2, x1, x2):
363
363
energy = self .w * np .exp (- self .l * np .power (np .linalg .norm (x1 - x2 , 2 ), 2 ))
364
364
return energy
365
365
366
- def segment (self ):
367
- for i in range (3 ):
366
+ def segment (self , iterations ):
367
+ for i in range (iterations ):
368
368
# For each label: Change current label to alpha?
369
369
for alpha in self .label :
370
370
self .alpha = alpha
@@ -394,17 +394,21 @@ def getimg(self):
394
394
return self .img
395
395
396
396
397
- def loadunaryfile (filename , ysize , xsize , labels ):
397
+ def loadunaryfile (filename ):
398
+ file = open (filename , "r" )
399
+
400
+ xsize = int (file .readline ())
401
+ ysize = int (file .readline ())
402
+ labels = int (file .readline ())
403
+
398
404
data = np .empty ((ysize , xsize , labels ))
399
- file = open ("filename" , "r" )
400
- for y in range (ysize ):
401
- for x in range (xsize ):
402
- line = file .readline ()
403
- strdata = line .split ("," )
404
- floatdata = []
405
- for i in strdata :
406
- floatdata .append (float (i ))
407
- data [y , x ] = floatdata
405
+
406
+ for x in range (xsize ):
407
+ for y in range (ysize ):
408
+ for l in range (labels ):
409
+ data [y , x , l ] = float (file .readline ())
410
+
411
+ return data
408
412
409
413
410
414
def binseg ():
@@ -430,18 +434,20 @@ def binseg():
430
434
431
435
432
436
def alphaexp ():
433
- imagename = ""
434
- unaryfilename = ""
437
+ imagename = "1_27_s.bmp "
438
+ unaryfilename = "1_27_s.c_unary.txt "
435
439
436
440
logging .info ("Read image." )
437
- img = misc .imread (imagename )
441
+ img = misc .imread (os . path . join ( "data" , imagename ) )
438
442
img = np .array (img , dtype = np .float64 ) / 255
439
443
440
- logging .info ("Load unaries." ) #
441
- unaries = loadunaryfile (unaryfilename , img .shape [0 ], img .shape [1 ], 21 )
444
+ logging .info ("Load unaries." )
445
+ unaries = loadunaryfile (os .path .join ("data" , unaryfilename ))
446
+ unaries = - np .log (unaries )
447
+ numlabels = unaries .shape [2 ]
442
448
443
- binseg = BinsegAlphaexp (img , unaries , 21 )
444
- binseg .segment ()
449
+ binseg = BinsegAlphaexp (img , unaries , numlabels )
450
+ binseg .segment (3 )
445
451
446
452
logging .info ("Save image." )
447
453
img = binseg .getimg ().astype (np .uint8 )
@@ -463,7 +469,7 @@ def alphaexpbinary():
463
469
unaries = np .load ("unary.npy" )
464
470
465
471
binseg = BinsegAlphaexp (img , unaries , 2 )
466
- binseg .segment ()
472
+ binseg .segment (1 )
467
473
468
474
logging .info ("Save image." )
469
475
img = binseg .getimg ().astype (np .uint8 )
0 commit comments