improc ass2 code cleanup.

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