我有大小为(197,233,189)的3D图像,其中该体积中的每个点都包含灰度值。我有两种不同的图像,左海马和右海马。为了将它们组合起来,我只需要明智地将这些图像元素相加即可。总共每个海马都有44张图像。右海马图像因此存储在包含44个元素的“向量”中,每个元素是(197,233,189)图像。左海马图像也是如此。我总是收到列表分配索引超出范围的错误。
我已经尝试过以下代码
def make_gt(vector_L_hc, vector_R_hc):
GT_hc_segmentation = []
for n in range(len(vector_L_hc)):
for x in range(vector_L_hc[n].shape[0]):
for y in range(vector_L_hc[n].shape[1]):
for z in range(vector_L_hc[n].shape[2]):
GT_hc_segmentation[n][x][y][z] = vector_L_hc[n][x][y][z] + vector_R_hc[n][x][y][z]
return GT_hc_segmentation
IndexError:列表分配索引超出范围
我也尝试了以下方法:
def make_gt(vector_L_hc, vector_R_hc):
GT_hc_segmentation = []
for i in range(len(vector_L_hc)):
GT_hc_segmentation[i] = np.add(vector_L_hc[i], vector_R_hc[i])
return GT_hc_segmentation
这也会产生错误:IndexError:列表分配索引超出范围
vector_L_hc和vector_R_hc的索引0是特定图像,索引1是x坐标,索引2是y坐标,索引3是z坐标。
我想要一个新的向量GT_hc_segmentation,其中一个图像是两个图像的总和,一个来自vector_L_hc,另一个来自vector_R_hc。结果将是列表还是数组?具有与vector_L_hc相同的大小,并且来自vector_R_hc的大小。
对于您的第一种方法,您似乎正在尝试对多维numpy数组使用列表索引。很难说,因为您没有在声明对象的地方显示代码。对于numpy数组,您要像[i,j,k]
而不是[i][j][k]
第二种方法:
def make_gt(vector_L_hc, vector_R_hc):
GT_hc_segmentation = []
for i in range(len(vector_L_hc)):
GT_hc_segmentation[i] = np.add(vector_L_hc[i], vector_R_hc[i])
return GT_hc_segmentation
您没有初始化GT_hc_segmentation。它是空的,因此您无法像在此处尝试那样对其进行索引。您要么需要使用长度来初始化它,要么可以像这样动态地向其中添加元素:
def make_gt(vector_L_hc, vector_R_hc):
GT_hc_segmentation = []
for i in range(len(vector_L_hc)):
GT_hc_segmentation.append(np.add(vector_L_hc[i], vector_R_hc[i]))
return GT_hc_segmentation