Skip to content

Commit

Permalink
Added many bugfixes, as well as DTI tracking functionally (using DTI&…
Browse files Browse the repository at this point in the history
…Fibertools)
  • Loading branch information
andreashorn committed Feb 7, 2015
1 parent a510e38 commit 3cbcbcc
Show file tree
Hide file tree
Showing 81 changed files with 2,190 additions and 358 deletions.
3 changes: 1 addition & 2 deletions .version.txt
@@ -1,2 +1 @@
1.348
1.300
1.351
34 changes: 31 additions & 3 deletions ea_autocoord.m
Expand Up @@ -71,18 +71,46 @@ function ea_autocoord(options)
try load([options.root,options.patientname,filesep,'ea_coregctmethod_applied']); end
if exist('coregct_method_applied','var')
try
coregct_method_applied{end+1}=options.normalize.method;
coregct_method_applied{end+1}=options.coregct.method;
catch
coregct_method_applied{1}=options.normalize.method;
clear coregct_method_applied
coregct_method_applied{1}=options.coregct.method;
end
else
coregct_method_applied{1}=options.normalize.method;
coregct_method_applied{1}=options.coregct.method;
end
coregct_method_applied=options.coregct.method;
save([options.root,options.patientname,filesep,'ea_coregctmethod_applied'],'coregct_method_applied');
end

if options.coregctcheck


% export "control" niftis with wireframe of normal anatomy..

ea_show_ctcoregistration(options);

end


% perform fibertracking
if options.ft.do

eval([options.ft.method,'(options)']); % triggers the fibertracking function and passes the options struct to it.
try load([options.root,options.patientname,filesep,'ea_ftmethod_applied']); end
if exist('ft_method_applied','var')
try
ft_method_applied{end+1}=options.ft.method;
catch
clear ft_method_applied
ft_method_applied{1}=options.ft.method;
end
else
ft_method_applied{1}=options.ft.method;
end
ft_method_applied=options.ft.method;
save([options.root,options.patientname,filesep,'ea_ftmethod_applied'],'ft_method_applied');
end

if options.normalize_fibers % normalize fibertracts ? for now these should be denoted in Freiburg format.
ea_normalize_fibers(options);
Expand Down
22 changes: 9 additions & 13 deletions ea_build_DTD.m
@@ -1,10 +1,6 @@
function [mr,b0idx,finames] = ea_build_DTD(bvalue,dirname,dtif,DTDf,HARDIf,bvalf,bvecf)





%% filenamen sammeln -> finames
% converts the dti.nii and dti.bval/.bvec files to Freiburg DTD and raw
% HARDI-formats.



Expand All @@ -22,19 +18,19 @@



%% hier wird in mrstruct umwandelt
%% change into mrstruct format

mr = nifti_to_mrstruct('series3D',finames);


sz = size(mr.dataAy);

%% dtd bauen
%% build dtd

mr.user.bfactor = bvalue; % bwert angeben
mr.user.bfactor = bvalue; % enter bvalue


[mr.user.bDir b0idx] = grads(dirname,bvecf,bvalf); % gradientenrichtungen und b0idxe (siehe unten)
[mr.user.bDir b0idx] = grads(dirname,bvecf,bvalf); % gradient directions



Expand All @@ -45,7 +41,7 @@
b0_image = mean(mr.dataAy(:,:,:,b0idx),4);
mean_DTI = mean(mr.dataAy(:,:,:,setdiff(1:sz(4),b0idx)),4);

display('calculating diffusion tensor ...');
display('Calculating diffusion tensor ...');

Eval = zeros([sz(1:3) 3]);
Evec = zeros([sz(1:3) 3 3]);
Expand All @@ -60,7 +56,7 @@



display('saving ...');
display('Done. Saving ...');


%% save as dtd_struct
Expand Down Expand Up @@ -92,7 +88,7 @@
if isempty(errStr)
msgStr= sprintf('DTI calculations done, write file dtdStruct as %s',dtdname);
else
msgStr= sprintf('error: DTI calculations done, BUT could not write DTD');
msgStr= sprintf('Error: DTI calculations done, BUT could not write DTD');
end

% write HARDI
Expand Down
79 changes: 45 additions & 34 deletions ea_coregctmri_edgedetect.m
Expand Up @@ -6,37 +6,53 @@
if ischar(options) % return name of method.
varargout{1}='Coregister postop-CT with preop-MRI (Edgedetection)';
varargout{2}={'SPM8','SPM12'};
varargout{3}=['0.6,0.4']; % suggestion for alpha-parameter.
return
end


Dopt=12;
maxiter=200;

disp('Loading images...');

%reslice_nii([options.root,options.patientname,filesep,options.prefs.prenii_unnormalized],[options.root,options.patientname,filesep,options.prefs.prenii_unnormalized],[0.5 0.5 0.5]);

reslice_nii([options.root,options.patientname,filesep,options.prefs.prenii_unnormalized],[options.root,options.patientname,filesep,'small_',options.prefs.prenii_unnormalized],[4 4 4]);
reslice_nii([options.root,options.patientname,filesep,options.prefs.rawctnii_unnormalized],[options.root,options.patientname,filesep,'small_',options.prefs.rawctnii_unnormalized],[4 4 4]);
% MR
if isfield(options,'usediffmr_coregct')
reslice_nii([options.root,options.patientname,filesep,options.usediffmr_coregct],[options.root,options.patientname,filesep,'small_',options.usediffmr_coregct],[2 2 2],0);
MR=load_nii_proxi([options.root,options.patientname,filesep,'small_',options.usediffmr_coregct]);
delete([options.root,options.patientname,filesep,'small_',options.usediffmr_coregct]);

else
reslice_nii([options.root,options.patientname,filesep,options.prefs.prenii_unnormalized],[options.root,options.patientname,filesep,'small_',options.prefs.prenii_unnormalized],[2 2 2],0);
MR=load_nii_proxi([options.root,options.patientname,filesep,'small_',options.prefs.prenii_unnormalized]);
delete([options.root,options.patientname,filesep,'small_',options.prefs.prenii_unnormalized]);

end

% CT
reslice_nii([options.root,options.patientname,filesep,options.prefs.rawctnii_unnormalized],[options.root,options.patientname,filesep,'small_',options.prefs.rawctnii_unnormalized],[2 2 2],0);
CT=load_nii_proxi([options.root,options.patientname,filesep,'small_',options.prefs.rawctnii_unnormalized]);

delete([options.root,options.patientname,filesep,'small_',options.prefs.rawctnii_unnormalized]);

disp('Done. Smoothing...');
MR.img(MR.img<100)=0; % remove noise around brain.
MR.img=smooth3(MR.img,'gaussian',[11 11 11]);
CT.img(CT.img<0)=0; % remove negative hounsfield parts.
CT.img=smooth3(CT.img,'gaussian',[11 11 11]);


vizz=1; % visualization on
alphas=options.coregct.coregthreshs;
%% estimate
disp('Loading images...');
CT=load_nii_proxi([options.root,options.patientname,filesep,'small_',options.prefs.rawctnii_unnormalized]);
MR=load_nii_proxi([options.root,options.patientname,filesep,'small_',options.prefs.prenii_unnormalized]);
delete([options.root,options.patientname,filesep,'small_',options.prefs.rawctnii_unnormalized]);
delete([options.root,options.patientname,filesep,'small_',options.prefs.prenii_unnormalized]);

disp('Done. Smoothing...');
MR.img(MR.img<100)=0; % remove noise around brain.
MR.img=smooth3(MR.img,'gaussian',[11 11 11]);
CT.img(CT.img<0)=0; % remove negative hounsfield parts.
CT.img=smooth3(CT.img,'gaussian',[11 11 11]);



if vizz
h=figure('color','w','name','Coregistering CT to MR...','NumberTitle','off');
ctmr=figure('color','w','name','Coregistering CT to MR...','NumberTitle','off','Toolbar','none','MenuBar','none','DockControls','off','WindowButtonMotionFcn',@ea_mouseMove);
axis equal
axis off
end
Expand Down Expand Up @@ -101,10 +117,13 @@
if D<priorD % improvement
fprintf('\n %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f \n\n',[D;M(:)]');
if vizz
plot3(ptMR(1,:),ptMR(2,:),ptMR(3,:),'.','Color',[0.8,0.2,0.8]);
set(0,'CurrentFigure',ctmr);
plot3(ptMR(1,:),ptMR(2,:),ptMR(3,:),'.','Color',[0,188/255,226/255]);
hold on
plot3(ptrCT(1,:),ptrCT(2,:),ptrCT(3,:),'.','Color',[0.2,0.8,0.8]);
plot3(ptrCT(1,:),ptrCT(2,:),ptrCT(3,:),'.','Color',[247/255,133/255,20/255]);
hold off
axis off
axis equal
drawnow
end

Expand Down Expand Up @@ -147,7 +166,11 @@
matlabbatch{1}.spm.util.reorient.transform.transM = M;
matlabbatch{1}.spm.util.reorient.prefix = 'r';
jobs{1}=matlabbatch;
try
cfg_util('run',jobs);
catch
warning('Pre-coregistration did not work. Please choose a different threshold.');
end
clear jobs matlabbatch


Expand Down Expand Up @@ -179,8 +202,15 @@
cfg_util('run',jobs);
clear matlabbatch jobs;

% keep users naming scheme:
try
movefile([options.root,options.patientname,filesep,'r',options.prefs.rawctnii_unnormalized],[options.root,options.patientname,filesep,options.prefs.ctnii_coregistered]);
end


function ea_mouseMove(object, eventdata)
[az,el]=view;
view(az+1,el);


function S=ea_mapvol(X,S,mat) % X= input image (moving), S=space of fixed image (and output), mat=transformation matrix
Expand All @@ -197,25 +227,6 @@



function Y=ea_detect_edges_3d(X,alpha)

%% function detect_edges_3d for Nii-Images
% Andreas Horn, 2014
% in: X (volume Matrix)
% alfa: threshold. Default is 0.1


Y=X;
dimensionality=size(X,3);
ea_dispercent(0,'Sampling');

for slice=1:dimensionality
edgedslice=ea_edgedetection(squeeze(X(:,:,slice)),alpha);
Y(1:end-1,1:end-1,slice)=edgedslice;
ea_dispercent(slice/dimensionality);
end
ea_dispercent(100,'end');




Expand Down
21 changes: 21 additions & 0 deletions ea_coregctmri_edgedetect_and_segment.m
@@ -0,0 +1,21 @@
function varargout=ea_coregctmri_edgedetect_and_segment(options)
% __________________________________________________________________________________
% Copyright (C) 2014 Charite University Medicine Berlin, Movement Disorders Unit
% Andreas Horn

if ischar(options) % return name of method.
varargout{1}='Coregister postop-CT with preop-MRI (Edgedetection+Segment)';
varargout{2}={'SPM8','SPM12'};
varargout{3}=['0.6,0.4']; % suggestion for alpha-parameter.
return
end

if ~exist([options.root,options.patientname,filesep,'c',options.prefs.prenii_unnormalized],'file')
ea_nc_segment(options);
end

%% run standard coregistration

options.usediffmr_coregct=['c',options.prefs.prenii_unnormalized];

ea_coregctmri_edgedetect(options);
28 changes: 28 additions & 0 deletions ea_coregctmri_edgedetect_and_segment_imtbx.m
@@ -0,0 +1,28 @@
function varargout=ea_coregctmri_edgedetect_and_segment_imtbx(options)
% __________________________________________________________________________________
% Copyright (C) 2014 Charite University Medicine Berlin, Movement Disorders Unit
% Andreas Horn

if ischar(options) % return name of method.
varargout{1}='Coregister postop-CT with preop-MRI (Edgedetection+Segment, use ML imagetoolbox)';
if exist('edge.m','file') % check for imtbx.
varargout{2}={'SPM8','SPM12'};
else
varargout{2}={};
end
varargout{3}=['0.9']; % suggestion for alpha-parameter.
return
end

if ~exist([options.root,options.patientname,filesep,'c',options.prefs.prenii_unnormalized],'file')

ea_nc_segment(options);
end



%% run standard coregistration

options.usediffmr_coregct=['c',options.prefs.prenii_unnormalized];

ea_coregctmri_edgedetect_imtbx(options);

0 comments on commit 3cbcbcc

Please sign in to comment.