I'm trying to scale a 3D array to size 64x64x64 (from a larger, non cube size), keeping aspect ratio.
I've done the same thing in a 2D array like this:
pad = Input.size[1]-Input.size[0]
padLeft = math.ceil(pad/2)
padRight = math.floor(pad/2)
if(pad > 0):
paddedInput = np.pad(Input, ((0,0), (padLeft,padRight)), 'constant', constant_values=(0,0))
else:
paddedInput = np.pad(Input, ((math.fabs(padLeft),math.fabs(padRight)), (0,0)), 'constant', constant_values=(0,0))
Output = misc.imresize(paddedInput,(InputHeight,InputHeight))
Is there a way to achieve the same thing in N (=3) dimensions?
Edit: My attempt at conversion to 3D:
pad = np.zeros((3,1))
pad[0,0] = max(Input.shape) - Input.shape[0]
pad[1,0] = max(Input.shape) - Input.shape[1]
pad[2,0] = max(Input.shape) - Input.shape[2]
paddedInput = np.zeros((max(Input.shape),max(Input.shape),max(Input.shape)))
print(paddedInput.shape)
for dimension in range(0,3):
padLeft = math.ceil(pad[dimension,0]/2)
padRight = math.floor(pad[dimension,0]/2)
if((padLeft > 0) or (padRight > 0)):
if dimension == 0:
paddedInput = np.pad(Input, ((padLeft,padRight),(0,0),(0,0)), 'constant', constant_values=0)
elif dimension == 1:
paddedInput = np.pad(paddedInput, ((0,0), (padLeft,padRight),(0,0)), 'constant', constant_values=0)
elif dimension == 2:
paddedInput = np.pad(paddedInput, ((0,0),(0,0), (padLeft,padRight)), 'constant', constant_values=0)
print(paddedInput.shape)
This runs but the dimensions that get padded are padded with double the amount they need to be...
The answer is to use pad and then numpy.ndarray.resize() :
pad = np.zeros((3,1))
pad[0,0] = max(Input.shape) - Input.shape[0]
pad[1,0] = max(Input.shape) - Input.shape[1]
pad[2,0] = max(Input.shape) - Input.shape[2]
paddedInput = np.zeros((max(Input.shape),max(Input.shape),max(Input.shape)))
paddedInput = np.pad(Input, ((int(math.ceil(pad[0,0]/2)),
int(math.floor(pad[0,0]/2))),(int(math.ceil(pad[1,0]/2)),
int(math.floor(pad[1,0]/2))),(int(math.ceil(pad[2,0]/2)),
int(math.floor(pad[2,0]/2)))), 'constant', constant_values=0)
paddedInput.resize((64,64,64))
Doing the pad all on one line fixes any errors there were.