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

dpoburkoSFU
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.

Thanks,
Damon

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

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

tic;
%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
     %Subtract
     for i = 1:nCh
         cList(:,i) = imgList(i:nCh:end,1); %images are stored grouped
by ch then z
         tic;
         for k = 1:nZ  %send images for current channel to ImageJ for
Rolling Ball
             if k == 1
                 MIJ.createImage('tmp', cList{k,i}, true);
             end
             if k > 1
                 MIJ.createImage('tmp2', cList{k,i}, true);
                 MIJ.selectWindow("tmp2");
                 MIJ.run("Copy");
                 MIJ.run('Close');
                 MIJ.selectWindow("tmp");
                 MIJ.run("Add Slice");
                 MIJ.run("Paste");
             end
             progressbar(k/(nZ*nCh),[],[]) ;
         end
         tDo(1) = toc;
         bs = strcat('Rolling=',num2str(ballSizes(i)), ' stack' );
         MIJ.run("Subtract Background...", bs);
         MIJ.run("16-bit");
         tDo(2) = toc;
         %save image to disk - faster than MIJ.getImage
         svPath = strcat('"path=[',tempPathML,']"');
         MIJ.run('Save', svPath);
         MIJ.run('Close')
         tDo(3) = toc;
         data2 = bfopen(tempPathML);
         tDo(4) = toc;

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

         disp(tDo)

         newName =
replace(strcat(savePath,'AIF/',imgName),'.nd2','_AIF.tif');

fstackND2(data2{1,1}(:,1),'alpha',alphas(i),'nhsize',nhsizes(i));
         if i==1 imwrite(ans,newName,'tif'); end
         if i>1 imwrite(ans,newName,'tif','WriteMode','append'); end

         tDo(5) = toc;
         disp(tDo)

     end

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

end

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
http://www.sfu.ca/bpk/faculty_directory/poburko.html


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html