Matlab only opens first frame of multi-page tiff stack

70 views (last 30 days)
I've created multi-page tiff files with a macro in ImageJ, and I'm now trying to open it using matlab, but I can only access the first frame.
Here is the result of imfinfo(filename). Accordingly, I get
length(imfinfo(filename)) = 1
Filename: [1x129 char]
FileModDate: '28-nov-2013 12:27:51'
FileSize: 6.7905e+09
Format: 'tif'
FormatVersion: []
Width: 512
Height: 512
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: [77 77 0 42]
ByteOrder: 'big-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'Uncompressed'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: 932625
SamplesPerPixel: 1
RowsPerStrip: 512
StripByteCounts: 262144
XResolution: []
YResolution: []
ResolutionUnit: 'None'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 8
ImageDescription: 'ImageJ=1.47q
images=25900
slices=25900
loop=false
However if I open the same tif file in ImageJ, then I can read and scroll through the 25900 frames...The weird thing is that matlab can read previous multipage tiff I had created in imageJ without my batch macro...
I don't understand what's happening...any help would be greatly appreciated ! Thanks, Steven

Accepted Answer

Christoph
Christoph on 28 Apr 2014
Edited: Image Analyst on 17 Oct 2019
ImageJ only writes the first IFD for TIFF files larger than 4GB at the beginning of the file.
The other IFD entries are placed at the end of the TIFF file.
ImageJ is able to read such files because it sees "images=n" in the ImageDescription tag and realizes it needs to open n images.
As a workaround in MATLAB, you can try whether below code works. Note: This code assumes that the TIFF image has Stripped Planar Configuration and contains one strip per image. This might not work for all images but it could provide you a general idea about how to read such files generated by ImageJ using low-level I/O functions.
fileName = 'Mylarge.tif'; % Determine the number of image frames and the offset to the first image
info = imfinfo(fileName)
This may yield something similar to (amongst others)
FileSize: 1.055313188500000e+10
Format: 'tif'
FormatVersion: []
Width: 692
Height: 520
BitDepth: 16
ColorType: 'grayscale'
FormatSignature: [77 77 0 42]
ByteOrder: 'big-endian'
NewSubFileType: 0
BitsPerSample: 16
Compression: 'Uncompressed'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: 3342765
SamplesPerPixel: 1
RowsPerStrip: 520
StripByteCounts: 719680
Now determine the number of frames:
numFramesStr = regexp(info.ImageDescription, 'images=(\d*)', 'tokens');
numFrames = str2double(numFramesStr{1}{1});
% Use low-level File I/O to read the file
fp = fopen(fileName , 'rb');
% The StripOffsets field provides the offset to the first strip. Based on
% the INFO for this file, each image consists of 1 strip.
fseek(fp, info.StripOffsets, 'bof');
% Assume that the image is 16-bit per pixel and is stored in big-endian format.
% Also assume that the images are stored one after the other.
% For instance, read the first 100 frames
framenum=100;
imData=cell(1,framenum);
for cnt = 1:framenum
imData{cnt} = fread(fp, [info.Width info.Height], 'uint16', 0, 'ieee-be')';
end
fclose(fp);
  1 Comment
Ashish Uthama
Ashish Uthama on 27 Sep 2021
Potential explanation for "The weird thing is that matlab can read previous multipage tiff I had created in imageJ without my batch macro...": I believe ImageJ switches to this (non-standard) tiff format when image size goes beyond 4GB, so if your previous images were smaller they would have been standard TIFF files that MATLAB could recognize.

Sign in to comment.

More Answers (1)

Ashish Uthama
Ashish Uthama on 25 Feb 2022
MATLAB R2020b has tiffreadVolume which supports non-BigTIFF volumes, greater than 4GB, created by ImageJ.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by