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

时间:2017-11-22 15:42:03

标签: matlab tiff imagej imagej-hyperstack

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 个答案:

答案 0 :(得分:3)

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

问题在于人们(或者只有一个人?)创建ImageJ-MATLAB在创建从MATLAB到ImageJ的路线方面做得很好,但是他们似乎并不打算选择行为。 ImageJ-MATLAB的IJM.show('name')有很多隐藏或未记录的限制(我大量改变the documentation,所以现在应该更清楚了)。最后,我继续编写一个包装函数

ijmshow

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

示例MATLAB代码

你可以用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');

您可能需要在保存之前设置每个频道的显示范围。

于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 copytoImgPlus对象的ImgPlus

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

答案 1 :(得分: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读取此格式。我建议使用ImageJ的Fiji发行版,它预装了Bio-Formats。

还有this same question posted on MATLAB Central

答案 2 :(得分:1)

使用Matlab' Tiff class

我可以组装一个我觉得很好的ImageDescription标签,ImageJ将它作为5D Hyperstack读取( File> Open ),但数据部分损坏。似乎存在偏移问题(我可以使用Matlab再次读取数据)。

然而,如果读取与TIFF文件的文件>导入>生物格式变得5D(XYCZT)中的ImageJ堆栈如果考虑到与Hyperstack堆被标记在生物格式导入对话框。感谢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数据的唯一完全铺设和可用的方法,尽管在没有太多元数据可用且需要传输的情况下,此解决方案可能是轻量级替代方案。