Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

Technical Solutions

How can I improve the performance of the IMREAD function when reading a TIFF-file that has a large number of pages?


Date Last Modified: Friday, July 17, 2009
Solution ID:   1-6OTY4U
Product:   MATLAB
Reported in Release:   No Release
Fixed in Release:   R2009a
Platform:   All Platforms
Operating System:   All OS
 

Subject:

How can I improve the performance of the IMREAD function when reading a TIFF-file that has a large number of pages?

Problem Description:

I am using IMREAD to import multipage TIFF-files, using the:
I = imread(filename, k);
syntax, where "filename" contains the name of the file and "k" is the index of the page I am reading.

I find that this operation takes more time if "k" is large. As a result, importing all the pages of a TIFF-file with many pages, as in:
for k = 1:K
I{k} = imread(filename, k);
end
where "K" is the number of pages in the file, can take a significant amount of time, because of the large amount of time needed to import each page near the end of the file.

Solution:

This issue has been addressed in MATLAB 7.8 (R2009a). The fix requires the use of a new syntax in order to get improved performance:
info = imfinfo(fname);
for k = 1:numel(info)
I{k} = imread(fname, k, ‘Info’, info);
end

The following provides more information on the original cause of the issue and provides a workaround for MATLAB 7.6 (R2008a):

In a TIFF-file, information about the location of each image's data and metadata is stored in that image's Image File Directory (or IFD, a section of data in the file). The files are encoded in such a way that to locate the IFD for a particular image, the file reader must start with the IFD for the first image, use that to find the IFD for the second image, use that to find the IFD for the third image, and so on, until the desired image's IFD is found.

Traversing this linked list to find images far from the beginning of the file can take considerable time. The attached replacement files allow the user to pass additional information to IMREAD, in order to avoid repeatedly traversing the same sections of this list in the process of extracting multiple images from the file, for a net savings of time on large files. Note that these files are will only work with MATLAB 7.6 (R2008a).

Once these files are properly installed (see below), one can get obtain the file information using IMFINFO:
info = imfinfo(filename);
and then pass this information to IMREAD:
I = imread(filename, 'Index', k, 'Info', info);
When this is used to read the entire file, it may take a form similar to:
info = imfinfo(filename);
for k = 1:K
I{k} = imread(filename, 'Index', k, 'Info', info);
end
where "info" is obtained only once, and used repeatedly.

To install these files in MATLAB 7.6 (R2008a), you will need to back up and replace several files in MATLAB's toolbox directories.

1. Find the $MATLABROOT directory, where $MATLABROOT is the MATLAB root directory on your machine, as returned by executing
matlabroot
at the MATLAB command prompt.

2. Find the $MEXEXT file extension, where $MEXEXT is the architecture-specific MEX-file extension, as returned by executing
mexext
at the MATLAB command prompt.

3. Quit MATLAB.

4. Rename the following M-file:


$MATLABROOT\toolbox\matlab\imagesci\imread.m
Rename the file to imread.m.old

5. Download the attached "imread.m" file. Place the new file(s) in the same directory mentioned in step 4.

6. Repeat the analogous process with each of the files:

$MATLABROOT\toolbox\matlab\imagesci\private\imjpginfo.m
$MATLABROOT\toolbox\matlab\imagesci\private\imtifinfo.m
$MATLABROOT\toolbox\matlab\imagesci\private\readtif.m
$MATLABROOT\toolbox\matlab\imagesci\private\rtifc.$MEXEXT
$MATLABROOT\toolbox\matlab\imagesci\private\tifftagsprocess.m
$MATLABROOT\toolbox\matlab\imagesci\private\tifftagsread.$MEXEXT
These replacement files (from the attached "private.zip") will go in the same directory as the old files:

$MATLABROOT\toolbox\matlab\imagesci\private

7. Restart MATLAB

8. After restarting MATLAB, issue the following command at the MATLAB command prompt:
rehash toolboxcache

 

Related Documents/Files:

Please provide feedback to help us improve this Solution
Contact support
E-mail this page
Print this page