Commit 8b011f3a authored by Taddeüs Kroes's avatar Taddeüs Kroes

Improc ass2: Added interpolation part of assignment.

parent 766203c1
from math import floor, ceil
bgcolor = 0
def inImage(image, x, y):
return x >= 0 and y >= 0 and x < image.shape[0] and y < image.shape[1]
def pV(image, x, y, method):
global bgcolor
if inImage(image, x, y):
if method == 'nearest':
return image[round(x)][round(y)]
elif method == 'linear':
x1 = floor(x)
x2 = ceil(x)
y1 = floor(y)
y2 = ceil(y)
return image[x1][y1] * (x2 - x) * (y2 - y) \
+ image[x2][y1] * (x - x1) * (y2 - y) \
+ image[x1][y2] * (x2 - x) * (y - y1) \
+ image[x2][y2] * (x - x1) * (y - y1)
else:
print 'Interpolation method "%s" is not supported' % method
else:
return bgcolor
if __name__ == '__main__':
from pylab import linspace, show, imread, plot, array
"""Profile of an image along line in n points."""
def profile(image, x0, y0, x1, y1, n, method):
return array([pV(image, x, y, method) for x, y in zip(linspace(x0, \
x1, n), linspace(y0, y1, n))])
a = imread('cameraman.png')
for method in ['nearest', 'linear']:
plot(profile(a, 100, 100, 120, 120, 20, method))
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