How can I save an image with four channels (or more) into an ImageJ-compatible TIFF format?
Show older comments
I have four channels image data and want to save it to TIFF format that is compatible with ImageJ/Fiji.
Standard imwrite does not support TIFF format with more than three channels. In fact, I wanted to save it into jpeg 2000, in which the original data was stored, but it looks like impossible.
So I'm playing around with Tiff class. It has lots of Tags to be specified, and ....
The following can save a file, but it seems incompatible with ImageJ. Image data in channel 1 repeatedly appear in all the four channels.
% I contains image data
% size(I) == [m,n,4]
tif = Tiff(filename,'w');
tif.setTag('ImageWidth',size(I,2));
tif.setTag('ImageLength',size(I,1));
tif.setTag('Photometric',Tiff.Photometric.MinIsBlack);
%NOTE Photometric must be before BitsPerSample and Compression
tif.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Separate);
%NOTE PlanarConfiguration must be Separate for Fiji to open the file properly
tif.setTag('BitsPerSample',16);
tif.setTag('SamplesPerPixel',4);
tif.setTag('Compression',Tiff.Compression.LZW);
tif.write(I);
tif.close();
Is there a way to save images with 4+ channels, other than Bio-Formats's bfsave, which is really slow?
Accepted Answer
More Answers (1)
Martin Privat
on 25 May 2020
Fiji uses a specific 'ImageDescription' Tiff field to know how the data is organized. This field is also used to save a normal stack or a hyperstack. You can just store the images one after the other as a multipage tiff, and specify the right ImageDescription field to tell Fiji how to organize the tiff pages.
To save a 16 bits multidimensional image with 4 chanels, 5 Z slices and 6 time points, one could do something like:
MultiDimImg = zeros(300,400,4,5,6,'uint16');
fiji_descr = ['ImageJ=1.52p' newline ...
'images=' num2str(size(MultiDimImg,3)*...
size(MultiDimImg,4)*...
size(MultiDimImg,5)) newline...
'channels=' num2str(size(MultiDimImg,3)) newline...
'slices=' num2str(size(MultiDimImg,4)) newline...
'frames=' num2str(size(MultiDimImg,5)) newline...
'hyperstack=true' newline...
'mode=grayscale' newline...
'loop=false' newline...
'min=0.0' newline...
'max=65535.0']; % change this to 256 if you use an 8bit image
t = Tiff('test.tif','w')
tagstruct.ImageLength = size(MultiDimImg,1);
tagstruct.ImageWidth = size(MultiDimImg,2);
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 16;
tagstruct.SamplesPerPixel = 1;
tagstruct.Compression = Tiff.Compression.LZW;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.SampleFormat = Tiff.SampleFormat.UInt;
tagstruct.ImageDescription = fiji_descr;
for frame = 1:size(MultiDimImg,5)
for slice = 1:size(MultiDimImg,4)
for channel = 1:size(MultiDimImg,3)
t.setTag(tagstruct)
t.write(im2uint16(MultiDimImg(:,:,channel,slice,frame)));
t.writeDirectory(); % saves a new page in the tiff file
end
end
end
t.close()
In my experience this is faster than bfsave
2 Comments
Andrew Harrison
on 16 Dec 2020
I'm using this and it works well but how do I modify it to save as RGB?
Andrew Harrison
on 16 Dec 2020
I changed,
'mode=grayscale'
to
'mode=RGB color'
Confusion with just using RGB in some instances and the american/english spellings.
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!