使用python在numpy数组中加载tiff堆栈 [英] Load a tiff stack in a numpy array with python

查看:802
本文介绍了使用python在numpy数组中加载tiff堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hallo Stack Overflow社区,

Hallo Stack Overflow community,

我对.tif文件有点问题。我确信这只是一个小问题,我无法解决(请记住,我是一个相对较新的程序员)。

I am having a little issue with .tif files. I am sure it is only a minor problem that I can´t get around (keep in mind, I am a relatively new programmer).

基本上:我已经准备好了。 tif文件大小为64x64xn(n直到1000)。图像只是包含所有切片的单个文件。我想将图像加载到(多维)numpy数组中。我试过:

Basically: I have prepared .tif files that are 64x64xn in size (n up until 1000). The image is only a single file that contains all of this slices. I would like to load the image into a (multidimensional) numpy array. I have tried:

from PIL import Image as pilimage

file_path=(D:\luca\test\test.tif)
print("The selected stack is a .tif")
dataset = pilimage(file_path)
tiffarray = np.array(dataset)
expim = tiffarray.astype(np.double);
print(expim.shape)

和其他东西(如tifffile)。我似乎只能读取堆栈的第一个切片。 expim是否可以包含tiff堆栈中保存的所有信息?

and other things (like tifffile). I only seem to be able to read the first slice of the stack. Is it possible for "expim" to contain all information that is saved in the tiff stack?

感谢任何帮助!

推荐答案

我不确定是否有办法让PIL打开tiff堆栈的多个切片。

I am not sure if there is a way to get PIL to open multiple slices of a tiff stack.

但是,如果你不一定要使用PIL,那么另一种选择是 scikit-image ,默认情况下会从tiff堆栈中打开多个切片。下面是一些使用scikit-image将tiff堆栈加载到Numpy数组的示例代码:

If you are not bound to using PIL, however, an alternative is scikit-image, which opens multiple slices from a tiff stack by default. Here is some sample code of how to load a tiff stack into a Numpy array using scikit-image:

>>> from skimage import io
>>> im = io.imread('an_image.tif')
>>> print im.shape
(2, 64, 64)

请注意,imread函数加载图像直接进入Numpy数组。此外,生成的数组的尺寸是有序的(z,y,x),其中z表示深度,y表示高度,x表示宽度。因此,要从堆栈中获取单个切片,您只需:

Note that the imread function loads the image directly into a Numpy array. Also, the dimensions of the resulting array are ordered (z, y, x) where z represents the depth, y represents the height, and x represents the width. Thus, to get a single slice from the stack all you have to do is:

>>> print im[1].shape
(64, 64)

这篇关于使用python在numpy数组中加载tiff堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆