Advice for Speeding Up TIFF concatenation

2 views (last 30 days)
Saluatation: Hey guys! I'm new to Matlab and working with someone else's code- thought I'd ask for some crowd-sourced help.
Script Function: I work in a neuro lab that records pictures of mouse brains. The raw files that the cameras record to record pictures from alternating cameras (picture of brain, picture of mouse head, picture of brain, ...). We're converting these files to TIFF, deinterleaving them, and then concatenating the files.
Problem: The concatenation is far too slow to be of any use. I'm not yet learned enough in Matlab to be able to troubleshoot. Suggestions?
Code:
%convert micam raw to tiff using image j
javaaddpath 'C:\Program Files\MATLAB\R2013a\java\mij.jar';
javaaddpath 'C:\Program Files\MATLAB\R2013a\java\ij.jar';
MIJ.start('C:\users\lee\desktop\imagej');
MIJ.run('Install...', 'install=[C:\\Users\\lee\\Desktop\\ImageJ\\macros\\Matthias\\Helmchen Macros modified djm.ijm]');
MIJ.run('Run...', 'path=[C:\\Users\\lee\\Desktop\\ImageJ\\macros\\Matthias\\Helmchen Macros modified djm.ijm]');
MIJ.run('Convert MiCam Raw to TIFF');
pause (30); %to prevent race condition, needs to be fixed to wait for input
MIJ.exit;
%prepare tiff stack folder fast
myFolder = uigetdir;
cd(myFolder);
filePattern = fullfile(myFolder, '*.tif');
tifffiles = dir(filePattern);
count = length(tifffiles);
for z = 1:count
A = tifffiles.name;
I = imreadtiffstack (A, 256);
%crop tiff stack
sizecrop = size(I);
framenum = sizecrop(3);
cropcollector = ones([100 101 256] , 'uint16'); %preallocates matrix for depositing cropped frames
for k = 1:framenum
frame = I(:,:,k);
I2 = imcrop(frame,[20 0 100 100]);
cropcollector(:,:,k)=I2;
%deinterleaves tiff stack
sizedinlv = size(cropcollector);
framenumdinlv = sizedinlv(3);
oddcollector = ones([100 101 128], 'uint16'); %preallocates array for odd deinterleaved frames
evencollector = ones([100 101 128], 'uint16'); %preallocates array for even deinterleaved frames
countodd = 0;
counteven = 0;
for k2 = (1:framenumdinlv)
if mod(k2, 2)==1
framedinlv = cropcollector(:,:,k2);
countodd = countodd +1;
oddcollector(:,:,countodd)=framedinlv;
else
framedinlv = cropcollector(:,:,k2);
counteven = counteven + 1;
evencollector(:,:,counteven)=framedinlv;
%concatenate
if mod (z, 2)==1;
oddhold = ones([100 101 128], 'uint16');
evenhold = ones([100 101 128], 'uint16');
oddhold = repmat(oddcollector, 1);
evenhold = repmat(evencollector, 1);
else
odd = num2str(1);
even = num2str(2);
brain = ones([100 101 256], 'uint16');
mouse = ones([100 101 256], 'uint16');
% nameoddframes = strcat(A(1:10), odd);
%nameevenframes = strcat(A(1:10), even);
brain = cat(3, oddhold, oddcollector);
mouse = cat(3, evenhold, evencollector);
end
end
end
end
end
%background subtraction

Answers (1)

Ashish Uthama
Ashish Uthama on 15 Aug 2013
First, try familarizing yourself with the profile tool. That will help you find the main parts which are slow.
I am not sure I follow the code, and there are functions there I am not familar with (like imreadtiffstack).
A code pattern which might be useful in what you are trying to do:
stack = cat(3, 1*ones(3,3), 2*ones(3,3), 3*ones(3,3), 4*ones(3,3));
evenStack = stack(:,:,2:2:end);
oddStack = stack(:,:,1:2:end);
  1 Comment
Camille Chaustre McNally
Camille Chaustre McNally on 15 Aug 2013
Thanks for the advice. I used the profile tool and found that there was a significant bottleneck happening at the repmat part of the function... Currently looking for workarounds.

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!