我想以"pythonic"方式将3D numpy数组拆分为3D块.我正在处理有些大型数组(1000X1200X1600)的图像序列,所以我需要将它们分成几块来进行处理.
我已经写了这样做的函数,但是我想知道是否有一种本地的numpy方法来实现这一点 - numpy.split似乎没有做我想要的3D数组(但也许我不理解它的功能)
要明确:下面的代码完成了我的任务,但我正在寻求一种更快的方法来完成它.
def make_blocks(x,t):
#x should be a yXmXn matrix, and t should even divides m,n
#returns a list of 3D blocks of size yXtXt
down = range(0,x.shape[1],t)
across = range(0,x.shape[2],t)
reshaped = []
for d in down:
for a in across:
reshaped.append(x[:,d:d+t,a:a+t])
return reshaped
def unmake_blocks(x,d,m,n):
#this takes a list of matrix blocks of size dXd that is m*n/d^2 long
#returns a 2D array of size mXn
rows = []
for i in range(0,int(m/d)):
rows.append(np.hstack(x[i*int(n/d):(i+1)*int(n/d)])) …Run Code Online (Sandbox Code Playgroud)