This repository has been archived by the owner on Jan 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yacine Benouniche
committed
Nov 24, 2018
1 parent
c802ac8
commit b53df62
Showing
1 changed file
with
61 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |