Commit 7cb2a9ff authored by Taddeüs Kroes's avatar Taddeüs Kroes

improc ass4: Source code cleanup.

parent 3ce74ceb
......@@ -7,7 +7,7 @@ from gauss import Gauss1, gD
def in_image(p, F):
"""Check if given pixel coordinates p are within the bound of image F."""
return p[0] >= 0 and p[1] >= 0 and p[0] < F.shape[0] and p[1] < F.shape[1]
return 0 <= p[0] < F.shape[0] and 0 <= p[1] < F.shape[1]
def canny(F, s, Tl=None, Th=None):
"""Apply the Canny Edge Detection algorithm with Gauss scale s to an
......@@ -28,24 +28,24 @@ def canny(F, s, Tl=None, Th=None):
p = (y, x)
# Gradient norm and rounded angle
G[p] = norm(append(Gx[p], Gy[p]))
G[p] = norm(append(Gy[p], Gx[p]))
A[p] = int(round(arctan2(Gy[p], Gx[p]) * 4 / pi + 1)) % 4
# Non-maximum suppression
E = zeros(F.shape)
for x in xrange(F.shape[0]):
for y in xrange(F.shape[1]):
g = G[x, y]
a = A[x, y]
compare = [((x, y - 1), (x, y + 1)), ((x - 1, y - 1), \
(x + 1, y + 1)), ((x - 1, y), (x + 1, y)), \
((x + 1, y - 1), (x - 1, y + 1))]
for y in xrange(F.shape[0]):
for x in xrange(F.shape[1]):
g = G[y, x]
a = A[y, x]
compare = [((y, x - 1), (y, x + 1)), ((y - 1, x - 1), \
(y + 1, x + 1)), ((y - 1, x), (y + 1, x)), \
((y + 1, x - 1), (y - 1, x + 1))]
na, nb = compare[a]
if (not in_image(na, G) or g > G[na]) \
and (not in_image(nb, G) or g > G[nb]):
E[x, y] = g
E[y, x] = g
# Only execute hysteresis thresholding if the thresholds are specified
if Tl is None or Th is None:
......@@ -57,29 +57,29 @@ def canny(F, s, Tl=None, Th=None):
T = zeros(F.shape, dtype=bool)
# Clear image borders
for x in xrange(F.shape[0]):
E[x, 0] = E[x, F.shape[1] - 1] = 0
for y in xrange(F.shape[0]):
E[x, 0] = E[y, F.shape[1] - 1] = 0
for y in xrange(1, F.shape[1] - 1):
E[0, y] = E[F.shape[0] - 1, y] = 0
for x in xrange(x, F.shape[1] - 1):
E[0, x] = E[F.shape[0] - 1, x] = 0
def follow_nb(x, y):
def follow_nb(y, x):
"""Follow the neighbouring pixels of an edge pixel in E recursively."""
if T[x, y]:
if T[y, x]:
return
T[x, y] = True
T[y, x] = True
for nx in xrange(-1, 2):
for ny in xrange(-1, 2):
if (nx != 0 or ny != 0) and E[nx, ny] > Tl:
follow_nb(nx, ny)
for nx in xrange(-1, 2):
if (ny or nx) and E[ny, nx] > Tl:
follow_nb(ny, nx)
# Follow edges that have a starting value above Th
for x in xrange(F.shape[0]):
for y in xrange(F.shape[1]):
if E[x, y] > Th:
follow_nb(x, y)
for y in xrange(F.shape[0]):
for x in xrange(F.shape[1]):
if E[y, x] > Th:
follow_nb(y, x)
return E, T
......
......@@ -31,12 +31,17 @@ def Gauss(s):
return W / W.sum()
def f_gauss(x, s):
"""Return the Gaussian function for a given x and scale."""
return exp(-(x ** 2 / (2 * s ** 2))) / (sqrt(2 * pi) * s)
def f_gauss_der_1(x, s):
"""Return the first derivative of the Gaussian function for a given x
and scale."""
return -x * exp(-(x ** 2 / (2 * s ** 2))) / (sqrt(2 * pi) * s ** 3)
def f_gauss_der_2(x, s):
"""Return the second derivative of the Gaussian function for a given x
and scale."""
return (x ** 2 - s ** 2) * exp(-(x ** 2 / (2 * s ** 2))) \
/ (sqrt(2 * pi) * s ** 5)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment