切片 3D 图像以创建 2D 图像
Slicing a 3D image to create a 2D image
我有几张形状为 (32,32,32) 的 3D 图像,我想根据它们创建 2D 图像。我想通过获取 z 轴上的每个切片并将每个切片按顺序放入方形数组中来做到这一点,如下所示:
因为我希望 2D 图像是方形的,所以我需要用零填充缺失的切片(示例中为黑色)。
这是我所做的:
# I created an array of the desired dimensions
grid = np.zeros((6*32,6*32))
# Then, I assigned to each section of the grid the values of every slice of the 3d_image:
grid[0:32, 0:32] = 3d_image[:,:,0]
grid[0:32, 32:64] = 3d_image[:,:,1]
grid[0:32, 64:96] = 3d_image[:,:,2]
grid[0:32, 96:128] = 3d_image[:,:,3]
grid[0:32, 128:160] = 3d_image[:,:,4]
grid[0:32, 160:192] = 3d_image[:,:,5]
grid[32:64, 0:32] = 3d_image[:,:,6]
grid[32:64, 32:64] = 3d_image[:,:,7]
grid[32:64, 64:96] = 3d_image[:,:,8]
grid[32:64, 96:128] = 3d_image[:,:,9]
grid[32:64, 128:160] = 3d_image[:,:,10]
grid[32:64, 160:192] = 3d_image[:,:,11]
grid[64:96, 0:32] = 3d_image[:,:,12]
grid[64:96, 32:64] = 3d_image[:,:,13]
...
grid[160:192, 160:192] = 3d_image[:,:,31]
成功了!!但我想自动化它,所以我试了这个:
d = [0, 32, 64, 96, 128, 160]
for j in range(6):
for i in d:
grid[0:32, i:i+32] = 3d_image[:,:,j]
但它没有用,3d_image (j) 的切片索引没有改变,我不知道如何在每第 6 个切片后更改网格的索引范围。
你能帮帮我吗?
这是一种自动执行此操作的方法。假设您的形状为 (32, 32, 32)
的数组称为 n
。请注意,此方法依赖于具有相同大小的所有 3 个维度。
num_layers = n.shape[0]
# num_across = how many images will go in 1 row or column in the final array.
num_across = int(np.ceil(np.sqrt(num_layers)))
# new_shape = how many numbers go in a row in the final array.
new_shape = num_across * num_layers
final_im = np.zeros((new_shape**2)).reshape(new_shape, new_shape)
for i in range(num_layers):
# Get what number row and column the image goes in (e.g. in the example,
# the image labelled 28 is in the 4th (3rd with 0-indexing) column and 5th
# (4th with 0-indexing) row.
col_num = i % num_across
row_num = i // num_across
# Put the image in the appropriate part of the final image.
final_im[row_num*num_layers:row_num*num_layers + num_layers, col_num*num_layers:col_num*num_layers + num_layers] = n[i]
final_im
现在包含您想要的内容。下面是一个表示,其中每个图像都是不同的颜色,“黑色”区域是紫色的,因为 matplotlib 颜色映射很有趣:
无论如何,您可以看出图像到达了它们应该到达的位置,并且您在底部得到了空的 space。
假设 img
是形状为 (32,32,32) 的数组,这应该有效:
N = 32
a = np.vstack([img, np.zeros((4, N, N), dtype=img.dtype)])
grid = a.transpose(1, 0, 2).reshape(N, -1, 6*N).transpose(1, 0, 2).reshape(6*N, -1)
我有几张形状为 (32,32,32) 的 3D 图像,我想根据它们创建 2D 图像。我想通过获取 z 轴上的每个切片并将每个切片按顺序放入方形数组中来做到这一点,如下所示:
因为我希望 2D 图像是方形的,所以我需要用零填充缺失的切片(示例中为黑色)。
这是我所做的:
# I created an array of the desired dimensions
grid = np.zeros((6*32,6*32))
# Then, I assigned to each section of the grid the values of every slice of the 3d_image:
grid[0:32, 0:32] = 3d_image[:,:,0]
grid[0:32, 32:64] = 3d_image[:,:,1]
grid[0:32, 64:96] = 3d_image[:,:,2]
grid[0:32, 96:128] = 3d_image[:,:,3]
grid[0:32, 128:160] = 3d_image[:,:,4]
grid[0:32, 160:192] = 3d_image[:,:,5]
grid[32:64, 0:32] = 3d_image[:,:,6]
grid[32:64, 32:64] = 3d_image[:,:,7]
grid[32:64, 64:96] = 3d_image[:,:,8]
grid[32:64, 96:128] = 3d_image[:,:,9]
grid[32:64, 128:160] = 3d_image[:,:,10]
grid[32:64, 160:192] = 3d_image[:,:,11]
grid[64:96, 0:32] = 3d_image[:,:,12]
grid[64:96, 32:64] = 3d_image[:,:,13]
...
grid[160:192, 160:192] = 3d_image[:,:,31]
成功了!!但我想自动化它,所以我试了这个:
d = [0, 32, 64, 96, 128, 160]
for j in range(6):
for i in d:
grid[0:32, i:i+32] = 3d_image[:,:,j]
但它没有用,3d_image (j) 的切片索引没有改变,我不知道如何在每第 6 个切片后更改网格的索引范围。
你能帮帮我吗?
这是一种自动执行此操作的方法。假设您的形状为 (32, 32, 32)
的数组称为 n
。请注意,此方法依赖于具有相同大小的所有 3 个维度。
num_layers = n.shape[0]
# num_across = how many images will go in 1 row or column in the final array.
num_across = int(np.ceil(np.sqrt(num_layers)))
# new_shape = how many numbers go in a row in the final array.
new_shape = num_across * num_layers
final_im = np.zeros((new_shape**2)).reshape(new_shape, new_shape)
for i in range(num_layers):
# Get what number row and column the image goes in (e.g. in the example,
# the image labelled 28 is in the 4th (3rd with 0-indexing) column and 5th
# (4th with 0-indexing) row.
col_num = i % num_across
row_num = i // num_across
# Put the image in the appropriate part of the final image.
final_im[row_num*num_layers:row_num*num_layers + num_layers, col_num*num_layers:col_num*num_layers + num_layers] = n[i]
final_im
现在包含您想要的内容。下面是一个表示,其中每个图像都是不同的颜色,“黑色”区域是紫色的,因为 matplotlib 颜色映射很有趣:
无论如何,您可以看出图像到达了它们应该到达的位置,并且您在底部得到了空的 space。
假设 img
是形状为 (32,32,32) 的数组,这应该有效:
N = 32
a = np.vstack([img, np.zeros((4, N, N), dtype=img.dtype)])
grid = a.transpose(1, 0, 2).reshape(N, -1, 6*N).transpose(1, 0, 2).reshape(6*N, -1)