-
Notifications
You must be signed in to change notification settings - Fork 3
/
padorcut.py
125 lines (86 loc) · 3.76 KB
/
padorcut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 9 13:59:52 2016
@author: francesco
"""
import math
import numpy as np
# new implementation, working on a generic number of axes
def padorcut(arrayin, newSize, axis = None):
nDims = arrayin.ndim
# extend dimensions
while nDims < len(newSize):
arrayin = np.expand_dims(arrayin, nDims)
nDims = arrayin.ndim
if type(axis) is int:
# check if newsz is iterable, otherwise assume it's a number
try:
newSz = newSize[axis]
except:
newSz = int(newSize)
oldSz = arrayin.shape[axis]
if oldSz < newSz:
padBefore = int(math.floor(float(newSz - oldSz)/2))
padAfter = int(math.ceil(float(newSz - oldSz)/2))
padding = []
for i in range(nDims):
if i == axis:
padding.append( (padBefore, padAfter) )
else:
padding.append( (0,0) )
return np.pad(arrayin, padding, 'constant')
elif oldSz > newSz:
cutBefore = int(math.floor(float(oldSz - newSz)/2))
cutAfter = int(math.ceil(float(oldSz - newSz)/2))
slc = [slice(None)]*nDims
slc[axis] = slice(cutBefore, -cutAfter)
return arrayin[tuple(slc)]
else:
return arrayin
else:
for ax in range(nDims):
arrayin = padorcut(arrayin, newSize, ax)
return arrayin
def padorcut_old(arrayin, newsize):
# function arrayout = padorcut(arrayin, newsize)
# Symmetrically pads or cuts an array (2D!!) so that its final size is newsize
oldsize = arrayin.shape
if oldsize[0] > newsize[0]:
# we have to cut
cutbefore = int(math.floor( float(oldsize[0] - newsize[0] ) / 2 ))
cutafter = int(math.ceil( float( oldsize[0] - newsize[0] ) / 2 ))
arraytemp = arrayin[cutbefore:(oldsize[0]-cutafter), :]
elif oldsize[0] < newsize[0]:
# we have to pad
padbefore = int(math.floor( float( newsize[0] - oldsize[0] ) / 2))
padafter = int(math.ceil( float( newsize[0] - oldsize[0] ) / 2))
arraytemp=np.pad(arrayin, ( (padbefore, padafter), (0,0) ), 'constant')
else: # oldsize[0] == newsize[0]
arraytemp=arrayin;
# second direction
if oldsize[1] > newsize[1]:
# we have to cut
cutbefore = int(math.floor( float( oldsize[1] - newsize[1] ) / 2 ))
cutafter = int(math.ceil( float( oldsize[1] - newsize[1] ) / 2 ))
arrayout = arraytemp[:, cutbefore:(oldsize[1]-cutafter)];
elif oldsize[1] < newsize[1]:
# we have to pad
padbefore = int(math.floor( float( newsize[1] - oldsize[1] ) / 2))
padafter = int(math.ceil( float( newsize[1] - oldsize[1] ) / 2))
arrayout=np.pad(arraytemp, ( (0,0), (padbefore, padafter) ), 'constant')
else: # oldsize[1] == newsize[1]
arrayout=arraytemp;
return arrayout
def translate(arrayin, translation):
padBeforeX = translation[0] if translation[0] > 0 else 0
padAfterX = -translation[0] if translation[0] < 0 else 0
padBeforeY = translation[1] if translation[1] > 0 else 0
padAfterY = -translation[1] if translation[1] < 0 else 0
cutBeforeX = padAfterX
cutAfterX = padBeforeX
cutBeforeY = padAfterY
cutAfterY = padBeforeY
nx, ny = arrayin.shape
cutArray = arrayin[cutBeforeX:(nx-cutAfterX), cutBeforeY:(ny-cutAfterY)]
arrayout = arrayout=np.pad(cutArray, ( (padBeforeX,padAfterX), (padBeforeY, padAfterY) ), 'constant')
return arrayout