0

我在一个记录老鼠大脑图片的神经实验室工作。摄像机记录的原始文件以记录来自交替摄像机的图片(大脑图片、鼠标头部图片、大脑图片等) 我们将这些文件转换为 TIFF,对它们进行去交错处理,然后将这些文件连接起来。

串联太慢了,没有任何用处。我还没有在 Matlab 中学到足够的知识来解决问题。我们如何提高这段代码的速度?

%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
4

0 回答 0