【发布时间】:2020-09-24 19:16:59
【问题描述】:
我想将几个 2D C 连续图像阵列加载到 3D 卷中,并通过直观的索引访问它们,例如slice3 = volume[:,:,2] 需要对原始串联的一维表示进行一些重塑。
现在,由于我加载了大量图像并从中计算出更多的新卷,这必须在您的普通 PC 上运行,所以我担心内存使用情况以及计算性能。
问:我怎样才能使 3D 体积和 2D 切片保持连续,以便我可以有效地在体积上做一些事情,在单个切片上做一些事情。
这里有一些可以玩的例子:
import numpy as np
# dimensions:
rows = 2
cols = 2
slices = 3
# create array
a = np.arange(rows*cols*slices)
print(a)
# [ 0 1 2 3 4 5 6 7 8 9 10 11]
# this is the original concatenated input of the slices [[0,1],[2,3]], [[4,5],[6,7]], [[8,9],[10,11]]
# a contiguous?
print(a.flags['C_CONTIGUOUS'])
# True
a = a.reshape(rows,cols,slices)
print(a)
# a still contiguous?
print(a.flags['C_CONTIGUOUS'])
# True
# what about a slice?
print(a[:,:,0])
# [[0 3]
# [6 9]]
# ouch! that's not the slice I wanted! I wanted to keep [[0,1],[2,3]] together
# this slice is of course also not contiguous:
print(a[:,:,0].flags['C_CONTIGUOUS'])
# False
# ok, let's start over
a = a.ravel()
print(a)
# [ 0 1 2 3 4 5 6 7 8 9 10 11]
a = a.reshape(slices,rows,cols)
a = a.swapaxes(0,1)
a = a.swapaxes(1,2)
# what about a slice?
print(a[:,:,0])
# [[0 1]
# [2 3]]
# now that's the kind of slice I wanted!
# a still contiguous?
print(a.flags['C_CONTIGUOUS'])
# False
# slice contiguous?
print(a[:,:,0].flags['C_CONTIGUOUS'])
# True
# only halfway there.. :(
再次声明:有没有办法实现所需的切片索引,同时保持体积以及单个切片 C 连续?
【问题讨论】: