4

縦横比を維持しながら、3D 配列を 64x64x64 のサイズ (より大きな非立方体サイズから) にスケーリングしようとしています。

私はこのような2D配列で同じことをしました:

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)

これは実行されますが、パディングされるディメンションは、必要な量の 2 倍でパディングされます...

4

2 に答える 2

2

答えは、パッドを使用してから 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))

パッドをすべて 1 行で実行すると、エラーが修正されます。

于 2015-03-24T12:53:12.147 に答える