Commit 4b7f8708 authored by Taddeüs Kroes's avatar Taddeüs Kroes

improc ass4: Used linear independency of Gauss function in gD.

parent 86da37b2
...@@ -19,10 +19,10 @@ def canny(F, s, Tl=None, Th=None): ...@@ -19,10 +19,10 @@ def canny(F, s, Tl=None, Th=None):
image F. Optionally specify a low and high threshold (Tl and Th) for image F. Optionally specify a low and high threshold (Tl and Th) for
hysteresis thresholding.""" hysteresis thresholding."""
# Noise reduction by a Gaussian filter # Noise reduction by a Gaussian filter
F = gD(F, s, 0, 0)[1] F = gD(F, s, 0, 0)
# Find intensity gradient # Find intensity gradient
mask = Gauss1(1, 1) mask = Gauss1(1.4, 1)
Gx = convolve1d(F, mask, axis=1, mode='nearest') Gx = convolve1d(F, mask, axis=1, mode='nearest')
Gy = convolve1d(F, mask, axis=0, mode='nearest') Gy = convolve1d(F, mask, axis=0, mode='nearest')
G = zeros(F.shape) G = zeros(F.shape)
......
#!/usr/bin/env python #!/usr/bin/env python
from numpy import zeros, arange, pi, e, ceil, meshgrid, array from numpy import zeros, arange, pi, e, ceil, meshgrid, array, dot
from matplotlib.pyplot import imread, imshow, plot, xlabel, ylabel, show, \ from matplotlib.pyplot import imread, imshow, plot, xlabel, ylabel, show, \
subplot, xlim, savefig subplot, xlim, savefig
from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d import Axes3D
...@@ -29,32 +29,35 @@ def Gauss(s): ...@@ -29,32 +29,35 @@ def Gauss(s):
# Make sure that the sum of all kernel values is equal to one # Make sure that the sum of all kernel values is equal to one
return W / W.sum() return W / W.sum()
def gauss(x, s): def f_gauss(x, s):
return e ** -(x ** 2 / (2 * s ** 2)) / (2 * pi * s ** 2) return e ** -(x ** 2 / (2 * s ** 2)) / (2 * pi * s ** 2)
def gauss_der_1(x, s): def f_gauss_der_1(x, s):
return -x * e ** -(x ** 2 / (2 * s ** 2)) / (2 * pi * s ** 4) return -x * e ** -(x ** 2 / (2 * s ** 2)) / (2 * pi * s ** 4)
def gauss_der_2(x, s): def f_gauss_der_2(x, s):
return (x ** 2 - s ** 2) * e ** -(x ** 2 / (2 * s ** 2)) \ return (x ** 2 - s ** 2) * e ** -(x ** 2 / (2 * s ** 2)) \
/ (2 * pi * s ** 6) / (2 * pi * s ** 6)
def Gauss1(s, order=0): def Gauss1(s, order=0):
"""Sample a one-dimensional Gaussian function of scale s.""" """Sample a one-dimensional Gaussian function of scale s."""
f = [gauss, gauss_der_1, gauss_der_2][order] f = [f_gauss, f_gauss_der_1, f_gauss_der_2][order]
s = float(s) s = float(s)
size = int(ceil(3 * s)) r = int(ceil(3 * s))
r = 2 * size + 1 size = 2 * r + 1
W = zeros(r) W = zeros(size)
#t = float(s) ** 2 #t = float(s) ** 2
#a = 1 / (2 * pi * t) #a = 1 / (2 * pi * t)
# Sample the Gaussian function # Sample the Gaussian function
#W = array([a * e ** -((x - size) ** 2 / (2 * t)) for x in xrange(r)]) #W = array([a * e ** -((x - size) ** 2 / (2 * t)) for x in xrange(r)])
W = array([f(x - size, s) for x in xrange(r)]) W = array([f(x - r, s) for x in xrange(size)])
# Make sure that the sum of all kernel values is equal to one # Make sure that the sum of all kernel values is equal to one
return W / W.sum() if not order:
W /= W.sum()
return W
def plot_mask(W, ax): def plot_mask(W, ax):
"""""" """"""
...@@ -64,30 +67,17 @@ def plot_mask(W, ax): ...@@ -64,30 +67,17 @@ def plot_mask(W, ax):
#ax.plot_surface(X, Y, W, rstride=stride, cstride=stride, cmap='jet') #ax.plot_surface(X, Y, W, rstride=stride, cstride=stride, cmap='jet')
ax.plot_surface(X, Y, W, rstride=1, cstride=1, linewidth=0, \ ax.plot_surface(X, Y, W, rstride=1, cstride=1, linewidth=0, \
antialiased=True, cmap='jet') antialiased=True, cmap='jet')
ax.set_xlabel('x') ax.set_xlabel('y')
ax.set_ylabel('y') ax.set_ylabel('x')
ax.set_zlabel('g(x, y)') ax.set_zlabel('g(x, y)')
def gD(F, s, iorder, jorder): def gD(F, s, iorder, jorder):
"""Create the Gaussian derivative convolution of image F.""" """Create the Gaussian derivative convolution of image F."""
funcs = [gauss, gauss_der_1, gauss_der_2] Fy = Gauss1(s, iorder)
#funcs = [lambda x: e ** -(x ** 2 / (2 * s ** 2)) / (2 * pi * s ** 2), \ Fx = Fy if jorder == iorder else Gauss1(s, jorder)
# lambda x: -x * e ** -(x ** 2 / (2 * s ** 2)) \ W = dot(array([Fy]).T, array([Fx]))
# / (2 * pi * s ** 4), \
# lambda x: (x ** 2 - s ** 2) * e ** -(x ** 2 / (2 * s ** 2)) \
# / (2 * pi * s ** 6)]
size = int(ceil(3 * s))
r = 2 * size + 1
iterator = map(float, range(r))
W = zeros((r, r))
Fx = funcs[iorder]
Fy = funcs[jorder]
for x in iterator:
for y in iterator:
W[x, y] = Fx(x - size, s) * Fy(y - size, s)
return W, convolve(F, W, mode='nearest') return convolve(F, W, mode='nearest')
if __name__ == '__main__': if __name__ == '__main__':
if len(argv) < 2: if len(argv) < 2:
...@@ -100,7 +90,14 @@ if __name__ == '__main__': ...@@ -100,7 +90,14 @@ if __name__ == '__main__':
exit_with_usage() exit_with_usage()
s = float(argv[2]) s = float(argv[2])
W, G = gD(F, s, int(argv[3]), int(argv[4])) iorder = int(argv[3])
jorder = int(argv[4])
Fy = Gauss1(s, iorder)
Fx = Fy if jorder == iorder else Gauss1(s, jorder)
W = dot(array([Fy]).T, array([Fx]))
G = gD(F, s, iorder, jorder)
subplot(131) subplot(131)
imshow(F, cmap='gray') imshow(F, cmap='gray')
plot_mask(W, subplot(132, projection='3d')) plot_mask(W, subplot(132, projection='3d'))
......
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