Commit 5ceba91f authored by actb's avatar actb

updated import export

parent 1aaf1351
<<<<<<< HEAD
% importExport.m
% mcc -m importExport.m -a D:\Andy\git\leverjs\matlab\sqlite-jdbc-3.21.0.jar -a ./assignment
function importExport(filename,outpath, gtFile)
tic
if ~exist('gtFile','var')
gtFile=[];
end
[~,~,ext]=fileparts(filename);
if strcmp(ext,'.LEVER')
export(filename,outpath,gtFile);
elseif exist(filename,'dir')
importFolder(filename,outpath);
else
fprintf(2,'error - argument unknown (tif or .lever): %s\n',filename);
end
y=toc;
if ~isempty(gtFile)
fprintf(1,'importExport elapsed time=%3.1f :: %s,%s,%s\n',y,filename,outpath,gtFile);
else
fprintf(1,'importExport elapsed time=%3.1f :: %s,%s\n',y,filename,outpath);
end
4;
function importFolder(folder,outfolder)
parts=strsplit(folder,filesep);
expname=[parts{end-1} '_' parts{end}];
if exist(fullfile(outfolder,[expname '.h5']),'file')
return
end
fprintf(1,'importing folder %s -> %s\n',folder, fullfile(outfolder,[expname '.h5']));
flist=dir(fullfile(folder,'*.tif'));
im=[];
imd1=[];
for t=1:length(flist)
fname=['t' num2str(t-1,'%03d') '.tif'];
info=imfinfo(fullfile(folder,fname));
if 1==t
try
imd1=MicroscopeData.Original.ReadMetadata(folder,fname);
catch
imd1.PixelPhysicalSize=[1,1,1];
end
end
for z=1:length(info)
im(:,:,z,1,t)=imread(fullfile(folder,fname),z);
end
end
writeH5(im,imd1,expname,outfolder);
function writeH5(im,imd1,expName,outfolder)
% make sure leverjsUtilities project is on your path!
% e.g. path(path,'pathTo\git\utilities\src\MATLAB')
imd=MicroscopeData.MakeMetadataFromImage(im);
imd.DatasetName=expName;
imd.PixelPhysicalSize=imd1.PixelPhysicalSize;
if (size(imd.Dimensions,2)~=3)
imd.Dimensions=[imd.Dimensions 1];
end
MicroscopeData.WriterH5(im,'imageData',imd,'path',outfolder);
function export(filename,outpath,gtFile)
[~,exp,~]=fileparts(filename);
tokens=regexp(exp,'(\D+)_(\d+)','tokens');
dsName=tokens{1}{1};
dsID=tokens{1}{2};
% strip off the _01 or _02 of the exp
if strcmp(dsID,'01')
subdir='01_RES';
else
subdir='02_RES';
end
% exp=exp(1:end-3);
outpath=fullfile(outpath,exp,subdir);
if exist(outpath,'dir')
rmdir(outpath,'s');
end
mkdir(outpath);
fprintf(1,'exporting to folder %s\n',outpath);
conn = database(filename, '','', 'org.sqlite.JDBC', 'jdbc:sqlite:');
CONSTANTS=Read.getConstants(conn);
if isempty(CONSTANTS)
fprintf(2,'bad lever file? %s\n',filename);
return
end
szImage=CONSTANTS.imageData.Dimensions;
szImage=[szImage(2),szImage(1),szImage(3)];
if ~isempty(gtFile)
% the datasets that have a 'ground truth' specify
tids=setExtFamilyGT(conn,CONSTANTS,gtFile);
else
tids=Read.getExtFamilies(conn);% which cells to track
end
if 0==length(tids)
fprintf(2,'no ext families -- no export?\n');
return
end
% generate the label,tBegin,tEnd,parent info
% lbgep=[label, begin, end, parent]
lbep=[];
for i=1:length(tids)
label=tids(i);
cmd=['select min(time),max(time) from tblCells where trackID=' num2str(tids(i))];
q=ljsFetch(conn,cmd);
if isempty(q)
continue
end
tBegin=q{1};
tEnd=q{2};
if strcmp(tBegin,'null') || strcmp(tEnd,'null') || isempty(tBegin) || isempty(tEnd)
continue
end
% find our parent
% note - cellID==trackID for first cell on track
cmd=['select cellID,trackID,time from tblCells inner join tblFamilies on '...
' cellID=cellID_parent where (cellID_child1=' num2str(tids(i)) ...
' or cellID_child2=' num2str(tids(i)) ') and cellID_child2 is not null'];
q=ljsFetch(conn,cmd);
if isempty(q)
parent=0;
else
parent=q{2};
cmd=['select max(time) from tblCells where trackID=' num2str(parent)];
q=ljsFetch(conn,cmd);
if ~isempty(q)
if q{1}>tBegin
fprintf(2,'found bad mitosis -- skipping\n');
parent=0;
end
end
end
% find our children
cmd=['select cellID,trackID,time from tblCells inner join tblFamilies on '...
' (cellID=cellID_child2 or cellID=cellID_child1) AND cellID_child2 is not null where cellID_parent IN '...
' (select cellID from tblCells where trackID=' num2str(tids(i)) ')'];
q=ljsFetch(conn,cmd);
if isempty(q)
childIDs=[0,0];
elseif 2==size(q,1)
childIDs=[q{1,1},q{2,1}];
else
% mitosis corrupted? not sure why. remove the children, mark this
% one as a bad parent, and keep going
fprintf(2,' bad mitotic event detected: corrupt db? --> %s\n',filename);
childIDs=[0,0];
end
% skip track fragments
if tEnd-tBegin<5 && childIDs(1)==0 && parent==0
continue
end
% skip tracks with no child / parent and that never make it out of the
% restricted ROI
if childIDs(1)==0 && parent==0 && roiClipTrack(conn,tids(i),50,szImage)
continue
end
lbep=[lbep; label, tBegin-1, tEnd-1, parent,tids(i),childIDs];
end
if 0==size(lbep,1)
fprintf(2,'no tracks to export?\n');
return;
end
if any(lbep(:,2)>lbep(:,3))
fprintf(2,'bad begin end timing detected ACK\n');
end
lbepOut=lbep(:,1:4);
dlmwrite(fullfile(outpath,'res_track.txt'),lbepOut,'newline','pc','delimiter',' ');
tids=[lbep(:,5),lbep(:,1:3)]; % [trackID,proxy,tBegin,tEnd]
if size(tids,1)>1
tidString=jsonencode(tids(:,1));
tidString(1)='(';
tidString(end)=')';
else
tidString=['(' jsonencode(tids(1)) ')'];
end
for t=1:CONSTANTS.imageData.NumberOfFrames
tImageLabels=[0]; % make sure no one gets over written
% NOTE -- add segCC asc as there is an issue with segCC's of -1 (from
% splits via the tblEnsemble) covering up other segmentations
cmd=['select cellID,trackID from tblCells where time=' num2str(t)...
' and trackID IN ' tidString 'order by segCC asc'];
Q=ljsFetch(conn,cmd);
ctID=cell2mat(Q);
im=zeros(szImage);
for i=1:size(ctID,1)
iCell=Read.getCell(conn,ctID(i,1));
if szImage(3)>1
idx=sub2ind(szImage,iCell.pts(:,2),iCell.pts(:,1),iCell.pts(:,3));
else
idx=sub2ind(szImage,iCell.pts(:,2),iCell.pts(:,1));
end
im(idx)=iCell.trackID;
end
im=setMissingTrackLabels(conn,im,tids,t);
fname=['mask' num2str(t-1,'%03d') '.tif'];
im=uint16(im);
if size(im,3)>1
imwrite(im(:,:,1),fullfile(outpath,fname));
for z=2:size(im,3)
imwrite(im(:,:,z),fullfile(outpath,fname),'WriteMode','append');
end
else
imwrite(im,fullfile(outpath,fname));
end
end
close(conn);
% tids -- trackid,proxy,tbegin,tend
function im=setMissingTrackLabels(conn,im,tids,t)
idsIm=unique(im(:));
idsIm(idsIm==0)=[];
tctc=t-1;
idx=find(tctc>=tids(:,3) & tctc<=tids(:,4));
idsNeeded=tids(idx,1);
idsMissing=setdiff(idsNeeded,idsIm);
if isempty(idsMissing)
return
end
for i=1:length(idsMissing)
% get the centroids
cmd=['select time,centroid from tblCells where trackID=' num2str(idsMissing(i))];
q=ljsFetch(conn,cmd);
trackTime=cell2mat(q(:,1));
cx=cellfun(@jsondecode,q(:,2),'uniformoutput',false);
idx0=find(trackTime==t-1);
idx1=find(trackTime==t+1);
if isempty(idx0) && isempty(idx1)
fprintf(2,'neither t-1 nor t+1 available for interp! failing...\n');
continue
end
if isempty(idx0)
idx0=idx1;
end
if isempty(idx1)
idx1=idx0;
end
cxInterp=(cx{idx0}+cx{idx1}) ./ 2;
cxInterp=round(cxInterp);
if length(size(im))<3
cxInterp=cxInterp(1:2);
end
cxInterp=min(cxInterp,size(im)');
cxInterp=max(cxInterp,ones(length(cxInterp),1));
im(cxInterp(2),cxInterp(1))=idsMissing(i);
end
=======
% importExport.m
% mcc -m importExport.m -a D:\Andy\git\leverjs\matlab\sqlite-jdbc-3.21.0.jar -a ./assignment
function importExport(filename,outpath, gtFile)
......@@ -540,4 +268,3 @@ end
>>>>>>> update from bioimage32
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment