What is the fastest way to load multiple image tiff file in matlab?
I have a multiple image tiff file (3000 frames for example) and want to load the each image into matlab (I am using 2010a now). But I found it takes longer time to read images as the index of the frame increasing. The following is the code I am using now
for i=1:no_frame;
IM=imread('movie.tif',i);
IM=double(IM);
Movie{i}=IM;
end
Is there any other way to do i开发者_如何学编程t faster?
The TIFF-specific syntax list for IMREAD says the following for the 'Info'
parameter:
When reading images from a multi-image TIFF file, passing the output of
imfinfo
as the value of the'Info'
argument helpsimread
locate the images in the file more quickly.
Combined with the preallocation of the cell array suggested by Jonas, this should speed things up for you:
fileName = 'movie.tif';
tiffInfo = imfinfo(fileName); %# Get the TIFF file information
no_frame = numel(tiffInfo); %# Get the number of images in the file
Movie = cell(no_frame,1); %# Preallocate the cell array
for iFrame = 1:no_frame
Movie{iFrame} = double(imread(fileName,'Index',iFrame,'Info',tiffInfo));
end
You may want to preassign the array Movie
(or use R2011a, where growing an array inside a loop is less of an issue)
Movie = cell(no_frame,1);
for i=1:no_frame;
IM=imread('movie.tif',i);
IM=double(IM);
Movie{i}=IM;
end
I had the same problem and found this using that I tried this code. I get different time than he did but it is still much better than other image formats. For the last method to work you should find tifflib.mexa64 in your matlab directory and copy it into your working directory.
FileTif='myfile.tif';
InfoImage=imfinfo(FileTif);
mImage=InfoImage(1).Width;
nImage=InfoImage(1).Height;
NumberImages=length(InfoImage);
t=zeros(1,1000);
FinalImage=zeros(nImage,mImage,3,NumberImages,'uint8');
for i=1:NumberImages
tic
FinalImage(:,:,:,i)=imread(FileTif,'Index',i);
t(i)=toc;
end
%disp(sprintf('test1 timing result:\n\t\t%d',toc));
mean(t)
clear
%%
FileTif='myfile.tif';
InfoImage=imfinfo(FileTif);
mImage=InfoImage(1).Width;
nImage=InfoImage(1).Height;
NumberImages=length(InfoImage);
FinalImage=zeros(nImage,mImage,3,NumberImages,'uint8');
t=zeros(1,1000);
for i=1:NumberImages
tic
FinalImage(:,:,:,i)=imread(FileTif,'Index',i,'Info',InfoImage);
t(i) = toc;
end
%disp(sprintf('test2 timing result:\n\t\t%d',toc));
mean(t)
clear
%%
FileTif='myfile.tif';
InfoImage=imfinfo(FileTif);
mImage=InfoImage(1).Width;
nImage=InfoImage(1).Height;
NumberImages=length(InfoImage);
FinalImage=zeros(nImage,mImage,3,NumberImages,'uint8');
t=zeros(1,1000);
TifLink = Tiff(FileTif, 'r');
for i=1:NumberImages
tic
TifLink.setDirectory(i);
FinalImage(:,:,:,i)=TifLink.read();
t(i) = toc;
end
TifLink.close();
%disp(sprintf('test3 timing result:\n\t\t%d',toc));
mean(t)
clear
%%
FileTif='myfile.tif';
InfoImage=imfinfo(FileTif);
mImage=InfoImage(1).Width;
nImage=InfoImage(1).Height;
NumberImages=length(InfoImage);
FinalImage=zeros(nImage,mImage,3,NumberImages,'uint8');
FileID = tifflib('open',FileTif,'r');
rps = tifflib('getField',FileID,Tiff.TagID.RowsPerStrip);
t=zeros(1,1000);
for i=1:NumberImages
tic
tifflib('setDirectory',FileID,i);
% Go through each strip of data.
rps = min(rps,nImage);
for r = 1:rps:nImage
row_inds = r:min(mImage,r+rps-1);
stripNum = tifflib('computeStrip',FileID,r);
FinalImage(row_inds,:,:,i) = tifflib('readEncodedStrip',FileID,stripNum);
end
t(i)=toc;
end
mean(t)
tifflib('close',FileID);
I have written a Matlab
class to efficiently and quickly read TIFF stacks.
Load multi-page TIFF stacks into Matlab fast.
or from the file exchange:
Fast lazy-loading of TIFF stacks.
I have found that it is much faster to avoid using imread. Try this:
function data = FastTiff(filename)
warning('off','all') % Suppress all the tiff warnings
tstack = Tiff(filename);
[I,J] = size(tstack.read());
K = length(imfinfo(filename));
data = zeros(I,J,K);
data(:,:,1) = tstack.read();
for n = 2:K
tstack.nextDirectory()
data(:,:,n) = tstack.read();
end
warning('on','all')
end
精彩评论