首页 文章

如何从Matlab生成有效的HyperStack ImageJ数据文件?

提问于
浏览
2

ImageJ HyperStacks具有数据类型(8-32位),宽度,高度,多个通道,多个切片和多个帧 . 它们代表5D(XYCZT)数据集 . ImageJ将它们存储为多页tiff文件,其中通道数乘以切片数乘以2D(XY)图像帧数 . 第一个图像似乎有两个ID为50838和50839的自定义标签 .

我想创建包含来自Matlab的5D数据的tif文件,ImageJ可以将其读取为有效的5D HyperStack .

我可以使用 imwrite(matrix, file, 'WriteMode','append') 在Matlab中将多个2D图像存储在多页tiff文件中,但ImageJ将仅将其读取为3D(XYZ)图像堆栈 . 不包含有关通道,切片和帧的信息 .

我想我可以查看ImageJ源代码,找出它们存储这些缺失信息的位置,然后使用Matlab的LibTIFF包装器重新创建ImageJ的元信息 . 但是,如果您已经知道该做什么或者有替代方案,我想听听 .

3 回答

  • 2

    ImageJ使用TIFF第一个IFD的ImageDescription标签来存储其元信息 . 以下是有丝分裂样本数据集(文件>打开样本>有丝分裂)的示例:

    ImageJ=1.51d
    images=510
    channels=2
    slices=5
    frames=51
    hyperstack=true
    mode=composite
    unit=\u00B5m
    finterval=0.14285714285714285
    loop=false
    min=1582.0
    max=6440.0
    

    MATLAB的Tiff类可能会为您提供细粒度的控制,以便将自定义ImageDescription写入第一个IFD;我不确定 .

    可能更容易使用Bio-Formats库的bfsave.m function来编写OME-TIFF,它可以是5维的; ImageJ可以通过Bio-Formats plugin读取此格式 . 我建议使用预装了Bio-Formats的ImageJ的Fiji发行版 .

    还有this same question posted on MATLAB Central .

  • 1

    我还认为Bio-Fomats是唯一的方法 . 但后来我意识到斐济的ImageJ-MATLAB允许你将MATLAB数据传递给ImageJ . 然后,您可以通过ImageJ保存图像,因此ImageJ可以打开它 .

    问题是人们(或者只是一个人?)创建的ImageJ-MATLAB在创建从MATLAB到ImageJ的路径方面做得很好,但是他们没有很多隐藏或未记录的限制(我大量改变了the documentation,所以现在应该更清楚了) . 最后,我忙着编写一个包装函数

    ijmshow

    https://github.com/kouichi-c-nakamura/ijmshow

    Example MATLAB code

    你可以用4行做到这一点 .

    addpath '/Applications/Fiji.app/scripts' % depends your Fiji installation
    ImageJ
    
    imp = ijmshow(I,'YXCZT') % I is 5D array of uint8 or uint16
    % the second argument determines which dimension represents what
    % imp is a 5D hyperstack
    
    ij.IJ.saveAsTiff(imp, 'image1.tif');
    

    您可能希望在保存之前设置每个 Channels 的显示范围 .

    已于5/7/2018添加

    copytoImagePlus 提供了比上面的 ijmshow 更好的解决方案(您可以使用相同的语法),因为 copytoImagePlus 不再依赖于基础工作区中的 IJM 变量 .

    https://github.com/kouichi-c-nakamura/copytoImagePlus

    addpath '/Applications/Fiji.app/scripts' % depends your Fiji installation
    ImageJ
    
    imp = copytoImagePlus(I,'YXCZT') % I is 5D array of uint8 or uint16
    % the second argument determines which dimension represents what
    % imp is a 5D hyperstack
    
    ij.IJ.saveAsTiff(imp, 'image1.tif');
    

    另请参见创建ImageJ2 ImgPlus 对象的 copytoImgPlus .

    https://github.com/fiji/fiji/blob/master/scripts/copytoImgPlus.m

  • 2

    使用Matlab的Tiff class .

    我可以组装一个我认为看起来很好的ImageDescription标签,ImageJ将其作为5D Hyperstack读取(文件>打开),但数据部分损坏 . 似乎存在偏移问题(我可以使用Matlab再次读取数据) .

    但是,如果使用File> Import> Bio-Formats读取tiff文件,如果在Bio-Formats导入对话框中标记了具有Hyperstack的视图堆栈,则它将成为ImageJ中的5D(XYCZT)堆栈 . 感谢ctrueden的有用评论 .

    d = ones(100, 200, 10, 2, 3, 'single') * 3.765;
    hyperstack_write('test.tif', d);
    
    function hyperstack_write(file, hyperstack)
    % Writes an (up to) 5D stack into a (XYCZT) HyperStack Tiff file
    % readable by ImageJ
    %
    % hyperstack must have class single
    
    % simple checks
    assert(nargin == 2, 'Not enough arguments');
    assert(isa(hyperstack, 'single'), 'hyperstack must be single');
    
    % get all five dimensions
    d = zeros(5, 1);
    for i = 1 : 5
        d(i) = size(hyperstack, i);
    end
    
    % assemble image description
    s = sprintf('ImageJ=1.51\nnimages=%d\nchannels=%d\nslices=%d\nframes=%d\nhyperstack=true\nmode=color\nloop=false\nmin=%.1f\nmax=%.1f\n', prod(d(3:5)), d(3), d(4), d(5), floor(min(hyperstack(:))*10)/10, ceil(max(hyperstack(:))*10)/10);
    
    % open tif file for writing and set file tags
    t = Tiff(file, 'w');
    
    ts.ImageLength = d(1);
    ts.ImageWidth = d(2);
    ts.Photometric = Tiff.Photometric.MinIsBlack;
    ts.Compression = Tiff.Compression.None;
    ts.BitsPerSample = 32;
    ts.SamplesPerPixel = 1;
    ts.SampleFormat = Tiff.SampleFormat.IEEEFP;
    ts.RowsPerStrip = 5;
    ts.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
    ts.Software = 'MATLAB';
    ts.ImageDescription = s;
    
    % loop over dimensions 3, 4, and 5
    for k = 1 : d(5)
        for j = 1 : d(4)
            for i = 1 : d(3)
                frame = hyperstack(:, :, i, j, k);
                t.setTag(ts)            
                t.write(frame);
                t.writeDirectory();
            end
        end
    end
    
    % close tif file
    t.close();
    
    end
    

    似乎Bio-Formats是从Matlab到ImageJ导出> 2D数据的唯一完全铺设和可用的方法,尽管这里的解决方案在没有太多元数据可用且需要传输的情况下可以是轻量级替代方案 .

相关问题