簡體   English   中英

帶有numpy的2D數組列表

[英]List of 2D Arrays with numpy

我有一個二維ndarry /形狀為(4096,2048)的數組。 我正在嘗試列出此數組的不同部分的列表,這些部分都是40x40。

我試過追加和串聯,但是沒有運氣。 這是我所擁有的:

#img = the 4096x2048 array. 
# I want to store 100 different 40x40 slices in cropped. The first #slice should start at 186, 290

cropped = img[186:226, 290:330]
for i in range(0,100):
    cropped_image = img[a: a+40, b:b+40]
    cropped.append(cropped,cropped_image)
    a += 1
    b += 1
return cropped

您可以使用列表推導來列出這些子數組的列表

import itertools
cropped = [img[i:i+40, j:j+40] for i,j in itertools.product(range(0, 4096, 40), range(0, 2048, 40)]

這會給你一個清單

[img[0:40, 0:40], img[40:80, 0:40], ...
 img[0:40, 40:80], img[40:80, 40:80], ...
 ...]

暫無
暫無

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

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