[英]how to concatenate numpy array of different shape
我有兩個NumPy
E29F6EF5E1778Z arrays 形狀:(批次,H,W,運河)。 我想將這些 arrays 連接到一個陣列中,但它們具有不同的形狀[209, 450, 450, 24]
和[209, 112, 112]
。
有誰知道該怎么做?
連接不同大小的向量的一種簡單方法是用零填充較小的向量,使其與較大的向量具有相同的形狀。
bigger = np.random.uniform(size=(209, 450, 450, 24)) # should be your input
smaller = np.random.uniform(size=(209, 112, 112))
s = np.zeros(bigger.shape[:-1])
s[:smaller.shape[0], :smaller.shape[1], :smaller.shape[2]] = smaller
s = s[..., np.newaxis] # reshaping to (209, 450, 450, 1)
result = np.concatenate([bigger, s], axis=-1)
但如果它們是圖像,您應該使用圖像庫將圖像調整為正確的形狀。
import PIL.Image
bigger = np.random.uniform(size=(209, 450, 450, 24)) # should be your input
smaller = np.random.uniform(size=(209, 112, 112))
s = np.zeros(bigger.shape[:-1])
for i in range(bigger.shape[0]):
s[i] = np.array(PIL.Image.fromarray(smaller[i]).resize((s.shape[2], s.shape[1])))
s = s[..., np.newaxis] # reshaping to (209, 450, 450, 1)
result = np.concatenate([bigger, s], axis=-1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.