从python中的单个tiff创建一个tiff堆栈 [英] Creating a tiff stack from individual tiffs in python

查看:113
本文介绍了从python中的单个tiff创建一个tiff堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据此处找到的示例创建不同大小的tiff堆栈:

这适用于以下代码:

来自skimage import io的

 将numpy导入为np导入操作系统dir ='C:/Users/Mich/Desktop/tiff stack/'列表文件= []对于os.listdir(dir)中的img_files:如果img_files.endswith(.tif"):listfiles.append(img_files)first_image = io.imread(dir + listfiles [0])io.imshow(first_image)first_image.shape堆栈= np.zeros((5,first_image.shape [0],first_image.shape [1]),np.uint8)对于范围(0,5)中的n:stack [n,:,:] = io.imread(dir + listfiles [n])path_results ='C:/Users/Mich/Desktop/'io.imsave(path_results +'Stack.tif',堆栈) 

当我只想堆叠前四个或前三个时,问题就来了.

带有4张tiff图片的示例:

  stack = np.zeros((4,first_image.shape [0],first_image.shape [1]),np.uint8)对于范围(0,4)中的n:stack [n,:,:] = io.imread(dir + listfiles [n]) 

然后我得到了这样的结果:

并尝试堆叠该文件夹的前三张图像时,它们会合并在一起!

  stack = np.zeros((3,first_image.shape [0],first_image.shape [1]),np.uint8)对于范围(0,3)中的n:stack [n,:,:] = io.imread(dir + listfiles [n]) 

我在代码中哪里有错,所以它只是将单个tiff添加到大小为3、4或5的多维堆栈中?

解决方案

指定图像数据的颜色空间( photometric ='minisblack'),否则,tifffile插件将根据形状进行猜测输入数组的大小.

这是直接使用tifffile的较短版本:

  import glob导入tifffile使用tifffile.TiffWriter('Stack.tif')作为堆栈:对于glob.glob('nucleus/*.tif')中的文件名:stack.save(tifffile.imread(文件名),photometric ='minisblack',连续=真) 

I am creating tiff stacks of different sizes based on the example found here: http://www.bioimgtutorials.com/2016/08/03/creating-a-z-stack-in-python/

A sample of the tif files can be downloaded here: nucleus

I have a folder with 5 tiff files inside. I want to stack them to be able to open them in imageJ so that they look like this:

And this works with the following code:

from skimage import io
import numpy as np
import os

dir = 'C:/Users/Mich/Desktop/tiff stack/'

listfiles =[]


for img_files in os.listdir(dir):
    if img_files.endswith(".tif") :
        listfiles.append(img_files)



first_image = io.imread(dir+listfiles[0])

io.imshow(first_image)

first_image.shape

stack = np.zeros((5,first_image.shape[0],first_image.shape[1]),np.uint8)



for n in range(0,5):
    stack[n,:,:]= io.imread(dir+listfiles[n])

path_results = 'C:/Users/Mich/Desktop/'
io.imsave(path_results+'Stack.tif' ,stack)

The problem comes when I just want to stack the 4 first ones or the 3 first ones.

Example with 4 tiff images:

   stack=np.zeros((4,first_image.shape[0],first_image.shape[1]),np.uint8)

   for n in range(0,4):
       stack[n,:,:]= io.imread(dir+listfiles[n])

Then I obtain this kind of result:

and while trying to stack the 3 first images of the folder, they get combined!

   stack=np.zeros((3,first_image.shape[0],first_image.shape[1]),np.uint8)

   for n in range(0,3):
       stack[n,:,:]= io.imread(dir+listfiles[n])

Where am I wrong in the code, so that it dosent just add the individual tiff in a multidimensional stack of the sizes 3, 4 or 5 ?

解决方案

Specify the color space of the image data (photometric='minisblack'), otherwise the tifffile plugin will guess it from the shape of the input array.

This is a shorter version using tifffile directly:

import glob
import tifffile

with tifffile.TiffWriter('Stack.tif') as stack:
    for filename in glob.glob('nucleus/*.tif'):
        stack.save(
            tifffile.imread(filename), 
            photometric='minisblack', 
            contiguous=True
        )

这篇关于从python中的单个tiff创建一个tiff堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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