Commit 73c83038 authored by Taddeüs Kroes's avatar Taddeüs Kroes

improc ass4: Added 1d Gaussian function to timer option.

parent 0c734114
#!/usr/bin/env python
from numpy import zeros, arange, pi, e, ceil, meshgrid
from numpy import zeros, arange, pi, e, ceil, meshgrid, array
from matplotlib.pyplot import imread, imshow, plot, xlabel, ylabel, show, \
subplot, xlim, savefig
from mpl_toolkits.mplot3d import Axes3D
from scipy.ndimage import convolve
from scipy.ndimage import convolve, convolve1d
from time import time
from sys import argv, exit
def exit_with_usage():
"""Print an error message with the program's usage and exit the program."""
print 'Usage: python %s timer REPEAT | diff SCALE' % argv[0]
print 'Usage: python %s timer METHOD [ REPEAT ] | diff SCALE' % argv[0]
exit(1)
def Gauss(s):
"""Create a sampled Gaussian function of scale s."""
"""Sample a two-dimensional Gaussian function of scale s."""
size = int(ceil(3 * s))
r = 2 * size + 1
r = 2 * size
W = zeros((r, r))
t = s ** 2.
t = float(s) ** 2
a = 1 / (2 * pi * t)
# Sample the Gaussian function
......@@ -28,33 +28,68 @@ def Gauss(s):
# Make sure that the sum of all kernel values is equal to one
return W / W.sum()
if len(argv) < 2:
def Gauss1(s):
"""Sample a one-dimensional Gaussian function of scale s."""
size = int(ceil(3 * s))
r = 2 * size
W = zeros((r,))
t = float(s) ** 2
a = 1 / (2 * pi * t)
# Sample the Gaussian function
W = array([a * e ** -((x - size) ** 2 / (2 * t)) for x in xrange(r)])
# Make sure that the sum of all kernel values is equal to one
return W / W.sum()
if __name__ == '__main__':
if len(argv) < 2:
exit_with_usage()
F = imread('cameraman.png')
F = imread('cameraman.png')
#W1 = Gauss1(10)
#X = arange(W1.shape[0])
#G = convolve1d(F, W1, axis=0, mode='nearest')
#subplot(121)
#imshow(F, cmap='gray')
#subplot(122)
#imshow(G, cmap='gray')
#show()
#exit(0)
if argv[1] == 'timer':
if len(argv) < 3:
exit_with_usage()
method = argv[2]
if argv[1] == 'timer':
# Time for multiple scales
S = [1, 2, 3, 5, 7, 9, 11, 15, 19]
repeat = int(argv[2])
timings = []
repeat = int(argv[3]) if len(argv) > 3 else 1
n = 0
times = []
for i, s in enumerate(S):
t = 0
for k in xrange(repeat):
W = Gauss(s)
start = time()
convolve(F, W, mode='nearest')
if method == '1d':
convolve1d(F, Gauss1(s), axis=n, mode='nearest')
elif method == '2d':
convolve(F, Gauss(s), mode='nearest')
t += time() - start
timings.append(t / repeat)
times.append(t / repeat)
xlim(S[0], S[-1])
xlabel('s')
ylabel('time (s)')
plot(S, timings, 'o-')
elif argv[1] == 'diff':
plot(S, times, 'o-')
elif argv[1] == 'diff':
# Calculate and plot the convolution of the given scale
if len(argv) < 3:
exit_with_usage()
......@@ -69,7 +104,7 @@ elif argv[1] == 'diff':
# Gauss function (3D plot)
x = arange(W.shape[0])
X, Y = meshgrid(x, x)
Y, X = meshgrid(x, x)
ax = subplot(132, projection='3d')
stride = s / 4
ax.plot_surface(X, Y, W, rstride=stride, cstride=stride, cmap='jet')
......@@ -81,4 +116,4 @@ elif argv[1] == 'diff':
subplot(133)
imshow(G, cmap='gray')
show()
show()
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