Skip to content
This repository has been archived by the owner on Jan 17, 2019. It is now read-only.

Commit

Permalink
edgeDetection done
Browse files Browse the repository at this point in the history
  • Loading branch information
LeC-D committed Nov 23, 2018
1 parent aff56a3 commit 9766013
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 70 deletions.
6 changes: 3 additions & 3 deletions Projet/Bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def run(self):

edges = self.edgeDetec.detect_edges(image)

test2 = np.asarray(edges)
clippedEdges = self.clip.clip(edges)

test2 = np.asarray(clippedEdges)
test = Image.fromarray(test2)
plot = plt.imshow(test)
plt.show()

#clippedEdges = self.clip.clip(edges)
#plot = plt.imshow(clippedEdges)
#plt.show()

Expand Down
4 changes: 2 additions & 2 deletions Projet/Clipping/Clipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def clip(self, edgesImage):

def calculY1(self):
y1 = len(self.image)
for i in range(len(self.image), 0, -1):
for i in range(len(self.image) - 1, 0, -1):
for j in range(0, len(self.image[i])):
if j < len(self.image[i]) - 3:
if self.image[i][j] == WHITE_PIXEL and self.image[i][j+1] == WHITE_PIXEL and self.image[i][j+1] == WHITE_PIXEL:
Expand All @@ -26,7 +26,7 @@ def calculY1(self):
def calculY2(self):
y2 = len(self.image)
diff = -1
for i in range(len(self.image), 0, -1):
for i in range(len(self.image) - 1, 0, -1):
first_white_pixel = -1
last_white_pixel = len(self.image[i])
for j in range(0,len(self.image[i])):
Expand Down
Binary file modified Projet/Clipping/__pycache__/Clipping.cpython-35.pyc
Binary file not shown.
91 changes: 26 additions & 65 deletions Projet/EdgeDetection/EdgeDetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@
class EdgeDetection:
def __init__(self):
self.threshold_1 = 80
self.threshold_2 = 65
self.threshold_2 = 80

def calc_offset(self, angle):
if math.cos(angle) == 0:
dl = 0
else:
dl = math.cos(angle) / abs(math.cos(angle))
if math.sin(angle) == 0:

epsilon = 0.0001

if abs(math.cos(angle)) < epsilon:
dk = 0
else:
dk = math.sin(angle) / abs(math.sin(angle))
dk = math.cos(angle) / abs(math.cos(angle))

if abs(math.sin(angle)) < epsilon:
dl = 0
else:
dl = math.sin(angle) / abs(math.sin(angle))

return [int(round(dl)), int(round(dk))]

def round_angles(self, angles):
ret_angles = np.zeros((len(angles), len(angles[0])))
comp = math.pi / 16
comp = math.pi / 8

for i in range(0, len(angles)):
for j in range (0, len(angles[0])):
for k in range(0, 7):
ret_angles[i][j] = k * math.pi / 8 if angles[i][j] - k * math.pi / 8 <= comp else ret_angles[i][j]
for k in range(-4, 5):
ret_angles[i][j] = k * math.pi / 4 if abs(angles[i][j] - k * math.pi / 4) <= comp else ret_angles[i][j]

return ret_angles

Expand All @@ -42,12 +47,8 @@ def operator(self, operande):
sobel_y = cv2.Sobel(filtered_image, cv2.CV_64F, 0, 1, ksize=5)

grad_mags = np.sqrt(np.square(np.array(sobel_x)) + np.square(np.array(sobel_y)))
grad_mags = (grad_mags / np.amax(np.amax(grad_mags, 1), 0))*255
grad_mags = grad_mags.astype(int)

second_sobel_x = sobel(grad_mags.tolist(), 0)
second_sobel_y = sobel(grad_mags.tolist(), 1)
grad_angles = self.round_angles(np.arctan(np.divide(np.array(second_sobel_y), np.array(second_sobel_x))))
grad_angles = self.round_angles(np.arctan2(np.array(sobel_y), np.array(sobel_x)))

return [grad_mags, grad_angles]

Expand All @@ -60,62 +61,22 @@ def non_maxima_supp(self, grad_image):
if i == 0 or j == 0 or i == len(grad_mags) - 1 or j == len(grad_mags[i]) - 1:
grad_mags[i][j] = 0

grad_mags_ret = copy.deepcopy(np.asarray(grad_mags)).tolist()
grad_mags_ret = np.zeros((len(grad_mags), len(grad_mags[0]))).tolist()

for i in range(0, len(grad_mags)):
for j in range(0, len(grad_mags[i])):

if grad_mags[i][j] > 0:
l = i
k = j
offset = self.calc_offset(grad_angles[i][j])

dl = offset[0]
dk = offset[1]

while grad_mags[l][k] < grad_mags[l + dl][k + dk] or grad_mags[l][k] < grad_mags[l - dl][k - dk]:
if not(grad_mags[l][k] >= grad_mags[l][k - 1] and grad_mags[l][k] >= grad_mags[l][k + 1] or
grad_mags[l][k] >= grad_mags[l - 1][k] and grad_mags[l][k] >= grad_mags[l + 1][k]):

grad_mags_ret[l][k] = 0

if grad_mags[l + dl][k + dk] > grad_mags[l - dl][k - dk]:
l = l + dl
k = k + dk
else:
l = l - dl
k = k - dk

offset = self.calc_offset(grad_angles[l][k])
dl = offset[0]
dk = offset[1]

for l in range(0, len(grad_mags_ret)):
for k in range(0, len(grad_mags_ret[l])):
if grad_mags_ret[l][k] > 0:
count = 0
count = count + 1 if grad_mags_ret[l][k - 1] != 0 else count
count = count + 1 if grad_mags_ret[l][k + 1] != 0 else count
count = count + 1 if grad_mags_ret[l - 1][k] != 0 else count
count = count + 1 if grad_mags_ret[l + 1][k] != 0 else count

count = count + 1 if grad_mags_ret[l - 1][k - 1] != 0 else count
count = count + 1 if grad_mags_ret[l - 1][k + 1] != 0 else count
count = count + 1 if grad_mags_ret[l + 1][k + 1] != 0 else count
count = count + 1 if grad_mags_ret[l + 1][k - 1] != 0 else count

if l == 298 and k == 140:
print(grad_mags_ret[l][k - 1])
print(grad_mags_ret[l][k + 1])
print(grad_mags_ret[l - 1][k])
print(grad_mags_ret[l + 1][k])
print(grad_mags_ret[l - 1][k - 1])
print(grad_mags_ret[l - 1][k + 1])
print(grad_mags_ret[l + 1][k + 1])
print(grad_mags_ret[l + 1][k - 1])

if count >= 4:
grad_mags_ret[l][k] = 0
di = offset[0]
dj = offset[1]

if grad_mags[i][j] > grad_mags[i + di][j + dj] and grad_mags[i][j] > grad_mags[i - di][j - dj]:
grad_mags_ret[i][j] = grad_mags[i][j]

grad_mags_ret = ((grad_mags_ret - np.amin(np.amin(grad_mags_ret, 1), 0)) / (np.amax(np.amax(grad_mags_ret, 1), 0)
- np.amin(np.amin(grad_mags_ret, 1), 0))) * 255
grad_mags_ret = grad_mags_ret.astype(int)

return grad_mags_ret

Expand Down
Binary file modified Projet/EdgeDetection/__pycache__/EdgeDetection.cpython-35.pyc
Binary file not shown.
Binary file modified Projet/__pycache__/Bootstrap.cpython-35.pyc
Binary file not shown.

0 comments on commit 9766013

Please sign in to comment.