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