我正在尝试将 3D 数组缩放到 64x64x64 大小(从更大的非立方体大小),保持纵横比。
我在这样的二维数组中做了同样的事情:
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))
有没有办法在 N (=3) 维度上实现相同的目标?
编辑:我尝试转换为 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)
这会运行,但是填充的尺寸是它们需要填充的两倍...