Skip to content

Commit

Permalink
Added ImageRegistrations class
Browse files Browse the repository at this point in the history
- Can automatically download Elastix if needed from GitHub
  • Loading branch information
delucaal committed Jan 9, 2024
1 parent 707aad5 commit 350be06
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
!.gitignore

.DS_Store
Elastix
MRIToolkitDefineLocalVars.m
2 changes: 1 addition & 1 deletion CommandLine/mrtd_deconv_fod.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function mrtd_deconv_fod(varargin)
elseif(strcmp(value,'-iso_rf'))
iso_rf(end+1) = {coptions{input_id+1}};
elseif(strcmp(value,'-mat'))
mat_file = {coptions{input_id+1}};
mat_file = coptions{input_id+1};
end

end
Expand Down
12 changes: 10 additions & 2 deletions CommandLine/mrtd_preproc.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,20 @@ function mrtd_preproc(varargin)
V(~isfinite(V)) = 0;
V2(~isfinite(V2)) = 0;
subplot(1,2,1)
imagesc(V,[0 prctile(V(:),95)]);
max_value = prctile(V(:),95);
if(max_value == 0)
max_value = eps;
end
imagesc(V,[0 max_value]);
axis image; % this ensures that getframe() returns a consistent size
axis off;
drawnow
subplot(1,2,2)
imagesc(V2,[0 prctile(V2(:),95)]);
max_value = prctile(V2(:),95);
if(max_value == 0)
max_value = eps;
end
imagesc(V2,[0 max_value]);
axis image; % this ensures that getframe() returns a consistent size
axis off;
drawnow
Expand Down
14 changes: 8 additions & 6 deletions ExploreDTIInterface/EDTI_Library.m
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,15 @@ function E_DTI_SMECEPI_Single(f_in,par)

for i=1:Le

A = textread(trafo_names{i},'%s');
% A = textread(trafo_names{i},'%s');

DM_info{i}{1} = ...
[str2num(A{7})*(180/pi) str2num(A{6})*(180/pi) str2num(A{8})*(180/pi);
str2num(A{16}) str2num(A{15}) str2num(A{17}(1:end-1));
str2num(A{13}) str2num(A{12}) str2num(A{14});
str2num(A{10}) str2num(A{9}) str2num(A{11})];
% DM_info{i}{1} = ...
% [str2num(A{7})*(180/pi) str2num(A{6})*(180/pi) str2num(A{8})*(180/pi);
% str2num(A{16}) str2num(A{15}) str2num(A{17}(1:end-1));
% str2num(A{13}) str2num(A{12}) str2num(A{14});
% str2num(A{10}) str2num(A{9}) str2num(A{11})];

DM_info{i}{1} = ImageRegistrations.ElastixRotationMatrix(trafo_names{i});

DM_info{i}{2} = EDTI_Library.E_DTI_Tra_Par_2_Tra_Mat(DM_info{i}{1});

Expand Down
2 changes: 0 additions & 2 deletions ImageRegistrations/DW_Elastix_Register.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
%%% Alberto De Luca - [email protected] $%%%
%%% Distributed under the terms of LGPLv3 %%%



function DW_Elastix_Register(moving,fixed,regparlist,output_dir,mMask,fMask,outfilename)
global MRIToolkit;
if(isfield(MRIToolkit,'Elastix'))
Expand Down
2 changes: 0 additions & 2 deletions ImageRegistrations/DW_Elastix_Transform.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
%%% Alberto De Luca - [email protected] $%%%
%%% Distributed under the terms of LGPLv3 %%%



function DW_Elastix_Transform(filein,fileout,transform_parameters)
global MRIToolkit;
if(isfield(MRIToolkit,'Elastix'))
Expand Down
101 changes: 101 additions & 0 deletions ImageRegistrations/ImageRegistrations.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
%%%$ Included in MRIToolkit (https://github.com/delucaal/MRIToolkit) %%%
%%% Alberto De Luca - [email protected] $%%%
%%% Distributed under the terms of LGPLv3 %%%

classdef ImageRegistrations < handle
methods(Static)
function elastix_cmd = BasicElastixCall()
global MRIToolkit;
if(isfield(MRIToolkit,'Elastix'))
elastix_path = MRIToolkit.Elastix.Location;
elastix_cmd = MRIToolkit.Elastix.ElastixCMD;
if(~ispc)
if(~ismac)
elastix_cmd = ['LD_LIBRARY_PATH=' elastix_path ' ' elastix_cmd];
else
elastix_cmd = ['DYLD_LIBRARY_PATH=' elastix_path ' ' elastix_path 'elastix_Mac_64'];
end
end
end
end

function transformix_cmd = BasicTransformixCall()
global MRIToolkit;
if(isfield(MRIToolkit,'Elastix'))
elastix_path = MRIToolkit.Elastix.Location;
transformix_cmd = MRIToolkit.Elastix.TransformixCMD;
if(~ispc)
if(~ismac)
transformix_cmd = ['LD_LIBRARY_PATH=' elastix_path ' ' transformix_cmd];
else
transformix_cmd = ['DYLD_LIBRARY_PATH=' elastix_path ' ' transformix_cmd];
end
end
end
end

function TransfPar = ElastixRotationMatrix(elastix_transform_file)
TransfPar = [];

if(exist(elastix_transform_file,"file") < 1)
disp('Cannot find the specified file')
return
end

f = fopen(elastix_transform_file,'rt');

parameters = {};
while(feof(f) == false)
l = fgetl(f);
if(contains(l,'(TransformParameters'))
l = strrep(l,'(','');
l = strrep(l,')','');
parameters = strsplit(l);
parameters = parameters(2:end);
break
end
end

fclose(f);

TransfPar = [
str2double(parameters{2})*(180/pi) str2double(parameters{1})*(180/pi) str2double(parameters{3})*(180/pi);
str2double(parameters{11}) str2double(parameters{10}) str2double(parameters{12});
str2double(parameters{8}) str2double(parameters{7}) str2double(parameters{9});
str2double(parameters{5}) str2double(parameters{4}) str2double(parameters{6})
];
TransfPar(isnan(TransfPar)) = 0;
end

function DownloadElastix()
global MRIToolkit;
download_url = '';
if(ispc)
download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-win64.zip';
elseif(ismac)
download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-mac.zip';
elseif(isunix)
download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-linux.zip';
end
dest_folder = userpath;
if(isempty(dest_folder))
dest_folder = MRIToolkit.RootFolder;
end
urlwrite (download_url, fullfile(dest_folder,'Elastix.zip'));
unzip(fullfile(dest_folder,'Elastix.zip'),fullfile(dest_folder,'Elastix'));
delete(fullfile(dest_folder,'Elastix.zip'));
if(ismac || isunix)
system(['mv ' fullfile(dest_folder,'Elastix','bin','*') ' ' fullfile(dest_folder,'Elastix')]);
system(['mv ' fullfile(dest_folder,'Elastix','lib','*') ' ' fullfile(dest_folder,'Elastix')]);
system(['chmod +x ' fullfile(dest_folder,'Elastix','elastix')]);
system(['chmod +x ' fullfile(dest_folder,'Elastix','transformix')]);
end

MRIToolkitGetSetVars('MRIToolkit.Elastix.Location',fullfile(dest_folder,'Elastix'));
MRIToolkit.Elastix.Location = fullfile(dest_folder,'Elastix');
MRIToolkit.Elastix.ElastixCMD = fullfile(MRIToolkit.Elastix.Location,'elastix');
MRIToolkit.Elastix.TransformixCMD = fullfile(MRIToolkit.Elastix.Location,'transformix');
end

end
end
71 changes: 71 additions & 0 deletions ImageRegistrations/mk_init.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
%%%$ Included in MRIToolkit (https://github.com/delucaal/MRIToolkit) %%%
%%% Alberto De Luca - [email protected] $%%%
%%% Distributed under the terms of LGPLv3 %%%



% A. De Luca
% Sub-toolbox specific initialization
% 29/03/2018: creation - v1.0
global MRIToolkit;
MRIToolkit.image_registrations_version = 1.1;

addpath(get_executed_file_path())
addpath(fullfile(get_executed_file_path(),'elastix_parameters'))

MRIToolkit.Elastix.Works = false;
try
if(~isfield(MRIToolkit.Elastix,'Location') || exist(MRIToolkit.Elastix.Location,'dir') < 1)
if(exist(fullfile(userpath,'Elastix'),'dir') > 0)
MRIToolkit.Elastix.Location = fullfile(userpath,'Elastix');
MRIToolkit.Elastix.ElastixCMD = fullfile(userpath,'Elastix','elastix');
MRIToolkit.Elastix.TransformixCMD = fullfile(userpath,'Elastix','transformix');
else
MRIToolkit.Elastix.Location = '';
end
end
if(exist(MRIToolkit.Elastix.Location,'dir') > 0)
if(exist(MRIToolkit.Elastix.ElastixCMD,'file') > 0 && exist(MRIToolkit.Elastix.TransformixCMD,'file') > 0)
[a,b] = system(MRIToolkit.Elastix.ElastixCMD);
[c,d] = system(MRIToolkit.Elastix.TransformixCMD);
if(contains(b,'elastix-usage') > 0 && contains(d,'transformix-usage') > 0)
MRIToolkit.Elastix.Works = true;
end
end
end
if(MRIToolkit.Elastix.Works == false)
warning('There seems to be a problem with the installation of Elastix. Do you want me to attempt fixing it by downloading elastix in your home folder?');
answer = input('Answer yes/no: ',"s");
if(strcmpi(answer,'yes'))
download_url = '';
if(ispc)
download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-win64.zip';
elseif(ismac)
download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-mac.zip';
elseif(isunix)
download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-linux.zip';
end
urlwrite (download_url, fullfile(userpath,'Elastix.zip'));
unzip(fullfile(userpath,'Elastix.zip'),fullfile(userpath,'Elastix'));
delete(fullfile(userpath,'Elastix.zip'));
if(ismac || isunix)
system(['mv ' fullfile(userpath,'Elastix','bin','*') ' ' fullfile(userpath,'Elastix')]);
system(['mv ' fullfile(userpath,'Elastix','lib','*') ' ' fullfile(userpath,'Elastix')]);
end
dest_folder = userpath;
if(isempty(dest_folder))
dest_folder = fullfile(MRIToolkit.RootFolder,'Elastix');
else
dest_folder = fullfile(dest_folder,'Elastix');
mkdir(dest_folder);
end
MRIToolkitGetSetVars('MRIToolkit.Elastix.Location',dest_folder);
MRIToolkit.Elastix.Location = dest_folder;
MRIToolkit.Elastix.ElastixCMD = fullfile(dest_folder,'elastix');
MRIToolkit.Elastix.TransformixCMD = fullfile(dest_folder,'transformix');
else
warning('Motion correction and image registrations will not work until Elastix is properly configured.');
end
end
catch
end
2 changes: 1 addition & 1 deletion ImageRegistrations/mk_init.m
Original file line number Diff line number Diff line change
@@ -1 +1 @@
%%%$ Included in MRIToolkit (https://github.com/delucaal/MRIToolkit) %%%%%% Alberto De Luca - [email protected] $%%%%%% Distributed under the terms of LGPLv3 %%%% A. De Luca% Sub-toolbox specific initialization% 29/03/2018: creation - v1.0global MRIToolkit;MRIToolkit.image_registrations_version = 1.0;addpath(get_executed_file_path())addpath(fullfile(get_executed_file_path(),'elastix_parameters'))MRIToolkit.Elastix.Works = false;try if(~isfield(MRIToolkit.Elastix,'Location') || exist(MRIToolkit.Elastix.Location,'dir') < 1) if(exist(fullfile(userpath,'Elastix'),'dir') > 0) MRIToolkit.Elastix.Location = fullfile(userpath,'Elastix'); MRIToolkit.Elastix.ElastixCMD = fullfile(userpath,'Elastix','elastix'); MRIToolkit.Elastix.TransformixCMD = fullfile(userpath,'Elastix','transformix'); else MRIToolkit.Elastix.Location = ''; end end if(exist(MRIToolkit.Elastix.Location,'dir') > 0) if(exist(MRIToolkit.Elastix.ElastixCMD,'file') > 0 && exist(MRIToolkit.Elastix.TransformixCMD,'file') > 0) [a,b] = system(MRIToolkit.Elastix.ElastixCMD); [c,d] = system(MRIToolkit.Elastix.TransformixCMD); if(contains(b,'elastix-usage') > 0 && contains(d,'transformix-usage') > 0) MRIToolkit.Elastix.Works = true; end end end if(MRIToolkit.Elastix.Works == false) warning('There seems to be a problem with the installation of Elastix. Do you want me to attempt fixing it by downloading elastix in your home folder?'); answer = input('Answer yes/no: ',"s"); if(strcmpi(answer,'yes')) download_url = ''; if(ispc) download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-win64.zip'; elseif(ismac) download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-mac.zip'; elseif(isunix) download_url = 'https://github.com/SuperElastix/elastix/releases/download/5.1.0/elastix-5.1.0-linux.zip'; end urlwrite (download_url, fullfile(userpath,'Elastix.zip')); unzip(fullfile(userpath,'Elastix.zip'),fullfile(userpath,'Elastix')); delete(fullfile(userpath,'Elastix.zip')); if(ismac || isunix) system(['mv ' fullfile(userpath,'Elastix','bin','*') ' ' fullfile(userpath,'Elastix')]); system(['mv ' fullfile(userpath,'Elastix','lib','*') ' ' fullfile(userpath,'Elastix')]); end MRIToolkitGetSetVars('MRIToolkit.Elastix.Location',MRIToolkit.Elastix.Location); MRIToolkit.Elastix.Location = MRIToolkit.Elastix.Location; MRIToolkit.Elastix.ElastixCMD = fullfile(userpath,'Elastix','elastix'); MRIToolkit.Elastix.TransformixCMD = fullfile(userpath,'Elastix','transformix'); else warning('Motion correction and image registrations will not work until Elastix is properly configured.'); end endcatchend
%%%$ Included in MRIToolkit (https://github.com/delucaal/MRIToolkit) %%%%%% Alberto De Luca - [email protected] $%%%%%% Distributed under the terms of LGPLv3 %%%% A. De Luca% Sub-toolbox specific initialization% 29/03/2018: creation - v1.0global MRIToolkit;MRIToolkit.image_registrations_version = 1.1;addpath(get_executed_file_path())addpath(fullfile(get_executed_file_path(),'elastix_parameters'))MRIToolkit.Elastix.Works = false;try if(~isfield(MRIToolkit.Elastix,'Location') || exist(MRIToolkit.Elastix.Location,'dir') < 1) if(exist(fullfile(userpath,'Elastix'),'dir') > 0) MRIToolkit.Elastix.Location = fullfile(userpath,'Elastix'); MRIToolkit.Elastix.ElastixCMD = fullfile(userpath,'Elastix','elastix'); MRIToolkit.Elastix.TransformixCMD = fullfile(userpath,'Elastix','transformix'); else MRIToolkit.Elastix.Location = ''; end end if(exist(MRIToolkit.Elastix.Location,'dir') > 0) if(exist(MRIToolkit.Elastix.ElastixCMD,'file') > 0 && exist(MRIToolkit.Elastix.TransformixCMD,'file') > 0) [a,b] = system(ImageRegistrations.BasicElastixCall()); [c,d] = system(ImageRegistrations.BasicTransformixCall()); if(contains(b,'elastix-usage') > 0 && contains(d,'transformix-usage') > 0) MRIToolkit.Elastix.Works = true; end end end if(MRIToolkit.Elastix.Works == false) warning('There seems to be a problem with the installation of Elastix. I can attempt fixing it by downloading elastix in your home folder. Call ImageRegistrations.DownloadElastix() to do so.'); warning('Motion correction and image registrations will not work until Elastix is properly configured.'); endcatchend
Expand Down
3 changes: 2 additions & 1 deletion MRIToolkitInit.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function MRIToolkitInit(SelectedToolboxes)
end

run_folder = get_executed_file_path();

MRIToolkit.RootFolder = run_folder;

% Add all toolboxes if no input is specified
if(nargin < 1)
SelectedToolboxes = available_toolboxes;
Expand Down

0 comments on commit 350be06

Please sign in to comment.