生成ZStack of images并使用python创建叠层图像的2D自顶向下视图

2024-09-30 12:15:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有n个相同大小的图像需要沿Z轴堆叠,如图所示创建一种三维模型。我已经尝试过使用Z-Stack和不同的库(比如here)来实现这一点,然后我需要创建一个自顶向下的视图,其中包含将3D模型转换为2D图像的堆叠图像。在

我试过旋转每个切片,并用它制作一个TIFF-Z堆栈,但似乎无法使其工作。enter image description here

n个图像的总数(ABCD),每个图像的大小为H x W像素,将沿着Z轴堆叠,如图所示,创建一个大小为W x n像素的自顶向下视图ABFE。在

ABFE是我想要达到的目标。在

由于我是新来的图像处理,任何帮助(也包括以后的研究术语)将不胜感激!在


Tags: 模型图像视图目标herestack堆栈切片
2条回答

在numpy中,您将使用^{}

import numpy as np

# height, width, and z
x = 10
y = 5
z = 3

# create random 3-d array of shape (x,y,z) to represent images
img1 = np.random.rand(x,y,z)
img2 = np.random.rand(x,y,z)
img3 = np.random.rand(x,y,z)

img1.shape
>>> (10, 5, 3)

# concatenate along the z axis
out_img = np.concatenate([img1, img2, img3], axis=2)
out_img.shape
>>> (10, 5, 9)

这里的axis参数是关键,它告诉numpy连接数组的输入列表的轴(又称维)。维度从0开始,因此第一个维度axis=0表示x轴,在这种情况下,第二个维度{}是y轴,而第三个维度{}是z轴。在

OpenCV中的每个图像都是三维张量,最后一个维度是通道数,通常为3。将它们沿着另一个新的维度叠加需要一个4维张量。在

W, H, n = 300, 200, 5
num_channels = 3 
# shape is (height, width, channels)
im = np.zeros((H, W, num_channels), dtype=np.uint8) # analogue of cv2.imread('test1.jpg')                                                                                                
images = [np.zeros_like(im) for i in range(5)]
# here goes the interesting part
meta_image = np.stack(images)
print(meta_image.shape) # (5, 200, 300, 3) == (n, H, W, num_channels)

# the top most image can be accessed as a slice
top_img = meta_image[:, 0, ...]
cv2.imshow("top", top_img)
# the bottom one
bottom_img = meta_image[:, H-1, ...]
cv2.imshow("bottom", bottom_img)
print(top_img.shape) # (5, 300, 3) == (n, W, num_channels)

# in case you want something like mean over this H slices:
some_mean_slice = np.mean(meta_image, axis=1).astype(np.uint8)
print(some_mean_slice.shape) # (5, 300, 3)

相关问题 更多 >

    热门问题