From b53df627ba27987e7ecba22c75dcf67f1dbee21a Mon Sep 17 00:00:00 2001 From: Yacine Benouniche Date: Sat, 24 Nov 2018 13:35:19 -0500 Subject: [PATCH] boundaryTracing update --- Projet/BoundaryTracing/BoudaryTracing.py | 95 +++++++++++++++--------- 1 file changed, 61 insertions(+), 34 deletions(-) diff --git a/Projet/BoundaryTracing/BoudaryTracing.py b/Projet/BoundaryTracing/BoudaryTracing.py index 5d242bf..f4b4239 100644 --- a/Projet/BoundaryTracing/BoudaryTracing.py +++ b/Projet/BoundaryTracing/BoudaryTracing.py @@ -1,85 +1,112 @@ import math +import numpy as np WHITE_PIXEL = 255 - +BLACK_PIXEL = 0 class BoundaryTracing: - def __init__(self, image): - self.image = image + def __init__(self): + self.image = None + self.minx = None + self.maxx = None + self.miny = None + self.maxy = None self.initial_trace_point = (0, 0) + self.UD = 0 + self.LR = 0 + + def run(self, image): + self.image = image self.minx = self.calcul_minx() self.maxx = self.calcul_maxx() self.miny = self.calcul_miny() self.maxy = self.calcul_maxy() - self.optimal_y = 0 - self.UD = 0 - self.LR = 0 - - def run(self): - self.optimal_y = self.optimal_y_level() self.initial_trace_direction() + self.trace() + self.flush() - def optimal_y_level(self): - dy = self.maxy - self.miny - optimaly = math.floor(0.33 * dy) - return optimaly + def flush(self): + self.image = None + self.minx = None + self.maxx = None + self.miny = None + self.maxy = None + + # def optimal_y_level(self): + # print(self.maxy) + # print(self.miny) + # dy = self.maxy - self.miny + # optimaly = math.floor(0.33 * dy) + # return optimaly def initial_trace_direction(self): self.initial_trace_point = (0, 0) - for i in range(0, len(self.image[self.optimal_y])): - if len(self.image[self.optimal_y][i]) == WHITE_PIXEL: - self.initial_trace_point = (i, self.optimal_y) + for i in range(self.minx, self.maxx): + if self.image[self.maxy][i] != BLACK_PIXEL: + self.initial_trace_point = (i, self.maxy) break x = self.initial_trace_point[0] y = self.initial_trace_point[1] self.UD = 1 - while y < self.miny and self.image[y][x] == WHITE_PIXEL: + while y >= self.miny and self.image[y][x] != BLACK_PIXEL: y = y - 1 if y > 0 and x < len(self.image[y]): - if self.image[y][x+1] == WHITE_PIXEL or self.image[y-1][x+1]: - self.LR = -1 - if y > 0 and x > 0: - if self.image[y][x-1] == WHITE_PIXEL or self.image[y-1][x-1]: + if self.image[y][x+1] != BLACK_PIXEL or self.image[y-1][x+1]: self.LR = 1 + if y > 0 and x > 0: + if self.image[y][x-1] != BLACK_PIXEL or self.image[y-1][x-1]: + self.LR = -1 def trace(self): x = self.initial_trace_point[0] y = self.initial_trace_point[1] count = 0 + print(x, ' ', y, ' ', self.LR, ' ', self.UD) + + mask = np.zeros((len(self.image), len(self.image[0]))) + while x != self.maxx: + if self.UD == 1: + y = y - 1 + elif self.UD == -1: + y = y + 1 + + if self.LR == 1 and self.image[y][x+1] != BLACK_PIXEL: + x = x + 1 + elif self.LR == -1 and self.image[y][x-1] != BLACK_PIXEL: + x = x - 1 def calcul_minx(self): minx = 0 - for i in range(0, len(self.image[0])): - for j in range(0, len(self.image)): - if self.image[j][i] == WHITE_PIXEL: + for i in range(1, len(self.image[0]) - 1): + for j in range(1, len(self.image) - 1): + if self.image[j][i] != BLACK_PIXEL: minx = i return minx return minx def calcul_maxx(self): maxx = 0 - for i in range(len(self.image[0]), 0, -1): - for j in range(0, len(self.image)): - if self.image[j][i] == WHITE_PIXEL: + for i in range(len(self.image[0]) - 1, 0, -1): + for j in range(1, len(self.image) - 1): + if self.image[j][i] != BLACK_PIXEL: maxx = i return maxx return maxx - def calcul_miny(self): miny = 0 - for i in range(0, len(self.image)): - for j in range(0, len(self.image[i])): - if self.image[i][j] == WHITE_PIXEL: + for i in range(1, len(self.image) - 1): + for j in range(1, len(self.image[i]) - 1): + if self.image[i][j] != BLACK_PIXEL: miny = i return miny return miny def calcul_maxy(self): maxy = 0 - for i in range(len(self.image), 0, -1): - for j in range(0, len(self.image[i])): - if self.image[i][j] == WHITE_PIXEL: + for i in range(len(self.image) - 2, 0, -1): + for j in range(1, len(self.image[i]) - 1): + if self.image[i][j] != BLACK_PIXEL: maxy = i return maxy return maxy