-
Notifications
You must be signed in to change notification settings - Fork 0
/
imreadalltiff.m
48 lines (39 loc) · 1.12 KB
/
imreadalltiff.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%-------------------------------------%
% djm works: img = imreadalltiff('dff1_s11010-034_prep_1.tif', 255);
function [A nFrames] = imreadalltiff(filename, nFrames)
% IMREADALLTIFF Utility method to read all frames from a TIFF file.
% read the first frame
tf = imformats('tif');
I = feval(tf.read, filename, 1);
% estimate number of frames from file size
if nargin < 2
D = dir(filename);
wh = whos('I');
nFrames = floor(D.bytes / wh.bytes);
else
nFrames=nFrames;
end
% preallocate output
A = zeros(size(I,1), size(I,2), nFrames, class(I));
% copy in first image
A(:,:,1) = I;
wait_bar = waitbar(0,'Matlab is loading the image stack...');
% try to read all frames
frame = 1;
try
while frame < nFrames
frame = frame + 1;
A(:,:,frame) = feval(tf.read, filename, frame);
waitbar(frame/nFrames, wait_bar);
end
close(wait_bar);
catch
frame = frame - 1;
disp(strcat('Frames read: ', int2str(frame)));
% optional line to trim off any extra frames read
% comment out if not needed
A = A(:,:,1:frame);
close(wait_bar);
end
end
%-------------------------------------%