| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| function ReconPhotoVolume_joint_multires(inputPhotoDir,inputREFERENCE,outputVol,... |
| outputVolMask,outputWarpedRef,outputMat,PHOTO_RES,SLICE_THICKNESS,... |
| TARGET_RES,scheduleITs,FS_MATLAB_PATH) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| REL_DICE_INTER_WEIGHT = 10; |
| REL_DICE_INTRA_WEIGHT = 2/50; |
| REL_NCC_INTRA_WEIGHT = 1/50; |
| REL_DETERMINANT_COST = 0.1/50; |
|
|
| |
|
|
| |
| |
| FACTOR_AFFINE_MAT=20; |
| FACTOR_SCALING = 20; |
|
|
| |
|
|
| |
| Nphotos_pre = 2; |
| Nphotos_post = 2; |
|
|
| |
|
|
| tic |
|
|
| |
|
|
| addpath(FS_MATLAB_PATH); |
| addpath([fileparts(mfilename('fullpath')) filesep 'functions/lbfgsb3.0_mex1.2/']); |
| addpath([fileparts(mfilename('fullpath')) filesep 'functions']); |
|
|
| |
| if strcmp(outputWarpedRef(end-3:end),'.mgz')==0 |
| error('Output warped reference volume must be a mgz file to support shear in the vox2ras matrix'); |
| end |
|
|
| |
|
|
|
|
| disp('Extracting slices from photographs') |
| d=dir([inputPhotoDir '/*.mat']); |
| Nphotos=length(d); |
| Iorig=[]; |
| Morig=[]; |
| grouping=[]; |
| for n=1:Nphotos_pre |
| Iorig{end+1}=zeros(3,1); |
| Morig{end+1}=1; |
| end |
| for n=1:Nphotos |
| X=imread([inputPhotoDir '/' d(n).name(1:end-4) '.tif']); |
| load([inputPhotoDir '/' d(n).name(1:end)],'LABELS'); Y=LABELS; clear LABELS |
| grouping=[grouping n*ones(1,max(Y(:)))]; |
| for l=1:max(Y(:)) |
| [mask,cropping]=cropLabelVol(Y==l,5/PHOTO_RES); |
| mask=imfill(mask,'holes'); |
| cropping(3)=1; cropping(6)=3; |
| image=applyCropping(X,cropping); |
| image(repmat(mask,[1 1 3])==0)=0; |
| Iorig{end+1}=image; |
| Morig{end+1}=mask; |
| end |
| end |
| for n=1:Nphotos_post |
| Iorig{end+1}=zeros(3,1); |
| Morig{end+1}=1; |
| end |
|
|
| |
|
|
| Nscales = length(TARGET_RES); |
| Nslices=length(Iorig); |
| if exist([inputPhotoDir filesep '..' filesep 'slice_order.mat']) |
| load([inputPhotoDir filesep '..' filesep 'slice_order.mat'], 'slice_order'); |
| slice_order = [1:Nphotos_pre slice_order+Nphotos_pre slice_order(end)+Nphotos_pre+1:slice_order(end)+Nphotos_pre+Nphotos_post]; |
| else |
| slice_order = 1:Nslices; |
| end |
|
|
| I=[]; |
| M=[]; |
| disp(['Resampling to highest target resolutioscheduleITsn: ' num2str(TARGET_RES(Nscales)) ' mm']); |
| for n=1:Nslices |
| |
| n_ordered = slice_order(n); |
| I{n}=imresize(Iorig{n_ordered},PHOTO_RES/TARGET_RES(Nscales)); |
| |
| M{n}=imresize(double(Morig{n_ordered}),PHOTO_RES/TARGET_RES(Nscales))>0.5; |
| I{n}(M{n}==0)=0; |
| if length(size(I{n})) < 3 |
| I{n} = zeros(3,1); |
| end |
| end |
|
|
| |
|
|
| |
| disp('Coarse alignment and padding'); |
| Imri=[]; |
| Mmri=[]; |
| cogs=zeros(Nslices,2); |
| for n=1:Nslices |
| [r,c]=find(M{n}); |
| if isempty(r) |
| cogs(n,1)=1; |
| cogs(n,2)=1; |
| else |
| cogs(n,1)=round(mean(r)); |
| cogs(n,2)=round(mean(c)); |
| end |
| end |
|
|
| semiLen = round(1.4 * max(cogs)); |
| siz=1+2*semiLen; |
| Imri{Nscales}=[]; |
| Imri{Nscales}.volres=[TARGET_RES(Nscales) TARGET_RES(Nscales) SLICE_THICKNESS]; |
| Imri{Nscales}.vox2ras0=[-TARGET_RES(Nscales) 0 0 0; 0 0 -SLICE_THICKNESS 0; 0 -TARGET_RES(Nscales) 0 0; 0 0 0 1]; |
| Imri{Nscales}.vol=zeros([siz Nslices 3]); |
| Mmri{Nscales}=Imri{Nscales}; |
| Mmri{Nscales}.vol=zeros([siz Nslices]); |
| for n=1:Nslices |
| idx1=semiLen-cogs(n,:); |
| idx2=idx1+size(M{n})-1; |
| Imri{Nscales}.vol(idx1(1):idx2(1),idx1(2):idx2(2),n,:)=reshape(I{n},[size(M{n}) 1 3]); |
| Mmri{Nscales}.vol(idx1(1):idx2(1),idx1(2):idx2(2),n)=M{n}; |
| end |
|
|
|
|
| |
|
|
|
|
| disp('Building resolution pyramid'); |
| for s=1:Nscales-1 |
| for n=1:Nslices |
| mri=Imri{Nscales}; mri.vol=mri.vol(:,:,n,1); mri=downsampleMRI2d(mri,TARGET_RES(s)/TARGET_RES(Nscales)); |
| if n==1 |
| Imri{s}.vol=zeros([size(mri.vol) Nslices 3]); |
| Imri{s}.vox2ras0=mri.vox2ras0; |
| Imri{s}.volres=mri.volres; |
| Mmri{s}=Imri{s}; |
| Mmri{s}.vol=zeros([size(mri.vol) Nslices]); |
| end |
| Imri{s}.vol(:,:,n,1)=mri.vol; |
| mri=Imri{Nscales}; mri.vol=mri.vol(:,:,n,2); mri=downsampleMRI2d(mri,TARGET_RES(s)/TARGET_RES(Nscales)); |
| Imri{s}.vol(:,:,n,2)=mri.vol; |
| mri=Imri{Nscales}; mri.vol=mri.vol(:,:,n,3); mri=downsampleMRI2d(mri,TARGET_RES(s)/TARGET_RES(Nscales)); |
| Imri{s}.vol(:,:,n,3)=mri.vol; |
| mri=Mmri{Nscales}; mri.vol=mri.vol(:,:,n); mri=downsampleMRI2d(mri,TARGET_RES(s)/TARGET_RES(Nscales)); |
| Mmri{s}.vol(:,:,n)=mri.vol>0.5; |
| end |
| end |
|
|
|
|
| |
|
|
| REFmri=MRIread(inputREFERENCE); |
| REFmri.vol=REFmri.vol/max(REFmri.vol(:)); |
| |
| aux=REFmri; |
| REFmri=[]; |
| REFmri.vol=aux.vol; |
| REFmri.volres=aux.volres; |
| REFmri.vox2ras0=aux.vox2ras0; |
|
|
| |
| |
|
|
| |
| disp('Initializing with centers of gravity'); |
| [IIref,JJref,KKref]=ndgrid(1:size(REFmri.vol,1),1:size(REFmri.vol,2),1:size(REFmri.vol,3)); |
| rasRef=vox2ras([IIref(:) JJref(:) KKref(:)],REFmri.vox2ras0); |
| cogREF=sum((rasRef.*repmat(REFmri.vol(:)',[3 1])),2)/sum(REFmri.vol(:)); |
| |
| IIph=[]; JJph=[]; KKph=[]; |
| for s=1:Nscales |
| [a,b,c]=ndgrid(1:size(Imri{s}.vol,1),1:size(Imri{s}.vol,2),1:size(Imri{s}.vol,3)); |
| IIph{s}=a; JJph{s}=b; KKph{s}=c; |
| end |
| rasPH=vox2ras([IIph{Nscales}(:) JJph{Nscales}(:) KKph{Nscales}(:)],Imri{Nscales}.vox2ras0); |
| cogPH=sum((rasPH.*repmat(Mmri{Nscales}.vol(:)',[3 1])),2)/sum(Mmri{Nscales}.vol(:)); |
|
|
| REFmri.vox2ras0(1:3,4)=REFmri.vox2ras0(1:3,4)+cogPH-cogREF; |
|
|
| |
| Nims=size(Imri{1}.vol,3); |
| Ngroups=Nphotos; |
| paramsOptim=cell(1,32); |
| historyX=cell(1,3); |
| historyCost=cell(1,3); |
|
|
| for mode=1:3 |
| |
| disp('*****************'); |
| disp(['* MODE ' num2str(mode) ' *']); |
| disp('*****************'); |
| |
| historyX{mode}=[]; |
| historyCost{mode}=[]; |
| |
| for s=1:Nscales |
| opts=[]; |
| opts.maxIts = scheduleITs(s,mode); |
| opts.maxTotalIts = 30 * opts.maxIts; |
| opts.printEvery = 1; |
| |
| opts.m = 5; |
| |
| if s==1 |
| |
| if mode==1 |
| opts.x0=zeros([3*Nims+7,1]); |
| |
| elseif mode==2 |
| |
| params = zeros([3*Nims+12,1]); |
| |
| |
| params(1:3*Nims)=x(1:3*Nims); |
| params(2:3*Nims)=x(2:3*Nims)/TARGET_RES(s)*TARGET_RES(Nscales); |
| params(3:3*Nims)=x(3:3*Nims)/TARGET_RES(s)*TARGET_RES(Nscales); |
| |
| |
| |
| params(end-2:end) = x(end-2:end); |
| |
| |
| scaling=exp(x(end-6)/FACTOR_SCALING); |
| rotx=x(end-5)/180*pi; |
| roty=x(end-4)/180*pi; |
| rotz=x(end-3)/180*pi; |
| T1=[1 0 0; 0 cos(rotx) -sin(rotx); 0 sin(rotx) cos(rotx)]; |
| T2=[cos(roty) 0 sin(roty); 0 1 0; -sin(roty) 0 cos(roty)]; |
| T3=[cos(rotz) -sin(rotz) 0; sin(rotz) cos(rotz) 0; 0 0 1]; |
| M = scaling * T3 * T2 * T1; |
| params(end-11:end-3) = reshape(M,[9 1])*FACTOR_AFFINE_MAT; |
| |
| opts.x0=params; |
| |
| else |
| |
| params = zeros([6*Nims+12,1]); |
| |
| |
| params(end-11:end) = x(end-11:end); |
| |
| |
| |
| theta=x(1:3:end-12)/180*pi; |
| for i = 1:Nims |
| M=[cos(theta(i)) -sin(theta(i)); sin(theta(i)) cos(theta(i))]; |
| params(4*i-3:4*i)=reshape(M,[4 1])*FACTOR_AFFINE_MAT; |
| end |
| |
| tr=x(2:3:end-12)/TARGET_RES(s)*TARGET_RES(Nscales); |
| tc=x(3:3:end-12)/TARGET_RES(s)*TARGET_RES(Nscales); |
| params(4*Nims+1:2:6*Nims)=tr; |
| params(4*Nims+2:2:6*Nims)=tc; |
| |
| opts.x0=params; |
| end |
| |
| else |
| |
| if mode==1 |
| idx=sort([2:3:length(x)-7 3:3:length(x)-7]); |
| elseif mode==2 |
| idx=sort([2:3:length(x)-12 3:3:length(x)-12]); |
| else |
| idx=sort([4*Nims+1:2:length(x)-12 4*Nims+2:2:length(x)-12]); |
| end |
| opts.x0=x; |
| opts.x0(idx)=opts.x0(idx)/TARGET_RES(s)*TARGET_RES(Nscales); |
| end |
| |
| |
| disp(['Running ' num2str(scheduleITs(s,mode)) ' iterations of BFGS at scale ' num2str(s) ' of ' num2str(Nscales)]); |
| n=length(opts.x0); |
| u = Inf*ones(n,1); |
| l = -u; |
| [x,~,info] = lbfgsb( @(p)costFun(p,cogREF,REFmri,Imri{s},Mmri{s},... |
| IIph{s},JJph{s},KKph{s},REL_NCC_INTRA_WEIGHT,REL_DICE_INTRA_WEIGHT,... |
| REL_DICE_INTER_WEIGHT,REL_DETERMINANT_COST,mode),l, u, opts ); |
| |
| |
| |
| if s<Nscales |
| if mode==1 |
| idx=sort([2:3:length(x)-7 3:3:length(x)-7]); |
| elseif mode==2 |
| idx=sort([2:3:length(x)-12 3:3:length(x)-12]); |
| else |
| idx=sort([4*Nims+1:2:length(x)-12 4*Nims+2:2:length(x)-12]); |
| end |
| x(idx)=x(idx)*TARGET_RES(s)/TARGET_RES(Nscales); |
| |
| else |
| paramsOptim{mode}=x; |
| end |
| |
| |
| historyCost{mode}=[historyCost{mode}; info.err(:,1) ]; |
| end |
| disp(' '); |
| end |
|
|
| disp('Optimization done!'); |
| [~,~, warpedPhotos, warpedMasks, REFvox2ras0New] = ... |
| costFun(x,cogREF,REFmri,Imri{Nscales},Mmri{Nscales},IIph{Nscales},JJph{Nscales},KKph{Nscales},... |
| REL_NCC_INTRA_WEIGHT,REL_DICE_INTRA_WEIGHT,REL_DICE_INTER_WEIGHT,... |
| REL_DETERMINANT_COST, mode); |
|
|
| disp('Writing results to disk...'); |
| mri=Imri{Nscales}; |
| mri.vol=warpedPhotos; |
| MRIwrite(mri,outputVol); |
| mri.vol=warpedMasks; |
| MRIwrite(mri,outputVolMask); |
| REFmri.vox2ras0=REFvox2ras0New; |
| REFmri.volres=sqrt(sum(Imri{Nscales}.vox2ras0(1:3,1:3).^2)); |
| MRIwrite(REFmri,outputWarpedRef); |
| save(outputMat,'paramsOptim','historyX','historyCost'); |
|
|
| disp('All done!'); |
| toc |
|
|
|
|
|
|
|
|
|
|