Commit e54a3999 authored by Taddeüs Kroes's avatar Taddeüs Kroes

improc ass4: Updated usage of gauss.py.

parent 3895c9fd
...@@ -8,12 +8,6 @@ from scipy.ndimage import convolve, convolve1d ...@@ -8,12 +8,6 @@ from scipy.ndimage import convolve, convolve1d
from time import time from time import time
from sys import argv, exit 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 METHOD [ REPEAT ] | diff SCALE' \
' | der SCALE IORDER JORDER' % argv[0]
exit(1)
def Gauss(s): def Gauss(s):
"""Sample a two-dimensional Gaussian function of scale s.""" """Sample a two-dimensional Gaussian function of scale s."""
size = int(ceil(3 * s)) size = int(ceil(3 * s))
...@@ -80,13 +74,34 @@ def plot_kernel(W, ax): ...@@ -80,13 +74,34 @@ def plot_kernel(W, ax):
ax.set_ylabel('x') ax.set_ylabel('x')
ax.set_zlabel('g(x, y)') ax.set_zlabel('g(x, y)')
def exit_with_usage():
"""Print an error message with the program's usage and exit the program."""
print 'Usage: python %s ( 2d SCALE | 1d SCALE IORDER JORDER' \
' | timer METHOD [ REPEAT ] )' % argv[0]
exit(1)
if __name__ == '__main__': if __name__ == '__main__':
if len(argv) < 2: if len(argv) < 2:
exit_with_usage() exit_with_usage()
F = imread('cameraman.png') F = imread('cameraman.png')
if argv[1] == 'der': if argv[1] == '2d':
# Calculate and plot the convolution of the given scale
if len(argv) < 3:
exit_with_usage()
s = float(argv[2])
W = Gauss(s)
G = convolve(F, W, mode='nearest')
# Show the original image, kernel and convoluted image respectively
subplot(131)
imshow(F, cmap='gray')
plot_kernel(W, subplot(132, projection='3d'))
subplot(133)
imshow(G, cmap='gray')
elif argv[1] == '1d':
if len(argv) < 5: if len(argv) < 5:
exit_with_usage() exit_with_usage()
...@@ -96,10 +111,12 @@ if __name__ == '__main__': ...@@ -96,10 +111,12 @@ if __name__ == '__main__':
iorder = int(argv[3]) iorder = int(argv[3])
jorder = int(argv[4]) jorder = int(argv[4])
G = gD(F, s, iorder, jorder)
# Create a 2D weight function for plotting purposes only
Fy = matrix([Gauss1(s, iorder)]) Fy = matrix([Gauss1(s, iorder)])
Fx = Fy if jorder == iorder else matrix([Gauss1(s, jorder)]) Fx = Fy if jorder == iorder else matrix([Gauss1(s, jorder)])
W = Fy.T * Fx W = Fy.T * Fx
G = gD(F, s, iorder, jorder)
# Show the original image, kernel and convoluted image respectively # Show the original image, kernel and convoluted image respectively
subplot(131) subplot(131)
...@@ -140,20 +157,7 @@ if __name__ == '__main__': ...@@ -140,20 +157,7 @@ if __name__ == '__main__':
xlabel('s') xlabel('s')
ylabel('time (s)') ylabel('time (s)')
plot(S, times, 'o-') plot(S, times, 'o-')
elif argv[1] == 'diff': else:
# Calculate and plot the convolution of the given scale exit_with_usage()
if len(argv) < 3:
exit_with_usage()
s = float(argv[2])
W = Gauss(s)
G = convolve(F, W, mode='nearest')
# Show the original image, kernel and convoluted image respectively
subplot(131)
imshow(F, cmap='gray')
plot_kernel(W, subplot(132, projection='3d'))
subplot(133)
imshow(G, cmap='gray')
show() show()
...@@ -84,14 +84,14 @@ because the sum of the filter should be equal to 1, it is divided by its own ...@@ -84,14 +84,14 @@ because the sum of the filter should be equal to 1, it is divided by its own
sum. sum.
The result of the \texttt{Gauss} function is shown in figure The result of the \texttt{Gauss} function is shown in figure
\ref{fig:gauss-diff}. The subplots respectively show the original image, the \ref{fig:gauss-2d}. The subplots respectively show the original image, the
Gaussian kernel and the convolved image. Gaussian kernel and the convolved image.
\begin{figure}[H] \begin{figure}[H]
\label{fig:gauss-diff} \label{fig:gauss-2d}
\hspace{-5cm} \hspace{-5cm}
\includegraphics[scale=.6]{gauss_diff_5.pdf} \includegraphics[scale=.6]{gauss_2d_5.pdf}
\caption{The result of \texttt{python gauss.py diff 5}.} \caption{The result of \texttt{python gauss.py 2d 5}.}
\end{figure} \end{figure}
\subsection{Measuring Performance} \subsection{Measuring Performance}
......
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