Commit 03165c7d authored by Taddes Kroes's avatar Taddes Kroes

improc ass4: Fixed angles bug in Canny and added subplot titles.

parent 94f437b9
#!/usr/bin/env python
from matplotlib.pyplot import imread, imshow, subplot, show
from matplotlib.pyplot import imread, imshow, subplot, show, axis
from numpy import arctan2, zeros, append, pi
from numpy.linalg import norm
from scipy.ndimage import convolve1d
......@@ -29,19 +29,19 @@ def canny(F, s, Tl=None, Th=None):
# Gradient norm and rounded angle
G[p] = norm(append(Gy[p], Gx[p]))
A[p] = int(round(arctan2(Gy[p], Gx[p]) * 4 / pi + 1)) % 4
A[p] = (4 - int(round(4 * arctan2(Gy[p], Gx[p]) / pi))) % 4
# Non-maximum suppression
E = zeros(F.shape)
compare = [((-1, 0), (1, 0)), ((-1, 1), (1, -1)), \
((0, 1), (0, -1)), ((1, 1), (-1, -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]
a, b = compare[A[y, x]]
na = (y + a[1], x + a[0])
nb = (y + b[1], x + b[0])
if (not in_image(na, G) or g > G[na]) \
and (not in_image(nb, G) or g > G[nb]):
......@@ -101,19 +101,24 @@ if __name__ == '__main__':
# Execute with tracing edges
E, T = canny(F, s, float(argv[2]), float(argv[3]))
subplot(131)
subplot(131, title='Original image')
imshow(F, cmap='gray')
subplot(132)
axis('off')
subplot(132, title='Gradient magnitudes')
imshow(E, cmap='gray')
subplot(133)
axis('off')
subplot(133, title='Thresholds applied')
imshow(T, cmap='gray')
axis('off')
else:
# Execute until nn-maximum suppression
E = canny(F, s)
subplot(121)
subplot(121, title='Original image')
imshow(F, cmap='gray')
subplot(122)
axis('off')
subplot(122, title='Gradient magnitudes')
imshow(E, cmap='gray')
axis('off')
show()
......@@ -2,7 +2,7 @@
from numpy import zeros, arange, meshgrid, array, matrix
from math import ceil, exp, pi, sqrt
from matplotlib.pyplot import imread, imshow, plot, xlabel, ylabel, show, \
subplot, xlim, savefig, axis
subplot, xlim, savefig, axis, ticklabel_format
from mpl_toolkits.mplot3d import Axes3D
from scipy.ndimage import convolve, convolve1d
from time import time
......@@ -96,11 +96,13 @@ if __name__ == '__main__':
G = convolve(F, W, mode='nearest')
# Show the original image, kernel and convoluted image respectively
subplot(131)
subplot(131, title='Original image')
imshow(F, cmap='gray')
plot_kernel(W, subplot(132, projection='3d'))
subplot(133)
axis('off')
plot_kernel(W, subplot(132, projection='3d', title='Kernel'))
subplot(133, title='Convoluted image')
imshow(G, cmap='gray')
axis('off')
elif argv[1] == '1d':
"""Calculate the gaussian kernel using derivatives of the specified
order in both directions."""
......@@ -119,11 +121,13 @@ if __name__ == '__main__':
W = Fy.T * Fx
# Show the original image, kernel and convoluted image respectively
subplot(131)
subplot(131, title='Original image')
imshow(F, cmap='gray')
plot_kernel(W, subplot(132, projection='3d'))
subplot(133)
axis('off')
plot_kernel(W, subplot(132, projection='3d', title='Kernel'))
subplot(133, title='Convoluted image')
imshow(G, cmap='gray')
axis('off')
elif argv[1] == 'timer':
"""Time the performance of a 1D/2D convolution and plot the results."""
if len(argv) < 3:
......
......@@ -88,7 +88,7 @@ The result of the \texttt{Gauss} function is shown in figure
Gaussian kernel and the convolved image.
\begin{figure}[H]
\hspace{-5cm}
\hspace{-3cm}
\includegraphics[scale=.6]{gauss_2d_5.pdf}
\caption{The result of \texttt{python gauss.py 2d 5}.}
\label{fig:gauss-2d}
......@@ -202,12 +202,12 @@ the resulting plot will contain an additional image containing a binary image
of edges. The thresholds can be specified in the range 0-255, they are scaled
down by the program to match the image's color range. An example execution of
edge detection on the cameraman image using a scale of 2, a lower threshold of
20 and a higher threshold of 60, can be viewed in figure \ref{fig:canny}.
50 and a higher threshold of 70, can be viewed in figure \ref{fig:canny}.
\begin{figure}[H]
\hspace{-5cm}
\includegraphics[scale=.6]{canny_2_20_60.pdf}
\caption{The result of \texttt{python canny.py 2 20 60}.}
\includegraphics[scale=.6]{canny_2_50_70.pdf}
\caption{The result of \texttt{python canny.py 2 50 70}.}
\label{fig:canny}
\end{figure}
......
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