Re: MIJ transfer speed from imagej to matlab

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re: MIJ transfer speed from imagej to matlab

Hi all,

   I have been enjoying getting familiar with the ImageJ/Fiji - Matlab
interoperability afforded by MIJ. It seems like the speed of
transferring data 2D images from Matlb to ImageJ is pretty speedy using
MIJ.createImage (<1s / 80 MB). However, it is painfully slow (~10s / 80
MB) to pull the data back into ImageJ using MIJ.getImage. I am wondering
if anyone has experience this and it there are any suggestions for
transferring data from ImageJ to Matlab more efficiently. At the moment,
it seems to be ~10x faster to instruct ImageJ to save the image to disk
and have Matlab re-open the image, which seems a little odd.

   For reference I am running Winows 10 Pro, Java v8 Update 151 (build
1.8.0_151-b12), Matlab 2017b, ImageJ 1.51v, Fiji updates run including
Matlab link.


Matlab Code:
addpath('C:\Users\.(obscured for privacy)...\\scripts'); %
Update for your ImageJ installation as appropriate
addpath(genpath('C:\Users\.(obscured for privacy)...\')); %
Update for your ImageJ installation as appropriate
addpath(genpath('C:\Users\.(obscured for
privacy)...\Matlab_DropBox_tools')); % Update for your ImageJ
installation as appropriate

currentFolder = pwd;
tempPathMIJ = strcat('"',currentFolder,'\','bridge.tif"');
tempPathML = strcat(currentFolder,'\','tmp.tif');

%Open Nikon ND2 files in a folder using BioFormates importer
folder =  'Z:/matlabOpenND2'; %data fold on a NAS drive

fList = dir(folder);
fListCell = struct2cell(fList)';
fListCell = cellstr(fListCell(:,1));
nd2Index = find(contains(fListCell,'.nd2'));
nd2List = fListCell(nd2Index);
[status,msg,msgID] = mkdir(strrep(strcat(folder,'/AIF'),'\','/' ));

%%parameter for fStack Extended Depth of Field
ballSizes = [250 20 20];
bs = num2str(ballSizes(1)) ;
alphas = [0.2 0.2 0.2];
nhsizes = [9 5 5];
progressbar('bkgd sub','channels','images') ;

t0 = tic;

for j = 1:length(nd2List)
%for j = 1:2
     startLap = tic;
     imgPath = strcat(folder,'\',nd2List(j) );
     imgPath = strrep(imgPath,'\','/');

     %Open ND2 image and store in 'data'.
     disp('time to read nd2')
     t1 = toc(t0);
     data = bfopen(imgPath{1,1});
     f1 = data{1,1}{1,2}; %get file name of first slice of stack
     % parse slice name to determine # of channels and z slices
         semicolons = strfind(f1,';');
         zText = f1(semicolons(2):semicolons(3));
         nZ = str2double(zText(strfind(zText,'/')+1:end-1));
         chText = f1(semicolons(3):end);
         nCh = str2double(chText(strfind(chText,'/')+1:end));
         savePath = f1(1:semicolons(1)-1);
         slashes = strfind(savePath,'/');
         savePath = savePath(1:slashes(length(slashes)));
         imgName = f1(1+slashes(length(slashes)):semicolons(1)-1);

     imgList = data{1,1}; %list of images stored a cell
     %cList = zeroes(nZ,nCh);

     progressbar([],[], j/length(nd2List));
     %for each channel in ND2, throw images to ImageJ, create a Z stack
and Rolling Ball
     for i = 1:nCh
         cList(:,i) = imgList(i:nCh:end,1); %images are stored grouped
by ch then z
         for k = 1:nZ  %send images for current channel to ImageJ for
Rolling Ball
             if k == 1
                 MIJ.createImage('tmp', cList{k,i}, true);
             if k > 1
                 MIJ.createImage('tmp2', cList{k,i}, true);
       "Add Slice");
             progressbar(k/(nZ*nCh),[],[]) ;
         tDo(1) = toc;
         bs = strcat('Rolling=',num2str(ballSizes(i)), ' stack' );"Subtract Background...", bs);"16-bit");
         tDo(2) = toc;
         %save image to disk - faster than MIJ.getImage
         svPath = strcat('"path=[',tempPathML,']"');'Save', svPath);'Close')
         tDo(3) = toc;
         data2 = bfopen(tempPathML);
         tDo(4) = toc;

         progressbar((k/nZ)-0.01,(i/nCh)-0.01,[]) ;


         newName =

         if i==1 imwrite(ans,newName,'tif'); end
         if i>1 imwrite(ans,newName,'tif','WriteMode','append'); end

         tDo(5) = toc;


     lapTime(i) = toc(startLap);
     progressbar([],j/length(nd2List)) ;


tTotal = toc(t0);
disp('All-in-focus analysis complete')


Assistant Professor | Biomedical Physiology & Kinesiology | Simon Fraser University
Member | Centre for Cell Biology, Development, and Disease | Simon Fraser University
Tel: 778 782 9464 | Fax: 778 782 3040 | Office L8004
Mail to: Room K9625, 8888 University Drive, Burnaby BC V5A 1S6

ImageJ mailing list: