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

ImProc ass1: Finished assignment 1 code.

parent 2bb870d9
from numpy import *
def linfilter1(f, w):
g = empty(f.shape, dtype=f.dtype)
M, N = f.shape
K, L = (array(w.shape) - 1) / 2
def value(i, j):
"""The function returning the value f[i,j] in case
(i,j) in an index 'in the image', otherwise it return 0"""
if i < 0 or i >= M or j < 0 or j >= N:
return 0
return f[i, j]
for j in xrange(N):
for i in xrange(M):
summed = 0
for k in xrange(-K, K + 1):
for l in xrange(-L, L + 1):
summed += value(i + k, j + l) * w[k + K, l + L]
g[i, j] = summed
return g
def linfilter2(f, w):
"""Linear Correlation based on neigborhood processing without loops"""
g = empty(f.shape, dtype=f.dtype)
M, N = f.shape
K, L = (array(w.shape) - 1) / 2
for j in xrange(N):
for i in xrange(M):
ii = minimum(M - 1, maximum(0, arange(i - K, i + K + 1)))
jj = minimum(N - 1, maximum(0, arange(j - L, j + L + 1)))
nbh = f[ix_(ii, jj)]
g[i, j] = (nbh * w).sum()
return g
def linfilter3(f, w):
"""Linear Correlation using Translates of Images"""
M, N = f.shape
K, L = (array(w.shape) - 1) / 2
di, dj = meshgrid(arange(-L, L + 1), arange(-K, K + 1))
didjw = zip(di.flatten(), dj.flatten(), w.flatten())
def translate(di,dj):
ii = minimum(M - 1, maximum(0, di + arange(M)))
jj = minimum(N - 1, maximum(0, dj + arange(N)))
return f[ix_(ii, jj)]
r = 0 * f
for di, dj, weight in didjw:
r += weight * translate(di, dj)
return r
def linfilter4(f, w):
return correlate(f, w, mode='nearest')
from linfilters import *
from scipy.ndimage.interpolation import zoom
from pylab import *
from time import time
repeat = 1
w = (3, 11)
f = zoom(imread('cameraman.png'), .25)
methods = [linfilter1, linfilter2, linfilter3]#, linfilter4]
timings = [[] for i in methods]
x = range(w[0], w[1] + 1, 2)
for i, method in enumerate(methods):
for j in x:
weight = ones((j, j)) / (j ** 2)
t = 0
for k in xrange(repeat):
start = time()
method(f, weight)
t += time() - start
timings[i].append(t / repeat)
for times in timings:
plot(x, times, 'o-')
semilogy()
show()
#subplot(1, 3, 1)
#imshow(a)
#f = zoom(a, .25)
#g = linfilter1(f, ones((5, 5)) / 25)
#subplot(1, 3, 2)
#imshow(f)
#subplot(1, 3, 3)
#imshow(g)
#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