Skip to content

Commit fb9dce6

Browse files
author
JeGa
committed
Some fixes.
1 parent 54545f9 commit fb9dce6

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

binseg.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def __init__(self, img, unaries, numlabel):
254254
self.label = range(numlabel)
255255

256256
# 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]))
258258

259259
self.l = 0.5
260260
self.w = 3.5
@@ -363,8 +363,8 @@ def pairwiseenergy(self, y1, y2, x1, x2):
363363
energy = self.w * np.exp(-self.l * np.power(np.linalg.norm(x1 - x2, 2), 2))
364364
return energy
365365

366-
def segment(self):
367-
for i in range(3):
366+
def segment(self, iterations):
367+
for i in range(iterations):
368368
# For each label: Change current label to alpha?
369369
for alpha in self.label:
370370
self.alpha = alpha
@@ -394,17 +394,21 @@ def getimg(self):
394394
return self.img
395395

396396

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+
398404
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
408412

409413

410414
def binseg():
@@ -430,18 +434,20 @@ def binseg():
430434

431435

432436
def alphaexp():
433-
imagename = ""
434-
unaryfilename = ""
437+
imagename = "1_27_s.bmp"
438+
unaryfilename = "1_27_s.c_unary.txt"
435439

436440
logging.info("Read image.")
437-
img = misc.imread(imagename)
441+
img = misc.imread(os.path.join("data", imagename))
438442
img = np.array(img, dtype=np.float64) / 255
439443

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]
442448

443-
binseg = BinsegAlphaexp(img, unaries, 21)
444-
binseg.segment()
449+
binseg = BinsegAlphaexp(img, unaries, numlabels)
450+
binseg.segment(3)
445451

446452
logging.info("Save image.")
447453
img = binseg.getimg().astype(np.uint8)
@@ -463,7 +469,7 @@ def alphaexpbinary():
463469
unaries = np.load("unary.npy")
464470

465471
binseg = BinsegAlphaexp(img, unaries, 2)
466-
binseg.segment()
472+
binseg.segment(1)
467473

468474
logging.info("Save image.")
469475
img = binseg.getimg().astype(np.uint8)

0 commit comments

Comments
 (0)