maximusdooku
maximusdooku

Reputation: 5542

How can I read a bunch of TIFF files faster in Matlab?

I have to read in hundreds of TIFF files, perform some mathematical operation, and output a few things. This is being done for thousands of instances. And the biggest bottleneck is imread. Using PixelRegion, I read in only parts of the file, but it is still very slow.

Currently, the reading part is here.

Can you suggest how I can speed it up?

for m = 1:length(pfile)
    if ~exist(pfile{m}, 'file')
        continue;
    end
    pConus = imread(pfile{m}, 'PixelRegion',{[min(r1),max(r1)],[min(c1),max(c1)]});
    pEvent(:,m) = pConus(tselect);
end

Upvotes: 0

Views: 631

Answers (1)

user9985
user9985

Reputation: 172

General Speedup

  • The pixel region does not appear to change at each iteration. I'm not entirely sure if Matlab will optimize the min and max calls (though I'm pretty sure it won't). If you don't change them at each iteration, move them outside the for loop and calculate them once.

Parfor

The following solution assumes you have access to the parallel computing toolbox. I tested it with 10,840 tiffs, each image was 1000x1000 originally, but I only read in a 300x300 section of them. I am not sure how many big pConus(tselect) is, so I just stored the whole 300x300 image.

P.S. Sorry about the formatting. It refuses to format it as a block of code.

Results based on my 2.3 GHz i7 w/ 16GB of ram

  • for: 130s
  • parfor: 26s + time to start pool

% Setup

clear;clc;
n = 12000;

% Would be faster to preallocate this, but negligeble compared to the
% time it takes imread to complete.
fileNames = {};

for i = 1:n
    name = sprintf('in_%i.tiff', i);
    % I do the exist check here, assuming that the file won't be touched in
    % until the program advances a files lines.
    if exist(name, 'file')
        fileNames{end+1} = name;
    end 
end
rows = [200, 499];
cols = [200, 499];
pics = cell(1, length(fileNames));

tic;
parfor i = 1:length(fileNames)
    % I don't know why using the temp variable is faster, but it is
    temp = imread(fileNames{i}, 'PixelRegion', {rows, cols});
    pics{i} = temp;
end
toc;

Upvotes: 2

Related Questions