Removing code repetition

This commit is contained in:
Ankit Agrawal
2013-07-04 17:24:13 +08:00
parent 19f0be9fbd
commit 8372b0b6aa
+13 -20
View File
@@ -10,21 +10,17 @@ def downsample(image, factors, method='sum'):
f1 = factors[1]
if (f0 - int(f0) != 0) or (f1 - int(f1) != 0):
print "Use resample for non-integer downsampling"
print "Use resample() for non-integer downsampling"
return
cropped = image[: is0 - (is0 % f0), : is1 - (is1 % f1)]
cropped = image[:is0 - (is0 % f0), :is1 - (is1 % f1)]
out = np.zeros((cropped.shape[0] / f0, cropped.shape[1] / f1))
for i in range(cropped.shape[0]):
for j in range(cropped.shape[1]):
out[int(i / f0)][int(j / f1)] += cropped[i][j]
if method == 'sum':
for i in range(cropped.shape[0]):
for j in range(cropped.shape[1]):
out[int(i / f0)][int(j / f1)] += cropped[i][j]
return out
if method == 'mean':
for i in range(cropped.shape[0]):
for j in range(cropped.shape[1]):
out[int(i / f0)][int(j / f1)] += cropped[i][j]
else:
return out / float(f0 * f1)
@@ -36,18 +32,15 @@ def upsample(image, factors, method='divide'):
f1 = factors[1]
if (f0 - int(f0) != 0) or (f1 - int(f1) != 0):
print "Use resample for non-integer upsampling"
print "Use resample() for non-integer upsampling"
return
out = np.zeros((f0 * image.shape[0], f1 * image.shape[1]))
if method == 'divide':
for i in range(out.shape[0]):
for j in range(out.shape[1]):
out[i][j] = (image[i / f0][j / f1])
return out / float(f0 * f1)
if method == 'uniform':
for i in range(out.shape[0]):
for j in range(out.shape[1]):
out[i][j] = (image[i / f0][j / f1])
for i in range(out.shape[0]):
for j in range(out.shape[1]):
out[i][j] = (image[i / f0][j / f1])
if method == 'divide':
return out / float(f0 * f1)
else:
return out