簡體   English   中英

Numpy scale 3D陣列

[英]Numpy scale 3D array

我正在嘗試將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)

這樣運行,但填充的尺寸填充了他們需要的兩倍...

看一下放大 scipy.ndimage 在這里,您可以提供沿每個軸的縮放因子,而不是最終的立方體大小,但這很容易理解。

inarr = np.ones((32,32,32))
outarr = ndimage.zoom(inarr, 2)
outarr.shape
(64, 64, 64)

答案是使用pad然后使用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))

在一行上完成所有填充都可以修復任何錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM